dhxpyt.slider.slider

Slider widget implementation

  1"""
  2Slider widget implementation
  3"""
  4
  5from typing import Any, Callable, Dict, List, Optional, Union
  6import json
  7from pyodide.ffi import create_proxy
  8import js
  9
 10from .slider_config import SliderConfig
 11
 12
 13class Slider:
 14    def __init__(self, config: SliderConfig, widget_parent: str = None):
 15        """
 16        Initializes the Slider widget.
 17
 18        :param config: The SliderConfig object containing the slider configuration.
 19        :param widget_parent: (Optional) The ID of the HTML element where the slider will be attached.
 20        """
 21        config_dict = config.to_dict()
 22
 23        # Handle the tickTemplate function separately
 24        tick_template = None
 25        if 'tickTemplate' in config_dict:
 26            tick_template = config_dict.pop('tickTemplate')
 27            # Create a proxy for the JavaScript side
 28            config_dict['tickTemplate'] = create_proxy(tick_template)
 29
 30        # Create the Slider instance
 31        self.slider = js.dhx.Slider.new(widget_parent, js.JSON.parse(json.dumps(config_dict)))
 32        if tick_template:
 33            # Assign the tickTemplate function
 34            self.slider.config.tickTemplate = config_dict['tickTemplate']
 35
 36    """ Slider API Functions """
 37
 38    def blur(self) -> None:
 39        """Removes focus from a thumb of Slider."""
 40        self.slider.blur()
 41
 42    def destructor(self) -> None:
 43        """Destroys the slider instance and releases occupied resources."""
 44        self.slider.destructor()
 45
 46    def disable(self) -> None:
 47        """Disables the slider."""
 48        self.slider.disable()
 49
 50    def enable(self) -> None:
 51        """Enables the slider."""
 52        self.slider.enable()
 53
 54    def focus(self, extra: bool = None) -> None:
 55        """
 56        Sets focus to a thumb of Slider.
 57
 58        :param extra: (Optional) If the range mode is activated, True will set focus to the second thumb.
 59        """
 60        if extra is not None:
 61            self.slider.focus(extra)
 62        else:
 63            self.slider.focus()
 64
 65    def get_value(self) -> List[float]:
 66        """
 67        Returns the current value of Slider.
 68
 69        :return: An array with the current value of the slider.
 70        """
 71        value = self.slider.getValue()
 72        return value.to_py()
 73
 74    def is_disabled(self) -> bool:
 75        """
 76        Checks whether Slider is disabled.
 77
 78        :return: True if the slider is disabled; otherwise, False.
 79        """
 80        return self.slider.isDisabled()
 81
 82    def paint(self) -> None:
 83        """Repaints Slider on a page."""
 84        self.slider.paint()
 85
 86    def set_value(self, value: Union[str, float, List[float]]) -> None:
 87        """
 88        Sets a value for the slider.
 89
 90        :param value: The value to be set for Slider.
 91        """
 92        self.slider.setValue(value)
 93
 94    """ Slider Event Handlers """
 95
 96    def add_event_handler(self, event_name: str, handler: Callable) -> None:
 97        """
 98        Adds an event handler for the specified event.
 99
100        :param event_name: The name of the event.
101        :param handler: The handler function to attach.
102        """
103        event_proxy = create_proxy(handler)
104        self.slider.events.on(event_name, event_proxy)
105
106    def on_before_change(self, handler: Callable[[float, float, bool], Union[bool, None]]) -> None:
107        """
108        Fires before changing of the slider value.
109
110        :param handler: The handler function with parameters value (float), oldValue (float), isRange (bool).
111                        Return False to prevent changing the slider value.
112        """
113        def event_handler(value, oldValue, isRange):
114            result = handler(value, oldValue, isRange)
115            if result is False:
116                return js.Boolean(False)
117        self.slider.events.on('beforeChange', create_proxy(event_handler))
118
119    def on_blur(self, handler: Callable[[], None]) -> None:
120        """
121        Fires when a thumb of Slider has lost focus.
122
123        :param handler: The handler function.
124        """
125        self.add_event_handler('blur', handler)
126
127    def on_change(self, handler: Callable[[float, float, bool], None]) -> None:
128        """
129        Fires on change of the slider value.
130
131        :param handler: The handler function with parameters value (float), oldValue (float), isRange (bool).
132        """
133        def event_handler(value, oldValue, isRange):
134            handler(value, oldValue, isRange)
135        self.slider.events.on('change', create_proxy(event_handler))
136
137    def on_focus(self, handler: Callable[[], None]) -> None:
138        """
139        Fires when a thumb of Slider has received focus.
140
141        :param handler: The handler function.
142        """
143        self.add_event_handler('focus', handler)
144
145    def on_keydown(self, handler: Callable[[Any], None]) -> None:
146        """
147        Fires when any key is pressed and a thumb of Slider is in focus.
148
149        :param handler: The handler function with parameter event (KeyboardEvent).
150        """
151        def event_handler(event):
152            handler(event)
153        self.slider.events.on('keydown', create_proxy(event_handler))
154
155    def on_mousedown(self, handler: Callable[[Any], None]) -> None:
156        """
157        Fires on pressing the left mouse button over the slider thumb.
158
159        :param handler: The handler function with parameter event (Event).
160        """
161        def event_handler(event):
162            handler(event)
163        self.slider.events.on('mousedown', create_proxy(event_handler))
164
165    def on_mouseup(self, handler: Callable[[Any], None]) -> None:
166        """
167        Fires on releasing the left mouse button over the slider thumb.
168
169        :param handler: The handler function with parameter event (Event).
170        """
171        def event_handler(event):
172            handler(event)
173        self.slider.events.on('mouseUp', create_proxy(event_handler))
class Slider:
 14class Slider:
 15    def __init__(self, config: SliderConfig, widget_parent: str = None):
 16        """
 17        Initializes the Slider widget.
 18
 19        :param config: The SliderConfig object containing the slider configuration.
 20        :param widget_parent: (Optional) The ID of the HTML element where the slider will be attached.
 21        """
 22        config_dict = config.to_dict()
 23
 24        # Handle the tickTemplate function separately
 25        tick_template = None
 26        if 'tickTemplate' in config_dict:
 27            tick_template = config_dict.pop('tickTemplate')
 28            # Create a proxy for the JavaScript side
 29            config_dict['tickTemplate'] = create_proxy(tick_template)
 30
 31        # Create the Slider instance
 32        self.slider = js.dhx.Slider.new(widget_parent, js.JSON.parse(json.dumps(config_dict)))
 33        if tick_template:
 34            # Assign the tickTemplate function
 35            self.slider.config.tickTemplate = config_dict['tickTemplate']
 36
 37    """ Slider API Functions """
 38
 39    def blur(self) -> None:
 40        """Removes focus from a thumb of Slider."""
 41        self.slider.blur()
 42
 43    def destructor(self) -> None:
 44        """Destroys the slider instance and releases occupied resources."""
 45        self.slider.destructor()
 46
 47    def disable(self) -> None:
 48        """Disables the slider."""
 49        self.slider.disable()
 50
 51    def enable(self) -> None:
 52        """Enables the slider."""
 53        self.slider.enable()
 54
 55    def focus(self, extra: bool = None) -> None:
 56        """
 57        Sets focus to a thumb of Slider.
 58
 59        :param extra: (Optional) If the range mode is activated, True will set focus to the second thumb.
 60        """
 61        if extra is not None:
 62            self.slider.focus(extra)
 63        else:
 64            self.slider.focus()
 65
 66    def get_value(self) -> List[float]:
 67        """
 68        Returns the current value of Slider.
 69
 70        :return: An array with the current value of the slider.
 71        """
 72        value = self.slider.getValue()
 73        return value.to_py()
 74
 75    def is_disabled(self) -> bool:
 76        """
 77        Checks whether Slider is disabled.
 78
 79        :return: True if the slider is disabled; otherwise, False.
 80        """
 81        return self.slider.isDisabled()
 82
 83    def paint(self) -> None:
 84        """Repaints Slider on a page."""
 85        self.slider.paint()
 86
 87    def set_value(self, value: Union[str, float, List[float]]) -> None:
 88        """
 89        Sets a value for the slider.
 90
 91        :param value: The value to be set for Slider.
 92        """
 93        self.slider.setValue(value)
 94
 95    """ Slider Event Handlers """
 96
 97    def add_event_handler(self, event_name: str, handler: Callable) -> None:
 98        """
 99        Adds an event handler for the specified event.
100
101        :param event_name: The name of the event.
102        :param handler: The handler function to attach.
103        """
104        event_proxy = create_proxy(handler)
105        self.slider.events.on(event_name, event_proxy)
106
107    def on_before_change(self, handler: Callable[[float, float, bool], Union[bool, None]]) -> None:
108        """
109        Fires before changing of the slider value.
110
111        :param handler: The handler function with parameters value (float), oldValue (float), isRange (bool).
112                        Return False to prevent changing the slider value.
113        """
114        def event_handler(value, oldValue, isRange):
115            result = handler(value, oldValue, isRange)
116            if result is False:
117                return js.Boolean(False)
118        self.slider.events.on('beforeChange', create_proxy(event_handler))
119
120    def on_blur(self, handler: Callable[[], None]) -> None:
121        """
122        Fires when a thumb of Slider has lost focus.
123
124        :param handler: The handler function.
125        """
126        self.add_event_handler('blur', handler)
127
128    def on_change(self, handler: Callable[[float, float, bool], None]) -> None:
129        """
130        Fires on change of the slider value.
131
132        :param handler: The handler function with parameters value (float), oldValue (float), isRange (bool).
133        """
134        def event_handler(value, oldValue, isRange):
135            handler(value, oldValue, isRange)
136        self.slider.events.on('change', create_proxy(event_handler))
137
138    def on_focus(self, handler: Callable[[], None]) -> None:
139        """
140        Fires when a thumb of Slider has received focus.
141
142        :param handler: The handler function.
143        """
144        self.add_event_handler('focus', handler)
145
146    def on_keydown(self, handler: Callable[[Any], None]) -> None:
147        """
148        Fires when any key is pressed and a thumb of Slider is in focus.
149
150        :param handler: The handler function with parameter event (KeyboardEvent).
151        """
152        def event_handler(event):
153            handler(event)
154        self.slider.events.on('keydown', create_proxy(event_handler))
155
156    def on_mousedown(self, handler: Callable[[Any], None]) -> None:
157        """
158        Fires on pressing the left mouse button over the slider thumb.
159
160        :param handler: The handler function with parameter event (Event).
161        """
162        def event_handler(event):
163            handler(event)
164        self.slider.events.on('mousedown', create_proxy(event_handler))
165
166    def on_mouseup(self, handler: Callable[[Any], None]) -> None:
167        """
168        Fires on releasing the left mouse button over the slider thumb.
169
170        :param handler: The handler function with parameter event (Event).
171        """
172        def event_handler(event):
173            handler(event)
174        self.slider.events.on('mouseUp', create_proxy(event_handler))
Slider( config: dhxpyt.slider.slider_config.SliderConfig, widget_parent: str = None)
15    def __init__(self, config: SliderConfig, widget_parent: str = None):
16        """
17        Initializes the Slider widget.
18
19        :param config: The SliderConfig object containing the slider configuration.
20        :param widget_parent: (Optional) The ID of the HTML element where the slider will be attached.
21        """
22        config_dict = config.to_dict()
23
24        # Handle the tickTemplate function separately
25        tick_template = None
26        if 'tickTemplate' in config_dict:
27            tick_template = config_dict.pop('tickTemplate')
28            # Create a proxy for the JavaScript side
29            config_dict['tickTemplate'] = create_proxy(tick_template)
30
31        # Create the Slider instance
32        self.slider = js.dhx.Slider.new(widget_parent, js.JSON.parse(json.dumps(config_dict)))
33        if tick_template:
34            # Assign the tickTemplate function
35            self.slider.config.tickTemplate = config_dict['tickTemplate']

