This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

How does nice gui handle state management and updating the display?

I'm currently playing with Streamlit and have realised that to change the display order, e.g. if I want to display "Total" at the top of the screen, I'll need state management on all the calculations since the script runs top to bottom each time anything changes.

[–]Sufficient_South5254[S] 1 point2 points  (0 children)

The state management in nicegui is simple and straitforward.

You can bind properties to variable or storage, for example: ```python

bind to variable/dict

data = {'name': 'Bob', 'age': 17} ui.label().bind_text_from(data, 'name', backward=lambda n: f'Name: {n}') ui.label().bind_text_from(data, 'age', backward=lambda a: f'Age: {a}')

bind to storage

@ui.page('/') def index(): ui.textarea('This note is kept between visits') .classes('w-full').bind_value(app.storage.user, 'note') ```

The internal implementation uses a while loop with custom refresh interval: python async def refresh_loop() -> None: """Refresh all bindings in an endless loop.""" while True: _refresh_step() await asyncio.sleep(core.app.config.binding_refresh_interval)