How to save plotly dash Input value as global variable

I am creating a dashboard using Plotly Dash that requires data to be filtered by date (sliders don’t seem to work since some charts are categorical).

I would like to allow the user to input a date which I would then use as a global variable. I would import this global variable to filter my dataframes in other python files, and then create the charts. My attempt below doesn’t work; the variable start_date does not update as the user enters a date. Please let me know if this is possible, or if I should approach it in a different way.

app = dash.Dash()

app.layout = html.Div([
    dcc.Input(id='date', value='start date (yyyy-mm-dd)', type='text'),
    html.Button(id='submit-button', type='submit', children='Submit'),

start_date = 0

@app.callback(Output('output_div', 'children'),
                [Input('submit-button', 'n_clicks')],
                [State('date', 'value')],
def update_output(clicks, input_value):
    if clicks:
        print(clicks, input_value)
        global start_date
        start_date = datetime.strptime(input_value, '%Y-%m-%d').date()
        return start_date

if __name__ == '__main__':
    app.run_server(debug=True, port=8051)
1 Like