When multiple users access the same window (@ui.page("/")), any user's modification to the rows of ui.table() will only take effect in the window accessed by the last user. #5024
-
First Check
Example Codefrom nicegui import ui
def change(key):
rows = {
"a": [{"name": "Aa", "artist": "Artist A"},
{"name": "Ab", "artist": "Artist A"}],
"b": [{"name": "Ba", "artist": "Artist B"},
{"name": "Bb", "artist": "Artist B"}],
"c": [{"name": "Ca", "artist": "Artist C"},
{"name": "Cb", "artist": "Artist C"}]
}
table.rows = rows[key]
@ui.page("/")
def index():
global table
columns = [
{'name': 'name', 'label': 'song_name', 'field': 'name', 'align': 'center'},
{'name': 'artist', 'label': 'song_artist', 'field': 'artist', 'align': 'center'}
]
table = ui.table(columns=columns, rows=[{"name": None, "artist": None}])
ui.input(on_change=lambda e: change(e.value))
ui.run() DescriptionI think the problem seems to be with the When If I manually pass the parameter NiceGUI Version2.20.0 Python Version3.13.5 BrowserEdge, Firefox Operating SystemWindows Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @SHDocter, With every call to Instead of keeping track of a single table only, you can try keeping a weak set of tables and iterate over them to update their rows. Using a from weakref import WeakSet
from nicegui import ui
tables: WeakSet[ui.table] = WeakSet()
def change(key):
for table in tables:
table.rows = {
'a': [{'name': 'Aa', 'artist': 'Artist A'}, {'name': 'Ab', 'artist': 'Artist A'}],
'b': [{'name': 'Ba', 'artist': 'Artist B'}, {'name': 'Bb', 'artist': 'Artist B'}],
'c': [{'name': 'Ca', 'artist': 'Artist C'}, {'name': 'Cb', 'artist': 'Artist C'}],
}[key]
@ui.page('/')
def index():
table = ui.table(columns=[
{'name': 'name', 'label': 'song_name', 'field': 'name', 'align': 'center'},
{'name': 'artist', 'label': 'song_artist', 'field': 'artist', 'align': 'center'}
], rows=[{'name': None, 'artist': None}])
tables.add(table)
ui.input(on_change=lambda e: change(e.value))
ui.run() To prove that no tables are held in memory after closing browser tabs, you can count then with an app.timer(0.5, lambda: (gc.collect(), print(f'tables: {len(tables)}'))) |
Beta Was this translation helpful? Give feedback.
Sure. If you don't need a global reference, you can simply pass the current instance to the change handler: