Dash Type Error

Hello Everyone!

I just tried to add an additional dropdown bar for the year and when I did, the error: TypeError: init() takes from 1 to 2 positional arguments but 3 were given, popped up. Please advise or provide resources, thank you.

Sample Code:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd

df = pd.read_csv(
“pulse.csv”
)
group_options = df[“Group”].unique()
year_options =df[“Year”].unique()

app = dash.Dash()

app.layout = html.Div([
html.H2(“Pulse Analysis Report”),
html.Div(
[
dcc.Dropdown(
id=“Group”,
options=[{
‘label’: i,
‘value’: i
} for i in group_options],
multi=True,
value=‘All Groups’),
],

    [
        dcc.Dropdown(
            id="Year",
            options=[{
                'label' : i,
                'value' : i,
            } for i in year_options],
            multi=True,
            value='All Years'),
    ],



    style={'width': '25%',
           **'display': 'inline-block'}),**
dcc.Graph(id='funnel-graph'),

])

@app.callback(
dash.dependencies.Output(‘funnel-graph’, ‘figure’),
[dash.dependencies.Input(‘Group’, ‘value’)])
def update_graph(Group):
if Group == “All Groups”:
df_plot = df.copy()
else:
df_plot = df[df[‘Group’] == Group]

pv = pd.pivot_table(
    df_plot,
    index=['Question'],
    columns=["Response"],
    values=['Percentage'],
    aggfunc='mean',
    fill_value=0)

trace1 = go.Bar(x=pv.index, y=pv[('Percentage', 'Skipped')], name='skipped')
trace2 = go.Bar(x=pv.index, y=pv[('Percentage', 'Neutral')], name='neutral')
trace3 = go.Bar(x=pv.index, y=pv[('Percentage', 'Agree')], name='agree')
trace4 = go.Bar(x=pv.index, y=pv[('Percentage', 'Disagree')], name='disagree')
trace5 = go.Bar(x=pv.index, y=pv[('Percentage', 'Strongly_Disagree')], name=' strongly_disagree')
trace6 = go.Bar(x=pv.index, y=pv[('Percentage', 'Strongly_Agree')], name='strongly_agree')

return {
    'data': [trace1, trace2, trace3, trace4, trace5, trace6],
    'layout':
    go.Layout(
        title='Pulse Analysis for {}'.format(Group),
        barmode='stack')
}

if name == ‘main’:
app.run_server(debug=True)

Please see the line with the double asterisks. This is where the issue is occurring and I’m unsure of how to resolve it.

I’ll try to figure out what pulse.csv should be, but it would be helpful if code blocks are used.

See 📣 How to provide your sample code in right format

1 Like

So the following code renders the page correctly but an error is thrown since I probably don’t have a correct pulse.csv file.

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

df = pd.read_csv('pulse.csv')

group_options = df['Group'].unique()
year_options = df['Year'].unique()

app = dash.Dash(__name__)

app.layout = html.Div([
    html.H2('Pulse Analysis Report'),
    html.Div(
        [
            dcc.Dropdown(
                id='Group',
                options=[{'label': i,
                          'value': i
                          } for i in group_options],
                multi=True,
                value='All Groups'),
            dcc.Dropdown(
                id="Year",
                options=[{
                    'label': i,
                    'value': i,
                } for i in year_options],
                multi=True,
                value='All Years'),
        ],

        style={'width': '25%',
               'display': 'inline-block'}
    ),

    dcc.Graph(id='funnel-graph'),
])


@app.callback(
    output=Output('funnel-graph', 'figure'),
    inputs=[Input('Group', 'value')])
def update_graph(Group):
    if Group == 'All Groups':
        df_plot = df.copy()
    else:
        df_plot = df[df['Group'] == Group]

    pv = pd.pivot_table(
        df_plot,
        index=['Question'],
        columns=["Response"],
        values=['Percentage'],
        aggfunc='mean',
        fill_value=0)

    trace1 = go.Bar(
        x=pv.index, y=pv[('Percentage', 'Skipped')], name='skipped')
    trace2 = go.Bar(
        x=pv.index, y=pv[('Percentage', 'Neutral')], name='neutral')
    trace3 = go.Bar(x=pv.index, y=pv[('Percentage', 'Agree')], name='agree')
    trace4 = go.Bar(
        x=pv.index, y=pv[('Percentage', 'Disagree')], name='disagree')
    trace5 = go.Bar(x=pv.index, y=pv[(
        'Percentage', 'Strongly_Disagree')], name=' strongly_disagree')
    trace6 = go.Bar(
        x=pv.index, y=pv[('Percentage', 'Strongly_Agree')], name='strongly_agree')

    return {
        'data': [trace1, trace2, trace3, trace4, trace5, trace6],
        'layout':
            go.Layout(
                title='Pulse Analysis for {}'.format(Group),
                barmode='stack')
    }


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

The problem you were having was related to the html.Div following your html.H2 block. Notice how the 2 dropdowns are contained in one array. You had them in 2, and with out specifying the keys, dash assumes the first argument is children. So in your case, dash didn’t know how to interpret your second argument.

So, the following snippet is also correct:

app.layout = html.Div([
    html.H2('Pulse Analysis Report'),
    html.Div(
       children= [
            dcc.Dropdown(
                id='Group',
                options=[{'label': i,
                          'value': i
                          } for i in group_options],
                multi=True,
                value='All Groups'),
            dcc.Dropdown(
                id="Year",
                options=[{
                    'label': i,
                    'value': i,
                } for i in year_options],
                multi=True,
                value='All Years'),
        ],

        style={'width': '25%',
               'display': 'inline-block'}
    ),

    dcc.Graph(id='funnel-graph'),
])
1 Like

Thank you! I am still learning so much about both Plotly and Dash and this forum is a great resource to have.