6

I’m trying to create a time-series Dash line graph that has multiple interactive dropdown user input variables. I would ideally like each of the dropdown inputs to allow for multiple selections.

While I’m able to create the drop down menus successfully, the chart isn’t updating like I’d like. When I allow the dropdowns to have multiple selections, I get an error that arrays are different lengths. And when I limit the dropdowns to one selection, I get an error that [‘Vendor_Name’] is not in index. So this may be two separate problems.

Graph that doesn’t work:

Snippet of Excel data imported into DF

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd
#import plotly.graph_objs as go 
df = pd.read_csv("Data.csv", sep = "\t")
df['YearMonth'] = pd.to_datetime(df['YearMonth'], format = '%Y-%m')
cols = ['Product_1', 'Product_2', 'Product_3']
vendor = df['Vendor'].unique()

app = dash.Dash('Data')

app.layout = html.Div([
    html.Div([
        html.Div([

            html.Label('Product'),
            dcc.Dropdown(
                 id = 'product',
                 options = [{
                         'label' : i, 
                         'value' : i
                 } for i in cols],
                multi = True,
                value = 'Product_1'

                 ),
                ]),

        html.Div([

            html.Label('Vendor'),
            dcc.Dropdown(
             id = 'vendor',
             options = [{
                     'label' : i, 
                     'value' : i
             } for i in vendor],
            multi = True,
             value = 'ABC')
             ,
        ]),
            ]),

    dcc.Graph(id = 'feature-graphic')
    ])


@app.callback(Output('feature-graphic', 'figure'),
    [Input('product', 'value'),
     Input('vendor', 'value')])


def update_graph(input_vendor, input_column):


    df_filtered = df[df['Vendor'] == input_vendor]

##also tried setting an index because of the error I was getting. Not sure if necessary
    df_filtered = df_filtered.set_index(['Vendor']) 

    traces = []

    df_by_col = df_filtered[[input_column, 'YearMonth']]

    traces.append({

        'x' :pd.Series(df_by_col['YearMonth']),
        'y' : df_by_col[input_column],
        'mode' : 'lines',
        'type' : 'scatter',
        'name' :'XYZ'}
        )

    fig = {
                    'data': traces,
                    'layout': {'title': 'Title of Chart'}
                    }
    return fig


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

Thanks in advance for helping! Still new-ish to Python, but very excited about Dash’s capabilities. I’ve been able to create other graphs with single inputs, and have read through documentation.

2 Answers 2

3

Here is the approach I followed: (editing common example available in google with my approach):

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

app = dash.Dash(__name__)

all_options = {
    'America': ['New York City', 'San Francisco', 'Cincinnati'],
    'Canada': [u'Montréal', 'Toronto', 'Ottawa']
}
app.layout = html.Div([
    dcc.Dropdown(
        id='countries-dropdown',
        options=[{'label': k, 'value': k} for k in all_options.keys()],
        value='America',  #default value to show
        multi=True,
        searchable=False
    ),

    dcc.Dropdown(id='cities-dropdown', multi=True, searchable=False, placeholder="Select a city"),

    html.Div(id='display-selected-values')
])

@app.callback(
    dash.dependencies.Output('cities-dropdown', 'options'),
    [dash.dependencies.Input('countries-dropdown', 'value')])
def set_cities_options(selected_country):
    if type(selected_country) == 'str':
        return [{'label': i, 'value': i} for i in all_options[selected_country]]
    else:
        return [{'label': i, 'value': i} for country in selected_country for i in all_options[country]]

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

Workaround here is: When there is single input present in parent dropdown, the value is in string format. But for multiple values, it comes in list format.
This code also work perfectly and gets updated automatically even when you click on cross option to remove any selected option.

Note: I have used 'placeholder' attribute instead of defining default value for it as it made no sense in this case. But you can also update the value dynamically in similar way.

Sign up to request clarification or add additional context in comments.

Comments

2

1 input data

The data as it is in the csv is hard to loop. And I would argue that it is the main reason your code does not work, because you seem to understand the fundamental code structure. Having put on my SQL glasses I think you should try to get it to sth like

Date, Vendor, ProductName, Value

2 callback input types change

multi is tricky because it changes switches between returning a str if only 1 item is selected and list if more than one is selected

3 callback return type

you code returns a dict but the callback declared figure as the return type

but here is the code with debugging traces of print() and sleep()

import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import time

df = pd.read_csv("Data.csv", sep="\t")
df['YearMonth'] = pd.to_datetime(df['YearMonth'], format='%Y-%m')
products = ['Product_1', 'Product_2', 'Product_3']
vendors = df['Vendor'].unique()

app = dash.Dash('Data')

app.layout = html.Div([
    html.Div([
        html.Div([
            html.Label('Product'),
            dcc.Dropdown(
                id='product',
                options=[{'label' : p, 'value' : p} for p in products],
                multi=True,
                value='Product_1'
            ),
        ]),
        html.Div([
            html.Label('Vendor'),
            dcc.Dropdown(
                id='vendor',
                options=[{'label': v, 'value': v} for v in vendors],
                multi=True,
                value='ABC'
            ),
        ]),
    ]),
    dcc.Graph(id='feature-graphic', figure=go.Figure())
])


@app.callback(
    Output('feature-graphic', 'figure'),
    [Input('product', 'value'),
     Input('vendor', 'value')])
def update_graph(input_product, input_vendor):
    # df_filtered[['Product_1', 'YearMonth']]
    if type(input_product) == str:
        input_product = [input_product]
    if type(input_vendor) == str:
        input_vendor= [input_vendor]

    datasets = ['']
    i = 1
    for vendor in input_vendor:
        df_filtered = df[df['Vendor'] == vendor]
        for product in input_product:
            datasets.append((df_filtered[['YearMonth', 'Vendor', product]]).copy())
            datasets[i]['ProductName'] = product
            datasets[i].rename(columns={product: 'Value'}, inplace=True)
            i += 1
    datasets.pop(0)
    print(datasets)

    traces = ['']
    for dataset in datasets:
        print(dataset)
        time.sleep(1)
        traces.append(
            go.Scatter({
                'x': dataset['YearMonth'],
                'y': dataset['Value'],
                'mode': 'lines',
                'name': f"Vendor: {dataset['Vendor'].iloc[0]} Product: {dataset['ProductName'].iloc[0]}"
        }))
    traces.pop(0)
    layout = {'title': 'Title of Chart'}

    fig = {'data': traces, 'layout': go.Layout(layout)}
    return go.Figure(fig)


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

quick and dirty disclosure:

If you handle the 1. issue it will dramatically simplify everything. So I'd try to isolate the pd.DataFrame() juggling out of the callback and into the upper I/O part.

1) don't use counters in for loops

2) my variable names aren't the best either

3) the following style is caveman's python and there must be a better way:

traces = ['']
traces.append(this_and_that)
traces.pop(0)

Generally:

using print(input_variable) and print(type(input_variable)) gets my wheels most of the time out of the mud.

after all

you should notice that each trace got its individual name which will show up in the legend. Clicking on the name in the legend will add or remove the trace without the need for@app.callback()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.