1
"""
2
Defines Player widgets which offer media-player like controls.
3
"""
4 7
from __future__ import absolute_import, division, unicode_literals
5

6 7
import param
7

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

13

14 7
class PlayerBase(Widget):
15

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

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

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

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

28 7
    height = param.Integer(default=80)
29

30 7
    width = param.Integer(default=510)
31

32 7
    _widget_type = _BkPlayer
33

34 7
    _rename = {'name': None}
35

36 7
    __abstract = True
37

38

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

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

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

51 7
    value = param.Integer(default=0, doc="Current player value")
52

53 7
    _supports_embed = True
54

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

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

71

72

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

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

83 7
    value = param.Parameter()
84

85 7
    _rename = {'name': None, 'options': None}
86

87 7
    _source_transforms = {'value': None}
88

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

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

Read our documentation on viewing source code .

Loading