Alonso
Alonso

Reputation: 51

How to use variables from callback in Python-Plotly-Dash layout?

I want to draw my Dash layout with a dynamic number of components (NumericInputs in particular). Everything goes fine when this number is static, I can change the values with a callback and everything. I use a for to control that.

However, when I want to change the number of components (beginning with the biggest number and conditioning when it is less than that), it is attached to the initial size and labels. I want to use a session variable from a callback like dcc.Store(id='k') to control it, but nothing. I have found solutions to use them in other callbacks and in fact I use them between them, but this time I need to use it in the layout context.

Any ideas or experiences about it? Thank you!

rows = [html.Tr([html.Td(categories[i]['name'], id={'type': 'categories_name', 'index': i}),
html.Td(categories[i]['size'], id={'type': 'categories_size', 'index': i}),
html.Td(daq.NumericInput(id={'type': 'threshold', 'index': i},
                         min=0, max=categories[i]['size'],
                         value=categories[i]['size'],
                         size=80)),
html.Td("0%", id={'type': 'percent', 'index': i})])

for i in range(k) if  i<(dcc.Store(id='k'))]

containment_body = [html.Tbody(rows)]
containment_table = dbc.Table(table_header + containment_body, bordered=False, className="text-right table-borderless table-sm m-0")

enter image description here

Upvotes: 0

Views: 1608

Answers (1)

Alonso
Alonso

Reputation: 51

I tried to solve with a lot of methods and finally used multi-page apps. I also used a get_layout method called from a callback and Flask cookies. See https://dash.plotly.com/urls. It keeps the reactive features.

 @dash_app.callback(
   Output('page-content', 'children'),
    [Input('url', 'pathname')])
def display_page(pathname):
    print ("Displaying", pathname)
    if pathname == '/':
        dash.callback_context.response.set_cookie('campus_cookie', "/campus1")
        return get_layout(len(campuses["campus1"]["categories"]), campuses["campus1"]["no_solutions"], campuses["campus1"]["budget"], campuses["campus1"]["buckets"], campuses["campus1"]["d"], campuses["campus1"]["pi"], campuses["campus1"]["p"], campuses["campus1"]["categories"])
    else:
        dash.callback_context.response.set_cookie('campus_cookie', pathname)            
        return get_layout(len(campuses[pathname[1:]]["categories"]), campuses[pathname[1:]]["no_solutions"], campuses[pathname[1:]]["budget"], campuses[pathname[1:]]["buckets"], campuses[pathname[1:]]["d"], campuses[pathname[1:]]["pi"], campuses[pathname[1:]]["p"], campuses[pathname[1:]]["categories"])

Upvotes: 1

Related Questions