updates Readme explaining how to use units with number sliders

This commit is contained in:
Mose Müller 2023-12-13 11:23:44 +01:00
parent b4edc31030
commit 31967d0d43

View File

@ -409,7 +409,47 @@ In this example, `MySlider` overrides the `min`, `max`, `step_size`, and `value`
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`