Initializes the Slider widget.

Parameters
  • config: The SliderConfig object containing the slider configuration.
  • widget_parent: (Optional) The ID of the HTML element where the slider will be attached.
slider
def blur(self) -> None:
39    def blur(self) -> None:
40        """Removes focus from a thumb of Slider."""
41        self.slider.blur()

Removes focus from a thumb of Slider.

def destructor(self) -> None:
43    def destructor(self) -> None:
44        """Destroys the slider instance and releases occupied resources."""
45        self.slider.destructor()

Destroys the slider instance and releases occupied resources.

def disable(self) -> None:
47    def disable(self) -> None:
48        """Disables the slider."""
49        self.slider.disable()

Disables the slider.

def enable(self) -> None:
51    def enable(self) -> None:
52        """Enables the slider."""
53        self.slider.enable()

Enables the slider.

def focus(self, extra: bool = None) -> None:
55    def focus(self, extra: bool = None) -> None:
56        """
57        Sets focus to a thumb of Slider.
58
59        :param extra: (Optional) If the range mode is activated, True will set focus to the second thumb.
60        """
61        if extra is not None:
62            self.slider.focus(extra)
63        else:
64            self.slider.focus()

Sets focus to a thumb of Slider.

Parameters
  • extra: (Optional) If the range mode is activated, True will set focus to the second thumb.
