Ran into a snag. I’m dynamically generating a number of fields like so:
## function to generate input widgets
## in_1 is a slider from 1-4, initialized to 1
## in_2 is a div placeholder for on-the-fly generated text fields
## run is a button
def generate_inputs():
in_1 = [html.Label(html.Strong('select number')),
dcc.Slider(id='num', min=1, max=4, value=1,
marks={i: i for i in range(5)})]
in_2 = [html.Label(html.Strong('text inputs')),
html.Div(id='ins')]
run = [html.Label(html.Strong('ready?')),
html.Button('run!', id='run', n_clicks=0)]
input_list = [in_1, in_2, run]
widgets = list()
for sublist in input_list:
sublist.append(html.H1(''))
for item in sublist:
widgets.append(item)
return widgets
## generate_input and an empty ghost div where I can send results
app.layout = html.Div([
html.H2('app'),
html.Div(generate_inputs(),
style={'float': 'left', 'width': '20%', 'padding': '0 0 50px 0'}),
html.Div(id='lookups',
style={'float': 'right', 'width': '75%'})
])
## when the number of the slider changes, run this
## return back the number of dcc.Inputs matching the desired
## number, each with an id of 'i'
@app.callback(
Output('in_2', 'children'),
[Input('in_1', 'value')])
def ins_generate(n):
boxes = [html.Div(
dcc.Input(id=str(i),
placeholder='a thing', value='thing1',
size=40, type='text')) for i in range(n)]
return boxes
## when the button is clicked access our on-the-fly field, which should get the name '0' `str(i)`
@app.callback(
Output('lookups', 'children'),
[Input('run', 'n_clicks')],
[State('0', 'value')])
def lookups(run_clicks, a0):
print(a0)
I get “error loading dependencies” when I take this route. I think it has to do with figuring out what the field id
is for my variable number of text entries. Sorry if this is a completely separate issue about how I’m trying to do a dynamic number of inputs.