1
"""
2
Renders Streamz Stream objects.
3
"""
4 7
from __future__ import absolute_import, division, unicode_literals
5

6 7
import sys
7

8 7
import param
9

10 7
from .base import ReplacementPane
11

12

13 7
class Streamz(ReplacementPane):
14

15 7
    always_watch = param.Boolean(default=False, doc="""
16
        Whether to watch even when not displayed.""")
17

18 7
    rate_limit = param.Number(default=0.1, bounds=(0, None), doc="""
19
        The minimum interval between events.""")
20

21 7
    _rename = {'rate_limit': None, 'always_watch': None}
22

23 7
    def __init__(self, object=None, **params):
24 7
        super(Streamz, self).__init__(object, **params)
25 7
        self._stream = None
26 7
        if self.always_watch:
27 0
            self._setup_stream()
28

29 7
    @param.depends('always_watch', 'object', 'rate_limit', watch=True)
30 2
    def _setup_stream(self):
31 7
        if self.object is None or (self.always_watch and self._stream):
32 7
            return
33 0
        elif self._stream:
34 0
            self._stream.destroy()
35 0
            self._stream = None
36 0
        if self._pane._models or self.always_watch:
37 0
            self._stream = self.object.latest().rate_limit(self.rate_limit).gather()
38 0
            self._stream.sink(self._update_inner)
39

40 7
    def _get_model(self, doc, root=None, parent=None, comm=None):
41 7
        model = super(Streamz, self)._get_model(doc, root, parent, comm)
42 7
        self._setup_stream()
43 7
        return model
44

45 7
    def _cleanup(self, root=None):
46 0
        super(Streamz, self)._cleanup(root)
47 0
        if not self._pane._models and self._stream:
48 0
            self._stream.destroy()
49 0
            self._stream = None
50

51
    #----------------------------------------------------------------
52
    # Public API
53
    #----------------------------------------------------------------
54

55 7
    @classmethod
56 2
    def applies(cls, obj):
57 7
        if 'streamz' in sys.modules:
58 7
            from streamz import Stream
59 7
            return isinstance(obj, Stream)
60 0
        return False

Read our documentation on viewing source code .

Loading