Replies: 6 comments 2 replies
-
You don't need to tag your property as dirty since you are not mutating the object in place but simply replacing it. def handle_device_update(device_name):
with state:
print(f"Device: {device_name} updated")
device = patch.fixtures.get(device_name) or patch.relays.get(device_name)
device_model = device_name.replace("-", "_")
if hasattr(device, 'state'):
state[f"{device_model}_state"] = device.state
if hasattr(device, 'color'):
state[f"{device_model}_color"] = rgb_int_to_hex(device.color) Also that section of the How to start guide is probably what you were looking for. |
Beta Was this translation helpful? Give feedback.
-
Hi jourdain, I appreciate your quick response. I've been working with your suggestions and I've made some progress. That being said, I've added these async def refresh_ui():
print("refreshing ui...")
with state:
state.flush()
print("UI refreshed") loop = asyncio.new_event_loop() and then in the def handle_device_update(device_name):
print(f"Device: {device_name} updated")
device = patch.fixtures.get(device_name) or patch.relays.get(device_name)
device_model = device_name.replace("-", "_")
if hasattr(device, 'state'):
state.update({f"{device_model}_state": device.state})
if hasattr(device, 'color'):
print(device.color)
state.update({f"{device_model}_color": rgb_int_to_hex(device.color)})
asyncio.set_event_loop(loop)
loop.run_until_complete(refresh_ui()) When a device update arrives on the network, the ouput shows
But the UI is always one action behind, the UI will update to current state if I then keyboard interrupt in the terminal running the trame instance. I feel like I am very close! There is still something I am failing to understand though |
Beta Was this translation helpful? Give feedback.
-
For thoroughness, I've tried throwing a in a flush on all the objects that support it, this doesn't change the behavior at all async def refresh_ui():
print("refreshing ui...")
with state:
state.flush()
server.js_call("flushState('bedroom_iris_color')")
server.ui.flush_content()
print("UI refreshed") |
Beta Was this translation helpful? Give feedback.
-
Okay, I have arrived at a solution that works! asyncio.set_event_loop(loop)
loop.run_until_complete(refresh_ui())
loop.run_until_complete(refresh_ui()) Just run the refresh function twice 😁 |
Beta Was this translation helpful? Give feedback.
-
Indeed async and Python can be tricky to grasp at the beginning. So base on what you are saying (hard to be sure of anything with the little info), it seems that you are starving the web server and therefore prevent any refresh. If you fix that part on your code that is synchronous, the code I share will be enough to fix your issue. If you share your pulling loop, I might be able to help you convert into an async version and free-up the server so the refresh will work. |
Beta Was this translation helpful? Give feedback.
-
Hi, What would change if I don't use async and use threading instead? I have this code that handles uploading a file and also preparing input (conversion to nifti) to be visualized in the UI. When preparation starts, I want to show a VProgressCircular with some text next to it that describes what's happening. I control how to show the progresscircular with
The preparation part happens in a separate thread because it could take some time. Once the preparation is done, I reset state.processing and state.message and I want that to be reflected in the UI (the progresscircular should disappear and text should become empty string). Unfortunately I haven't been able to get those changes reflected in the UI. Moreover, it seems that the app just halts in that level. No subsequent actions happen at all. Here's the
Any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
Hi, I am just getting started with trame. I have a question have dug through the existing discussions and I'm not quite understanding this concept though I suspect it is very solvable.
I have some MQTT devices that are controlled by a module that I wrote, I am attempting to use trame to provide a web interface, but other devices on the network can also change the state of these devices, so I would like the web interface to reflect those changes within a reasonable time after they happen. What I have now successfully gets the device changes into the server state, but the UI does not refresh until some other widget on the page is interacted with (Clicking on a button, pulling a slider, etc)
This is the class for the controls of one such device:
and the function that is called every time a device state is changed externally
If, for instance, a light is blue and you can see that value in its color picker, another device on the network can change it to red. The UI will not react until another element on the page is interacted with, and then the color picker will snap to the correct color.
I have looked briefly at the
StateQueue
object here: https://github.com/Kitware/trame/blob/master/trame/app/asynchronous.py#L4-L5But I am not fully grasping how this would be used for a case like this
Beta Was this translation helpful? Give feedback.
All reactions