Not sure why I have such a hard time wrapping my head around dash
! I’m trying to migrate an app from shiny
to dash
. You can see it here. Here was my attempt via dash
:
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import json
app = dash.Dash()
app.css.append_css({'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'})
app.config['suppress_callback_exceptions']=True
app.layout = html.Div([
html.Div([
html.Label('number of inputs'),
dcc.Slider(id='n_inputs', min=1, max=5, step=1, value=1,
marks={i: i for i in [1, 2, 3, 4, 5]}),
html.Br(),
html.Div(id='input_container')],
style={'float': 'left', 'width': '20%',
'padding': '0 0 50px 5px'}),
html.Div(id='input_values',
style={'float': 'right', 'width': '75%'})
])
@app.callback(
Output('input_container', 'children'),
[Input('n_inputs', 'value')])
def make_sliders(n):
return [dcc.Input(id='input_{}'.format(i),
type='text',
value='') for i in range(n)]
@app.callback(
Output('input_values', 'children'),
[Input('input_{}'.format(i), 'value') for i in range(5)])
def printcontents(txt0, txt1, txt2, txt3, txt4):
vals = [txt0, txt1, txt2, txt3, txt4]
print(vals)
return vals
if __name__ == '__main__':
app.run_server(debug=True)
Some issues:
-
the container for input values doesn’t update unless the slider is at 5. Does my list of
[Input()]
ids imply that they all need to be present for that callback to fire? -
the text box contents don’t persist when changing the slider. Each time the input slider changes, I return a fresh list of
dcc.Slider
components, but I’m not sure how to know what their values were and keep them… do I need to do something like this were you’re storing the last value/history in a hiddendiv
and bring it back? -
I found this and this (replicates of each other) and see what you’re doing, but the inputs were all defined ahead of time. Should I do that? Is there no way to generate them on the fly?
-
Is there no way to generate an
Input
list based on some other value? It’s so tempting to want to get at the value of the slider when specifying a list ofInputs
so I can know how many there should be.
Thanks for any pointers. I have some other attempts at the bottom here, but decided to re-post as that was initially about something else (solved by finding out about State
), and now it’s about a dynamic number of UI components.