I'm trying to make some order in a script that uses nicegui. I'm using a dictionary to store all button labels and the pages they will redirect to.
I'm iterating over all keys in pages dictionary and calling ui.button for each of them.
from nicegui import ui
pages = {'page_one':'Page','page_two':'Page 2','page_three':'Page 3'}
for page in pages:
ui.button(pages[page], on_click=lambda page=page: ui.open(page))
when binding page it becomes a completely different type nicegui.events.ClickEventArguments instead of a normal string
not binding page causes all buttons to redirect to page_three
ui.button(pages[page], on_click=lambda: ui.open(page))
I feel like I completely misunderstand what actually binding is and how lambdas operate.
Why is the page variable changing type when bound to function in this context?
NiceGUI automatically passes event arguments to event handlers - depending on whether they expect arguments or not. This way you can conveniently write something like
without caring about possible event arguments.
But you can also accept and consume event arguments:
Now if you define an event handler like
lambda page=page: ui.open(page), NiceGUI tries to pass event arguments to thepageparameter, because the lambda function expects arguments. In order to avoid overwriting thepageargument with event arguments, you can insert another unused parameter, commonly denoted with_, that swallows the event argument while keeping thepageuntouched:lambda _, page=page: ui.open(page).Disclaimer: I'm one of the NiceGUI developers. And I'm thinking about whether NiceGUI could detect a case like
lambda page=page: ui.open(page)and not passing event arguments, since all function parameters already have default values.