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
42
README.md
42
README.md
@ -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`
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user