1

I'm trying to show a radio button with the options argument being a dict, then changing that dict in a function and trying to update the radio button for it to show the new option.

The code for this is the following:

from nicegui import ui

class RecipeSelector:
    def __init__(self,initval):
        self.value = initval
        self.name = 'Recipeselector'

selectedOption  = RecipeSelector('1')
optionsDict = {'1': 'Opt1', '2': 'Opt2'}

async def UpdateOptions():
    result = await suredialog
    if result == 'Yes':
        optionsDict = {'1': 'Opt1', '2': 'Opt2'} #<- This line is the issue.
        index=int(max(optionsDict.keys()))+1
        optionsDict[str(index)] = 'Opt'+str(index)
        optionSelector.update()
        ui.notify("Radio Updated")
    else:
        ui.notify(f'You chose {result}')

with ui.dialog() as suredialog, ui.card():
    ui.label('Are you sure?')
    with ui.row():
        ui.button('Yes', on_click=lambda: suredialog.submit('Yes'))
        ui.button('No', on_click=lambda: suredialog.submit('No'))

optionSelector = ui.radio(optionsDict).bind_value(selectedOption, 'value')
ui.button('New Option', on_click=UpdateOptions)

ui.run()
  • What exactly is your question? What is not working as expected? When I try your code with the radio value being bound to a label, the label updates just fine when selecting options 1, 2 or 3. – Falko Apr 13 '23 at 07:53
  • Thanks for your answer Falko, I've just reviewed the example code and It does not represent my actual code. I've just updated the question with a complete code that does reproduce the issue. The issue seems to be the line that I commented with "<- This line is the issue.", because when I comment the whole line, the radio button updates correctly, but when it's there, the radio button does not update. That line in the real code is a dict of the files that I have in a local folder where the key is an index and the value, the name of the file. – Lucas A. Rivas P. Apr 13 '23 at 13:18
  • I see. You're introducing a new variable `optionsDict` with the same name like the global dictionary. But apart from that it has nothing to do with the select element. Why do you do that? Why don't you simply remove the line in question and modify the global dictionary? This way the code works, I guess. – Falko Apr 13 '23 at 17:01
  • That was it, I was confused when using and creating the optionsDict variable. I thought I was updating the global variable, but instead, I was modifying the local one. The only doubt that I'm left with is how the UI Updates example works (https://nicegui.io/documentation#ui_updates) if you are not calling globally the chart object inside the functions or passing it as an argument? Thanks again for your answer. It may have been simple, but it helped me to get my project moving again. – Lucas A. Rivas P. Apr 14 '23 at 14:29
  • That's good news! Regarding the UI Update examples: As long as you don't _create_ a new variable inside a function, Python automatically uses the global (or nonlocal) variable for read access. The examples access the global `chart` object only to _get_ its options. Even though the options are written, the `chart` object is not. This way there is _no_ local variable created that would hide the global one. I hope that makes sense. – Falko Apr 14 '23 at 16:41

0 Answers0