def get_value(self) -> List[float]:
66    def get_value(self) -> List[float]:
67        """
68        Returns the current value of Slider.
69
70        :return: An array with the current value of the slider.
71        """
72        value = self.slider.getValue()
73        return value.to_py()

Returns the current value of Slider.

Returns

An array with the current value of the slider.

def is_disabled(self) -> bool:
75    def is_disabled(self) -> bool:
76        """
77        Checks whether Slider is disabled.
78
79        :return: True if the slider is disabled; otherwise, False.
80        """
81        return self.slider.isDisabled()

Checks whether Slider is disabled.

Returns

True if the slider is disabled; otherwise, False.

def paint(self) -> None:
83    def paint(self) -> None:
84        """Repaints Slider on a page."""
85        self.slider.paint()

Repaints Slider on a page.

def set_value(self, value: Union[str, float, List[float]]) -> None:
87    def set_value(self, value: Union[str, float, List[float]]) -> None:
88        """
89        Sets a value for the slider.
90
91        :param value: The value to be set for Slider.
92        """
93        self.slider.setValue(value)

Sets a value for the slider.

Parameters
  • value: The value to be set for Slider.
def add_event_handler(self, event_name: str, handler: Callable) -> None:
 97    def add_event_handler(self, event_name: str, handler: Callable) -> None:
 98        """
 99        Adds an event handler for the specified event.
100
101        :param event_name: The name of the event.
102        :param handler: The handler function to attach.
103        """
104        event_proxy = create_proxy(handler)
105        self.slider.events.on(event_name, event_proxy)

