Modularization concepts #3822
-
DescriptionHi everyone. I'm trying to structure my code so I can keep everything maintainable. I saw the modularization examples and get some ideas. I've ended with some structure like this: from nicegui import ui
class MyPage:
def __init__(self) -> None:
@ui.page('/')
def root():
ui.button('Create card', on_click=lambda: self.card_creator(text='Ey'))
with ui.row() as self.card_container:
pass
def card_creator(self, text):
with self.card_container:
with ui.card():
ui.label(text)
MyPage()
ui.run() This minimal example is intended to set a "container" ready to be filled upon some event. Am I thinking it right? It seems to run right, but when I have two tabs open (both localhost:8080) the first one stops creating cards and only the second one works properly. It seems that the server creates the instance of MyPage on one tab but destroys it when the other tab is open. Probably I'm missing something. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @MateoSaezMata, There is only one instance of Pylint actually warns about defining instance attributes "outside" the |
Beta Was this translation helpful? Give feedback.
If you visit the page again, only the function
root()
will be called again on the single object created by theMyPage()
constructor call.Thats basic Python. NiceGUI does not do any magic re-evaluation of you whole program like Streamlit does. Hence the
MainPage()
is only called once (actually twice, becausereload=True
by default inui.run
).