Getting option values from the callback

Hi,

In the dropdown, we normally define the values for the option either inside the layout part or should be already created before that.
My question is that in my app, I create a df in the callback, therefore I want my option values to be unique values from the created df.

Right now here is the structure of my app:

1- create df and generate unique values
2- define layout and include the values created above
3- run the callback
  4- This callback will run the same df process in line 1, but will be running every 10 seconds to pick up updates

Now, as you can see that I run the df process two times, one to create the option values and second is where I process my data, my issues with this approach are:
1- I have to run the df process two times, which is not good for performance
2- The values created in the first df process is not changing, as it runs only at the beginning of the app, but the process inside the callback is continuously running, (and new values might be added), but the dropdown is not picking up these new values

I tried defining the dropdown values as empty lists before the layout thinking that it will be updated from the callback, but obviously it is not happening.

So, the question is that, is there a way that I can make the values in the option (within the layout section) pick them up from the callback? Normally this will return a not defined variable inside the layout section.

Here is a (non-workable) example of the code, just for clarification

df = read_csv(file)
options_values = list(df['column1'].unique())

layout = html.Div([
    dcc.Interval(
        id = 'Interval',
        interval = 5 *2000,
        n_intervals = 0
        ),
	dcc.Dropdown(
        id='dropdown1',
        options = options_values,
	html.Button(id='submit_btn', n_clicks=0, children='Submit'),
])

@callback(
    Output('graph_container', 'children'),
    [Input('submit_btn', 'n_clicks'),
	Input('Interval', 'n_intervals'),
	State('dropdown1', 'value'),
])
def Select_options(n_clicks, intervals, value):
	df = read_csv(file)
    SOME PROCESSES
	DO A GRAPH

So the summary is that I want to drop the first two lines of the code and use the one inside the callback function instead

I hope this is enough explanation of the situation

Thanks

Hi @arafeek if you need the value from the dropdown ( as the State('dropdown', 'value') suggests), there is not much you can do.

The only thin you could do, is use a dummy value (which has to be present always in the list(df['column1'].unique()) and update the options within your callback.

	dcc.Dropdown(
        id='dropdown1',
        options = [],
        value=guaranteed_value
    )

and then

@callback(
    Output('graph_container', 'children'),
    Output('dropdown1', 'options'),
    [Input('submit_btn', 'n_clicks'),
	Input('Interval', 'n_intervals'),
	State('dropdown1', 'value'),

])

I actually would rather not do this and leave it as it is right now.

Take into consideration that dropdown has different properties, then you can use as Output in the callback the “options” property sending the new list generated in the callback:

@callback(
    Output('graph_container', 'children'),
    Output('dropdown1', 'options'),    
    [Input('submit_btn', 'n_clicks'),
	Input('Interval', 'n_intervals'),
	State('dropdown1', 'value'),
])
def Select_options(n_clicks, intervals, value):
	df = read_csv(file)
    SOME PROCESSES
    options_values = list(df['column1'].unique())
	DO A GRAPH

    return GRAPH, options_values
1 Like