Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! ✅Check out the new docs.

Returning dcc properties from callback

Is it possible to return various properties of a core component through a callback?
For instance I want to be able to change the limits on a RangeSlider based on a previous input from a RadioItems component.
Can I return the updated min and max limits to the slider?

1 Like

A bit more context to this -
I’ve tried creating the slider in a callback which takes the input from radio buttons and then returning the whole slider (with updated limits) as a ‘children’ property to the app layout. However when I try this, the next callback with calls both the values from the slider and the initial radio buttons.
My code has got a bit complex but I’ve included a simplified version of it below:

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__)
app.config['suppress_callback_exceptions']=True
app.layout = html.Div(children =
    # Choose a location
    [dcc.RadioItems(
        id = 'site_choice',
        options = [{'label': i, 'value': i} for i in ['Edinburgh', 'Heathfield']],
        value = 'Edinburgh'
    ),
    html.Br(),
    # Choose the variables available at the chosen location
    dcc.Dropdown(id = 'variable_options',
    placeholder = 'Select variables'),
    html.Br(),

    # Resample the data
    dcc.RadioItems(id = 'DataResample',
        options = [{'label': i, 'value': i} for i in ['Raw','Daily', 'Weekly','Monthly']],
        value = 'Daily'),
    html.Br(),
    html.Div( id = 'year-slider'),
    html.Br(),
    html.Div( id = 'date-choice'),

    # Then go on to produce graph with resampled data from chosen location
    ])

# Callback to show which variables are available at each location 
@app.callback(Output('variable_options', 'options'),
    [Input('site_choice', 'value')])
def site_user_choices(value):
    if value == 'Edinburgh':
        variable_list = ['Ozone','Nitrogren Dioxide','Carbon Monoxide','PM2.5']
    if value == 'Heathfield':
        variable_list = ['Carbon dioxide','Methane','Carbon Monoxide']
        
    var_options = [{'label': i, 'value': i} for i in variable_list]

    return var_options


### Callback for the year range slider - will set values for date range
@app.callback(Output('year-slider','children'),
    [Input('site_choice','value'),
    Input('DataResample','value')])
def get_year_range(site_value, resample_value):
    if site_value == 'Edinburgh':
        year_min= 1995
        year_max = 2015
    if site_value == 'Heathfield':
        year_min = 2002
        year_max = 2018

    value_range = [year_min, year_max]
    
    slider = dcc.RangeSlider( id = 'year-range',
        min = year_min,
        max = year_max,
        value = value_range,
        allowCross = False)
    return slider


### Callback to print the dates chosen
@app.callback(Output('date-choice','children'),
    [
    Input('site_choice','value'),
    Input('DataResample','value'),
    Input('year-range','value')
    ])
def print_date_choices(site_value, resample_value, slider_vals):
    begin_date = slider_vals[0]
    end_date = slider_vals[1]
    
    out_string = "You've chosen %s between %s and %s sampled %s" % (site_value, begin_date, end_date, resample_value)
    return out_string

if __name__ == '__main__':
    app.run_server(debug=True)

The app doesn’t produce the slider. If I remove the values from the site_choice and the DataResample and leave only the ‘year-range’ then it works but is no longer include information I want about the site and resampling.

The reason I initially wondered if you could return slider properties is because I thought I could make the slider in the app,layout and that might help get round some issues.

Hopefully I’ve explained my issue well enough - apologies for the length of it!
Any help would be greatly appreciated

See https://github.com/plotly/dash/issues/149. For now, you’ll have to use multiple callbacks. Some other topics about this in the forum here: https://community.plotly.com/search?q=multiple%20outputs%20category%3A16

1 Like