Hello! @chriddyp
I am confused. Is this like calling an object propiertie but not in the callback?
Let me be more specific.
I am in the need to generate checklists depending of the value of a dropdown.
If I select āoption1ā I generate 2 checklist (There are shown in tabs) if I select āoption 2ā I generate 5 checklist( (each one shown in a tab). The need is to obtain the value of each checklist generated (boxes selected). This task is normally done in the callback header, where you can put each checklist as a State (better as State since input will launch an action, and the selection of checkboxes is passive, at least in my page), but if I put then as entry argument in the callback, there will be inconsistencies since āoption 1ā of dropdown only generates ids āchecklist_1ā and āchecklist_2ā. and āoption 2ā generates āchecklist from 1 to 5ā, so I can not put all the State() 's because in option 1 an error will appear since checklist_3/4/5 are not generated (at least in option 1, but remember this is only a small example, options are more and number could change to bigger ones).
Like a pythonist way yo solve this maybe something like:
NOTE: I know how many are generated because of a function that consult a database
@app.callback(...)
def function(...):
value_required = [app.layout['all-tabs']['tabs_1']['checklist_'+str(i)]['value'] for i in range(get_number_of_generated_checklists)]
.
.
.
Does this thread want to improve this? I know this will leave āstateā kind of obsolete since I can refer to objects directly in the function without reference then in the callback header.
EDIT:
I review the you code @chriddyp and see something similar to:
app.layout['all-tabs']['tabs_1']['checklist_'+str(i)]['value']
I tried in a dropdown but it does not update with the value selected.
scene: dropdown and buttom. The buttom just make a print:
Note: Toast is just for the ouput of the callback.
Note2: I know this scenario could be done by a callback, but for the previous scenario the need of calling an object inside a function only by the name and propiertie without puttin in the header, is high.
dcc.Dropdown(id='dropdown-1',
options=[
{'label': i, 'value': i} for i in range(5)
],
)
html.Div(id='div-enviar',children=[
dbc.Button("click-me", id="but1", color="primary", className="mr-1")
]
@app.callback( [Output("positioned-toast", "is_open")]
,inputs=[Input("but1", "n_clicks")])
def click_me(n_clicks):
print(app.layout['dropdown-1'].options)
return False
if : print(app.layout['dropdown-1'].options)
the print goes as expected. a list of dictionaries with keys label and value.
then, I suppose that I could call a value so I did:
print(app.layout['dropdown-1'].value)
and the answer is key_error āvalueā not found.
I thought that was a mistakeā¦ but then i tried:
print(app.layout['dropdown-1'])
and clicked the buttom before selecting an option, and after. The answer was the same, The object like a dictionary of the propierties wrotten in code before deploying in the web browser.
So now i am wondering if there is a line needed to āupdateā the layout, because in the web-broser, the propiertie āvalueā appears in the object dropdown-1.
Thanks for any clue on solving this!
E.M.