How do models work and how do they relate to storage? #5549
-
|
Hi, I recently discovered NiceGUI and I am very much interested in giving it a go, but there are some things I don't quite understand after working through the documentation. The NiceGUI documentation on bindings says "NiceGUI is able to directly bind UI elements to models" and provides this code example: from nicegui import ui
class Demo:
def __init__(self):
self.number = 1
demo = Demo()
v = ui.checkbox('visible', value=True)
with ui.column().bind_visibility_from(v, 'value'):
ui.slider(min=1, max=3).bind_value(demo, 'number')
ui.toggle({1: 'A', 2: 'B', 3: 'C'}).bind_value(demo, 'number')
ui.number().bind_value(demo, 'number')
ui.run()I understand the example is using script mode and the "code will be executed once per client connection". Just from looking at the code though, So, my first question would be: Will an individual Reading about pages, they seem to be the preferred way to structure an application. Intuitively, I would upgrade the above code like this: from nicegui import ui
class Demo:
def __init__(self):
self.number = 1
@ui.page("/my_page")
def my_page():
demo = Demo()
v = ui.checkbox('visible', value=True)
with ui.column().bind_visibility_from(v, 'value'):
ui.slider(min=1, max=3).bind_value(demo, 'number')
ui.toggle({1: 'A', 2: 'B', 3: 'C'}).bind_value(demo, 'number')
ui.number().bind_value(demo, 'number')
ui.run()Would that be correct? Would it yield the same behavior? Now it definitely looks like there should be one As an immediate follow-up, in case there is some magic happening here, where is the state represented by Finally, NiceGUI also has storage with well-documented behavior that can also be bound to UI element properties. How does the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi @NiklasKappel, In the context of multiple visitors, this demo might be a bit misleading. As you noticed correctly, it is using script mode because there is no page function, so NiceGUI creates one for you. This function wraps all code, including the Your second snippet results in the same behavior, because there's a new If you move the The state is stored on the server, in a simple Python object. Clients are updated via websocket connections that are kept alive until they disconnect. The storage module allows to store data, e.g., across server restarts. Where does your
What type of object to use? That strongly depends on your requirements (see the storage table). |
Beta Was this translation helpful? Give feedback.
Hi @NiklasKappel,
In the context of multiple visitors, this demo might be a bit misleading. As you noticed correctly, it is using script mode because there is no page function, so NiceGUI creates one for you. This function wraps all code, including the
Democlass definition and thedemoinstance, which kind of misses the point of such a data model class. The app is still working, but - yes - every visitor gets to see a newdemoinstance.Your second snippet results in the same behavior, because there's a new
demoinstance for every visitor. The only technical difference is that all instances are created from the sameDemoclass, not like in the first snippet where even the class was creat…