Adds an event handler for the specified event.

Parameters
  • event_name: The name of the event.
  • handler: The handler function to attach.
def on_before_change(self, handler: Callable[[float, float, bool], Optional[bool]]) -> None:
107    def on_before_change(self, handler: Callable[[float, float, bool], Union[bool, None]]) -> None:
108        """
109        Fires before changing of the slider value.
110
111        :param handler: The handler function with parameters value (float), oldValue (float), isRange (bool).
112                        Return False to prevent changing the slider value.
113        """
114        def event_handler(value, oldValue, isRange):
115            result = handler(value, oldValue, isRange)
116            if result is False:
117                return js.Boolean(False)
118        self.slider.events.on('beforeChange', create_proxy(event_handler))

Fires before changing of the slider value.

Parameters
  • handler: The handler function with parameters value (float), oldValue (float), isRange (bool). Return False to prevent changing the slider value.
def on_blur(self, handler: Callable[[], NoneType]) -> None:
120    def on_blur(self, handler: Callable[[], None]) -> None:
121        """
122        Fires when a thumb of Slider has lost focus.
123
124        :param handler: The handler function.
125        """
126        self.add_event_handler('blur', handler)

Fires when a thumb of Slider has lost focus.

Parameters
  • handler: The handler function.
def on_change(self, handler: Callable[[float, float, bool], NoneType]) -> None:
128    def on_change(self, handler: Callable[[float, float, bool], None]) -> None:
129        """
130        Fires on change of the slider value.
131
132        :param handler: The handler function with parameters value (float), oldValue (float), isRange (bool).
133        """
134        def event_handler(value, oldValue, isRange):
135            handler(value, oldValue, isRange)
136        self.slider.events.on('change', create_proxy(event_handler))

Fires on change of the slider value.

Parameters
  • handler: The handler function with parameters value (float), oldValue (float), isRange (bool).
def on_focus(self, handler: Callable[[], NoneType]) -> None:
138    def on_focus(self, handler: Callable[[], None]) -> None:
139        """
140        Fires when a thumb of Slider has received focus.
141
142        :param handler: The handler function.
143        """
144        self.add_event_handler('focus', handler)

Fires when a thumb of Slider has received focus.

Parameters
  • handler: The handler function.
def on_keydown(self, handler: Callable[[Any], NoneType]) -> None:
146    def on_keydown(self, handler: Callable[[Any], None]) -> None:
147        """
148        Fires when any key is pressed and a thumb of Slider is in focus.
149
150        :param handler: The handler function with parameter event (KeyboardEvent).
151        """
152        def event_handler(event):
153            handler(event)
154        self.slider.events.on('keydown', create_proxy(event_handler))

Fires when any key is pressed and a thumb of Slider is in focus.

Parameters
  • handler: The handler function with parameter event (KeyboardEvent).
def on_mousedown(self, handler: Callable[[Any], NoneType]) -> None:
156    def on_mousedown(self, handler: Callable[[Any], None]) -> None:
157        """
158        Fires on pressing the left mouse button over the slider thumb.
159
160        :param handler: The handler function with parameter event (Event).
161        """
162        def event_handler(event):
163            handler(event)
164        self.slider.events.on('mousedown', create_proxy(event_handler))

Fires on pressing the left mouse button over the slider thumb.

Parameters
  • handler: The handler function with parameter event (Event).
def on_mouseup(self, handler: Callable[[Any], NoneType]) -> None:
166    def on_mouseup(self, handler: Callable[[Any], None]) -> None:
167        """
168        Fires on releasing the left mouse button over the slider thumb.
169
170        :param handler: The handler function with parameter event (Event).
171        """
172        def event_handler(event):
173            handler(event)
174        self.slider.events.on('mouseUp', create_proxy(event_handler))

Fires on releasing the left mouse button over the slider thumb.

Parameters
  • handler: The handler function with parameter event (Event).