holoviz / panel
1
"""
2
Defines Player widgets which offer media-player like controls.
3
"""
4 7
import param
5

6 7
from ..models.widgets import Player as _BkPlayer
7 7
from ..util import isIn, indexOf
8 7
from .base import Widget
9 7
from .select import SelectBase
10

11

12 7
class PlayerBase(Widget):
13

14 7
    interval = param.Integer(default=500, doc="Interval between updates")
15

16 7
    loop_policy = param.ObjectSelector(default='once',
17
                                       objects=['once', 'loop', 'reflect'], doc="""
18
        Policy used when player hits last frame""")
19

20 7
    step = param.Integer(default=1, doc="""
21
        Number of frames to step forward and back by on each event.""")
22

23 7
    show_loop_controls = param.Boolean(default=True, doc="""
24
        Whether the loop controls radio buttons are shown""")
25

26 7
    height = param.Integer(default=80)
27

28 7
    width = param.Integer(default=510)
29

30 7
    _widget_type = _BkPlayer
31

32 7
    _rename = {'name': None}
33

34 7
    __abstract = True
35

36

37 7
class Player(PlayerBase):
38
    """
39
    The Player provides controls to play and skip through a number of
40
    frames defined by explicit start and end values.  The speed at
41
    which the widget plays is defined by the interval, but it is also
42
    possible to skip frames using the step parameter.
43
    """
44

45 7
    start = param.Integer(default=0, doc="Lower bound on the slider value")
46

47 7
    end = param.Integer(default=10, doc="Upper bound on the slider value")
48

49 7
    value = param.Integer(default=0, doc="Current player value")
50

51 7
    _supports_embed = True
52

53 7
    def __init__(self, **params):
54 7
        if 'length' in params:
55 0
            if 'start' in params or 'end' in params:
56 0
                raise ValueError('Supply either length or start and end to Player not both')
57 0
            params['start'] = 0
58 0
            params['end'] = params.pop('length')-1
59 7
        elif params.get('start', 0) > 0 and not 'value' in params:
60 0
            params['value'] = params['start']
61 7
        super().__init__(**params)
62

63 7
    def _get_embed_state(self, root, values=None, max_opts=3):
64 0
        if values is None:
65 0
            values = list(range(self.start, self.end, self.step))
66 0
        return (self, self._models[root.ref['id']][0], values,
67
                lambda x: x.value, 'value', 'cb_obj.value')
68

69

70

71 7
class DiscretePlayer(PlayerBase, SelectBase):
72
    """
73
    The DiscretePlayer provides controls to iterate through a list of
74
    discrete options.  The speed at which the widget plays is defined
75
    by the interval, but it is also possible to skip items using the
76
    step parameter.
77
    """
78

79 7
    interval = param.Integer(default=500, doc="Interval between updates")
80

81 7
    value = param.Parameter()
82

83 7
    _rename = {'name': None, 'options': None}
84

85 7
    _source_transforms = {'value': None}
86

87 7
    def _process_param_change(self, msg):
88 7
        values = self.values
89 7
        if 'options' in msg:
90 7
            msg['start'] = 0
91 7
            msg['end'] = len(values) - 1
92 7
            if values and not isIn(self.value, values):
93 0
                self.value = values[0]
94 7
        if 'value' in msg:
95 7
            value = msg['value']
96 7
            if isIn(value, values):
97 7
                msg['value'] = indexOf(value, values)
98 7
            elif values:
99 0
                self.value = values[0]
100 7
        return super()._process_param_change(msg)
101

102 7
    def _process_property_change(self, msg):
103 7
        if 'value' in msg:
104 7
            value = msg.pop('value')
105 7
            if value < len(self.options):
106 7
                msg['value'] = self.values[value]
107 7
        return msg

Read our documentation on viewing source code .

Loading