mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-20 00:10:03 +02:00
updates Readme explaining how to use units with number sliders
This commit is contained in:
parent
b4edc31030
commit
31967d0d43
112
README.md
112
README.md
@ -364,52 +364,92 @@ In this example, `MySlider` overrides the `min`, `max`, `step_size`, and `value`
|
|||||||
|
|
||||||
Here's an illustrative example:
|
Here's an illustrative example:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
import pydase
|
import pydase
|
||||||
import pydase.components
|
import pydase.components
|
||||||
|
|
||||||
|
|
||||||
class MySlider(pydase.components.NumberSlider):
|
class MySlider(pydase.components.NumberSlider):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
value: float,
|
value: float,
|
||||||
on_change: Callable[[float], None],
|
on_change: Callable[[float], None],
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(value=value)
|
super().__init__(value=value)
|
||||||
self._on_change = on_change
|
self._on_change = on_change
|
||||||
|
|
||||||
# ... other properties ...
|
# ... other properties ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self) -> float:
|
def value(self) -> float:
|
||||||
return self._value
|
return self._value
|
||||||
|
|
||||||
@value.setter
|
@value.setter
|
||||||
def value(self, new_value: float) -> None:
|
def value(self, new_value: float) -> None:
|
||||||
if new_value < self._min or new_value > self._max:
|
if new_value < self._min or new_value > self._max:
|
||||||
raise ValueError("Value is either below allowed min or above max value.")
|
raise ValueError("Value is either below allowed min or above max value.")
|
||||||
self._value = new_value
|
self._value = new_value
|
||||||
self._on_change(new_value)
|
self._on_change(new_value)
|
||||||
|
|
||||||
|
|
||||||
class MyService(pydase.DataService):
|
class MyService(pydase.DataService):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.voltage = MySlider(
|
self.voltage = MySlider(
|
||||||
5,
|
5,
|
||||||
on_change=self.handle_voltage_change,
|
on_change=self.handle_voltage_change,
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle_voltage_change(self, new_voltage: float) -> None:
|
def handle_voltage_change(self, new_voltage: float) -> None:
|
||||||
print(f"Voltage changed to: {new_voltage}")
|
print(f"Voltage changed to: {new_voltage}")
|
||||||
# Additional logic here
|
# Additional logic here
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
service_instance = MyService()
|
service_instance = MyService()
|
||||||
my_service.voltage.value = 7 # Output: "Voltage changed to: 7"
|
my_service.voltage.value = 7 # Output: "Voltage changed to: 7"
|
||||||
pydase.Server(service_instance).run()
|
pydase.Server(service_instance).run()
|
||||||
````
|
```
|
||||||
|
|
||||||
|
- Incorporating units in `NumberSlider`
|
||||||
|
|
||||||
|
The `NumberSlider` is capable of displaying units alongside values, enhancing its usability in contexts where unit representation is crucial. When utilizing `pydase.units`, you can specify units for the slider's value, allowing the component to reflect these units in the frontend.
|
||||||
|
|
||||||
|
Here's how to implement a `NumberSlider` with unit display:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pydase
|
||||||
|
import pydase.components
|
||||||
|
import pydase.units as u
|
||||||
|
|
||||||
|
class MySlider(pydase.components.NumberSlider):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
value: u.Quantity = 0.0 * u.units.V,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(value)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def value(self) -> u.Quantity:
|
||||||
|
return self._value
|
||||||
|
|
||||||
|
@value.setter
|
||||||
|
def value(self, value: u.Quantity) -> None:
|
||||||
|
if value.m < self._min or value.m > self._max:
|
||||||
|
raise ValueError("Value is either below allowed min or above max value.")
|
||||||
|
self._value = value
|
||||||
|
|
||||||
|
class MyService(pydase.DataService):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.voltage = MySlider()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
service_instance = MyService()
|
||||||
|
service_instance.voltage.value = 5 * u.units.V
|
||||||
|
print(service_instance.voltage.value) # Output: 5 V
|
||||||
|
pydase.Server(service_instance).run()
|
||||||
|
```
|
||||||
|
|
||||||
#### `ColouredEnum`
|
#### `ColouredEnum`
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user