mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-21 00:40:01 +02:00
feat: adding backend NumberSlider
This commit is contained in:
parent
fa0d69feb8
commit
95dd12bf7f
@ -1,7 +1,9 @@
|
|||||||
from .data_service import DataService
|
from .data_service import DataService
|
||||||
from .data_service_list import DataServiceList
|
from .data_service_list import DataServiceList
|
||||||
|
from .number_slider import NumberSlider
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"DataService",
|
"DataService",
|
||||||
"DataServiceList",
|
"DataServiceList",
|
||||||
|
"NumberSlider",
|
||||||
]
|
]
|
||||||
|
@ -472,7 +472,9 @@ class DataService(rpyc.Service):
|
|||||||
|
|
||||||
if isinstance(value, DataService):
|
if isinstance(value, DataService):
|
||||||
result[key] = {
|
result[key] = {
|
||||||
"type": type(value).__name__,
|
"type": type(value).__name__
|
||||||
|
if type(value).__name__ in ("NumberSlider")
|
||||||
|
else "DataService",
|
||||||
"value": value.serialize(),
|
"value": value.serialize(),
|
||||||
"readonly": False,
|
"readonly": False,
|
||||||
"doc": inspect.getdoc(value),
|
"doc": inspect.getdoc(value),
|
||||||
@ -542,3 +544,38 @@ class DataService(rpyc.Service):
|
|||||||
- value (Any): The value of the parameter.
|
- value (Any): The value of the parameter.
|
||||||
"""
|
"""
|
||||||
self._notification_callbacks.append(callback)
|
self._notification_callbacks.append(callback)
|
||||||
|
|
||||||
|
def apply_updates(self, data: dict[str, Any]) -> None:
|
||||||
|
"""
|
||||||
|
Applies updates to the attributes of this DataService instance.
|
||||||
|
|
||||||
|
For each key-value pair in the provided data dictionary, this function
|
||||||
|
checks if the attribute with the corresponding name exists in the instance,
|
||||||
|
and if the current value of this attribute is different from the new value.
|
||||||
|
If the attribute exists and the values are different, it updates the attribute
|
||||||
|
in the instance with the new value.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data (dict): A dictionary containing the updates to be applied. The keys
|
||||||
|
should correspond to the names of attributes in the DataService instance
|
||||||
|
and the values should be the new values for these attributes.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
This function assumes that all values can be directly compared with
|
||||||
|
the != operator and assigned with the = operator. If some attributes need
|
||||||
|
more complex update logic, this function might not work correctly for them.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# TODO: check if attribute is DataService instance -> nested updates?
|
||||||
|
# Might not be necessary as each DataService instance change would trigger its
|
||||||
|
# own frontend_update notification.
|
||||||
|
|
||||||
|
for key, new_value in data.items():
|
||||||
|
if hasattr(self, key):
|
||||||
|
current_value = getattr(self, key)
|
||||||
|
if current_value != new_value:
|
||||||
|
setattr(self, key, new_value)
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
f"Attribute {key} does not exist in the DataService instance."
|
||||||
|
)
|
||||||
|
16
src/pyDataInterface/data_service/number_slider.py
Normal file
16
src/pyDataInterface/data_service/number_slider.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from .data_service import DataService
|
||||||
|
|
||||||
|
|
||||||
|
class NumberSlider(DataService):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
value: float | int = 0,
|
||||||
|
min: int = 0,
|
||||||
|
max: int = 100,
|
||||||
|
step_size: float = 1.0,
|
||||||
|
) -> None:
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
self.value = value
|
||||||
|
self.step_size = step_size
|
||||||
|
super().__init__()
|
@ -9,6 +9,7 @@ from loguru import logger
|
|||||||
|
|
||||||
from pyDataInterface import DataService
|
from pyDataInterface import DataService
|
||||||
from pyDataInterface.config import OperationMode
|
from pyDataInterface.config import OperationMode
|
||||||
|
from pyDataInterface.data_service import NumberSlider
|
||||||
from pyDataInterface.version import __version__
|
from pyDataInterface.version import __version__
|
||||||
|
|
||||||
|
|
||||||
@ -52,7 +53,11 @@ class WebAPI:
|
|||||||
@sio.on("frontend_update") # type: ignore
|
@sio.on("frontend_update") # type: ignore
|
||||||
def handle_frontend_update(sid: str, data: FrontendUpdate) -> None:
|
def handle_frontend_update(sid: str, data: FrontendUpdate) -> None:
|
||||||
logger.debug(f"Received frontend update: {data}")
|
logger.debug(f"Received frontend update: {data}")
|
||||||
setattr(self.service, data["name"], data["value"])
|
attr = getattr(self.service, data["name"])
|
||||||
|
if isinstance(attr, DataService):
|
||||||
|
attr.apply_updates(data["value"])
|
||||||
|
else:
|
||||||
|
setattr(self.service, data["name"], data["value"])
|
||||||
|
|
||||||
self.__sio = sio
|
self.__sio = sio
|
||||||
self.__sio_app = socketio.ASGIApp(self.__sio)
|
self.__sio_app = socketio.ASGIApp(self.__sio)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user