-
First Check
Example Codefrom nicegui import ui
nic_list = [{'name': 'if1'},
{'name': 'if2'},]
@ui.refreshable
def generate_nic_dd():
global nicdd
with ui.dropdown_button('Network Interface') as nicdd:
for nic in nic_list:
name = nic['name']
with ui.item(on_click=lambda name=name: ui.notify(f'You clicked item {name}')):
ui.label(name)
def update_nic_list():
# I tried delay with async.sleep()
# I tried nicdd.update()
if nicdd.value is False:
#nic_list = get_nics_in_system()
generate_nic_dd.refresh()
generate_nic_dd()
ui.timer(1, update_nic_list)
ui.run() DescriptionI want to update a drop down menu each second, so dropdown has value, which is True when opnend. Any Idea or proposal for a better approach here? NiceGUI Version2.11.1 Python VersionPython 3.10.12 BrowserOther Operating SystemLinux Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@arne123 Thank you for your report. To summarize:
But then, two burning questions:
With that said, I think this is a UI/UX design problem more than a NiceGUI problem. I would propose:
See if the code makes more sense: from nicegui import ui
nic_list = [{'name': 'if1'},
{'name': 'if2'},]
@ui.page('/')
def main_page():
# go to /selector, and /manage_interfaces
ui.link('Selector', '/selector')
ui.link('Manage Interfaces', '/manage_interfaces')
@ui.page('/manage_interfaces')
def manage_interfaces_page():
ui.label('Manage Interfaces')
interface_input = ui.input('Enter interface name', placeholder='e.g. eth0')
def add_interface_from_input():
print('Adding interface:', interface_input.value)
add_interface(interface_input.value)
def add_interface(name):
# Add the interface to the list
nic_list.append({'name': name})
ui.navigate.reload()
ui.button('Add Interface', on_click=add_interface_from_input)
def delete_interface(name):
# Delete the interface from the list
global nic_list
nic_list = [nic for nic in nic_list if nic['name'] != name]
ui.navigate.reload()
for nic in nic_list:
name = nic['name']
with ui.card():
ui.label(name)
ui.button('Delete', on_click=lambda name=name: delete_interface(name))
ui.link('Back', '/')
@ui.page('/selector')
def selector_page():
last_nic_list = nic_list.copy()
with ui.row():
with ui.dropdown_button('Network Interface') as nicdd:
ui.label('Loading...')
def generate_nic_dd():
global nic_list
nicdd.clear()
with nicdd:
for nic in nic_list:
name = nic['name']
with ui.item(on_click=lambda name=name: ui.notify(f'You clicked item {name}')):
ui.label(name)
reload_button.set_visibility(False)
reload_button = ui.button('Reload', on_click=generate_nic_dd)
reload_button.set_visibility(False)
def poll_nic_list():
nonlocal last_nic_list
if nic_list != last_nic_list:
last_nic_list = nic_list.copy()
reload_button.set_visibility(True)
ui.notify('Network interface list changed, please reload.')
generate_nic_dd()
ui.timer(1, poll_nic_list)
ui.run() Notable changes:
|
Beta Was this translation helpful? Give feedback.
@arne123 Thank you for your report.
To summarize:
nic_list
is the list of interfaces, which may change if, say, user plugs in a USB WiFi dongle.nic_list
is refreshed once every second, which disrupts the dropdown element, and closes the dropdown forcibly on the user.nic_list
refresh is paused when the dropdown is open.But then, two burning questions:
With that said, I think th…