# Dash plotly\_error #callbacks

**URL:** <https://community.plotly.com/t/dash-plotly-error-callbacks/39342>\
**Category:** Dash Python\
**Created:** [May 13, 2020, 10:05am UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342 "2020-05-13T10:05:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenz78](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Lorenz78](https://community.plotly.com/u/Lorenz78)\
**Post date:** [May 13, 2020, 10:05am UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/1 "2020-05-13T10:05:21Z")

</div>

Hi everyone, I got this error running the scrpt below!Could anyone help me? many thx

**ERROR**  
 ![Error_RideID](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/8/8e2aad6033b5af76cdc4177bfeeff686c2bf843c.jpeg)

**DATAFRAME** (df3 and grouped df5)  
[https://drive.google.com/drive/folders/1ItPsuy1OFRxLvGpixJeGoaBVisQ9WMNL](https://drive.google.com/drive/folders/1ItPsuy1OFRxLvGpixJeGoaBVisQ9WMNL)

```auto
import dash_core_components as dcc
import dash
import dash_html_components as html
import dash_table
import pandas
import plotly
import plotly.express as px
from dash.dependencies import Input, Output

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash( __name__ , external_stylesheets=external_stylesheets)

csv = pandas.read_csv('C:\\Users\\acer\\Desktop\\Lorenzo\\GTT\\convalide\\esportazione716_3.csv', sep=';', low_memory = False)
csv1 = pandas.read_csv('C:\\Users\\acer\\Desktop\\Lorenzo\\GTT\\convalide\\dash3.csv', sep=',', low_memory = False, encoding='latin-1')

df1 = csv1[['RIDEID' , 'WK' , 'TARIFFFAMILYID' , 'COUNT(*)']]
df2=pandas.pivot_table(df1, index = ['RIDEID' , 'WK'] , columns = 'TARIFFFAMILYID' , values = 'COUNT(*)' , aggfunc = sum, fill_value=0)
df3=df2.reset_index().rename_axis(None, axis=1)
df4 = df3.rename(columns = {10: 'a10', 12: 'a12' , 20: 'a20', 40: 'a40', 60: 'a60'})
df5 = df4.groupby('RIDEID', as_index=False)[['a10','a12','a20','a40','a60']].sum()

print(df5)

app.layout = html.Div([
    html.H1(children='Hello World',
            style = {
                'textAlign' : 'center',
                 'color' : '#ff0000'
            }
    ),

    html.Div('prova'),
html.Div([
    dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i, "deletable": False, "selectable": False} for i in df5.columns],
        data=df5.to_dict('records'),
        page_size=10,
        editable=False,
        filter_action="native",
        sort_action="native",
        sort_mode="multi",
        row_selectable="multi",
        row_deletable=False,
        selected_rows=[],
        page_action="native",
        page_current=0,
        # page_action='none',
        # style_cell={
        # 'whiteSpace': 'normal'
        # },
        # fixed_rows={ 'headers': True, 'data': 0 },
        # virtualization=False,
    ),
], className='row'),

    html.Div([
        html.Div([
            dcc.Dropdown(id='linedropdown',
                options=[
                         {'label': 'abb10', 'value': 'a10'},
                         {'label': 'abb12', 'value': 'a12'},
                         {'label': 'abb20', 'value': 'a20'},
                         {'label': 'abb40', 'value': 'a40'},
                         {'label': 'abb60', 'value': 'a60'}
                ],
                value='a10',
                multi=False,
                clearable=False
            ),
        ],className='six columns'),

        html.Div([
            dcc.Dropdown(id='piedropdown',
                options=[
                     {'label': 'REASONCODE1', 'value': 'REASONCODE'},
                     {'label': 'DEVICECLASSID1', 'value': 'DEVICECLASSID'}
                ],
                value='DEVICECLASSID',
                multi=False,
                clearable=False
            ),
        ],className='six columns'),

    ],className='row'),

    html.Div([
        html.Div([
            dcc.Graph(id='linechart'),
        ],className='six columns'),

        html.Div([
            dcc.Graph(id='piechart'),
        ],className='six columns'),

    ],className='row'),

    html.H2(children='Arrivederci'),
])

@app.callback(
    Output('linechart', 'figure'),   
    [Input('table', 'selected_rows'),
     Input('linedropdown', 'value')]
     
)
def update_data(chosen_rows, linedropval):
    if len(chosen_rows)==0:
        df_filterd1 = df5[df5['RIDEID'].isin([1, 2])]
    else:
        print(chosen_rows)
        df_filterd1 = df5[df5.index.isin(chosen_rows)]

    #extract list of chosen countries
    df_filterd1 = df5[df5.index.isin(chosen_rows)]
    list_chosen_rides=df_filterd1['RIDEID'].tolist()
    #filter original df according to chosen countries
    #because original df has all the complete dates
    df_line = df4[df4['RIDEID'].isin(list_chosen_rides)]

    print(df_line)

    line_chart = px.line(
            data_frame=df_line,
            x='WK',
            y=linedropval,
            color='RIDEID',
            labels={'RIDEID1':'RIDEID', 'WK':'WK'}
            )
    line_chart.update_layout(uirevision='foo')

    return (line_chart)

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

```

---

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [May 14, 2020, 1:18pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/2 "2020-05-14T13:18:56Z")

</div>

> [@Lorenz78](#):
>
> ```auto
> csv = pandas.read_csv('C:\\Users\\acer\\Desktop\\Lorenzo\\GTT\\convalide\\esportazione716_3.csv', sep=';', low_memory = False)
> csv1 = pandas.read_csv('C:\\Users\\acer\\Desktop\\Lorenzo\\GTT\\convalide\\dash3.csv', sep=',', low_memory = False, encoding='latin-1')
> 
> ```

Hi @Lorenz78  
Can you share dash3.csv data with us please, so we can reproduce your code.

Inside your `def update_data()` function, you are using df4 together with df5:

```auto
    df_line = df4[df4['RIDEID'].isin(list_chosen_rides)]

```

Did you mean to use df5 here?

---

<div class="post-metadata">

**Author:** ![Lorenz78](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Lorenz78](https://community.plotly.com/u/Lorenz78)\
**Post date:** [May 14, 2020, 3:54pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/3 "2020-05-14T15:54:24Z")

</div>

Hi @adamschroeder , thx for your answer, I need to use df4 not df5.

Here below the links to **dataframes**

> **[df3.csv](https://drive.google.com/file/d/1w4nKQzO6XDpaMcbEICTNxvLy7Slni33L/view?usp=sharing)**
>
> Google Drive file.

> **[df4.csv](https://drive.google.com/file/d/1e15KYHZgwtSfHPVtens7pQPsjeFXFvc6/view?usp=sharing)**
>
> Google Drive file.

> **[df5.csv](https://drive.google.com/file/d/1yOefmX3K9TEeC0xyMezB4uaVV-yyXTmm/view?usp=sharing)**
>
> Google Drive file.

Here below the **error**

![Error_RideID](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/2/24271da46cc657e34ddc6d8bc83d13ed9f2fbfd0.jpeg)

Below the **script**.

I changed a little bit respect the previous one just adding another graph, but it gives always the same kind of error. When i open the webapp i don’t see nothing as “default” in the figure. Once i choose from table, the chosen lines update both graphs

You can copy and paste the code.

Many thx  
Lorenzo

```auto
import dash_core_components as dcc
import dash
import dash_html_components as html
import dash_table
import pandas
import plotly
import plotly.express as px
from dash.dependencies import Input, Output

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash( __name__ , external_stylesheets=external_stylesheets)

csv = pandas.read_csv('C:\\Users\\acer\\Desktop\\Lorenzo\\GTT\\convalide\\esportazione716_3.csv', sep=';', low_memory = False)
csv1 = pandas.read_csv('C:\\Users\\acer\\Desktop\\Lorenzo\\GTT\\convalide\\dash3.csv', sep=',', low_memory = False, encoding='latin-1')

df1 = csv1[['RIDEID' , 'WK' , 'TARIFFFAMILYID' , 'COUNT(*)']]
df2=pandas.pivot_table(df1, index = ['RIDEID' , 'WK'] , columns = 'TARIFFFAMILYID' , values = 'COUNT(*)' , aggfunc = sum, fill_value=0)
df3=df2.reset_index().rename_axis(None, axis=1)
df4 = df3.rename(columns = {10: 'a10', 12: 'a12' , 20: 'a20', 40: 'a40', 60: 'a60'})
df5 = df4.groupby('RIDEID', as_index=False)[['a10','a12','a20','a40','a60']].sum()

app.layout = html.Div([
    html.H1(children='Hello World',
            style = {
                'textAlign' : 'center',
                 'color' : '#ff0000'
            }
    ),

    html.Div('prova'),
html.Div([
html.Div([
    dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i, "deletable": False, "selectable": False} for i in df5.columns],
        data=df5.to_dict('records'),
        page_size=10,
        editable=False,
        filter_action="native",
        sort_action="native",
        sort_mode="multi",
        row_selectable="multi",
        row_deletable=False,
        selected_rows=[],
        page_action="native",
        page_current=0,
    ),
    ], className='six columns')
], className='row'),
    html.Div([
        html.Div([
            dcc.Dropdown(id='linedropdown',
                options=[
                         {'label': 'abb10', 'value': 'a10'},
                         {'label': 'abb12', 'value': 'a12'},
                         {'label': 'abb20', 'value': 'a20'},
                         {'label': 'abb40', 'value': 'a40'},
                         {'label': 'abb60', 'value': 'a60'}
                ],
                value='a10',
                multi=False,
                clearable=False
            ),
        ],className='six columns'),

        html.Div([
            dcc.Dropdown(id='piedropdown',
                options=[
                    {'label': 'abb10', 'value': 'a10'},
                    {'label': 'abb12', 'value': 'a12'},
                    {'label': 'abb20', 'value': 'a20'},
                    {'label': 'abb40', 'value': 'a40'},
                    {'label': 'abb60', 'value': 'a60'}
                ],
                value='a10',
                multi=False,
                clearable=False
            ),
        ],className='six columns'),
    ],className='row'),

    html.Div([
        html.Div([
            dcc.Graph(id='linechart'),
        ],className='six columns'),

        html.Div([
            dcc.Graph(id='piechart'),
        ],className='six columns'),
    ],className='row'),

    html.H2(children='Arrivederci'),
])

@app.callback(
    [Output('piechart', 'figure'),
     Output('linechart', 'figure')],
    [Input('table', 'selected_rows'),
     Input('piedropdown', 'value'),
     Input('linedropdown', 'value')]
)
def update_data(chosen_rows, piedropval, linedropval):
    if len(chosen_rows)==0:
        df_filterd = df5[df5['RIDEID'].isin([1, 2])]
    else:
        print(chosen_rows)
        df_filterd = df5[df5.index.isin(chosen_rows)]

    pie_chart=px.pie(
           data_frame=df_filterd,
            names='RIDEID',
            values=piedropval,
            hole=.3,
            labels={'RIDEID':'RIDEID'}
            )

    #extract list of chosen rides
    df_filterd = df5[df5.index.isin(chosen_rows)]
    list_chosen_rides=df_filterd['RIDEID'].tolist()
    #filter original df according to chosen rides, because original df has all the dates
    df_line = df4[df4['RIDEID'].isin(list_chosen_rides)]

    line_chart = px.line(
            data_frame=df_line,
            x='WK',
            y=linedropval,
            color='RIDEID',
            labels={'RIDEID1':'RIDEID', 'WK':'WK'}
            )
    line_chart.update_layout(uirevision='foo')

    return (pie_chart,line_chart)

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

```

---

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [May 14, 2020, 4:51pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/4 "2020-05-14T16:51:06Z")

</div>

> [@Lorenz78](#):
>
> ```auto
> def update_data(chosen_rows, piedropval, linedropval):
> if len(chosen_rows)==0:
> df_filterd = df5[df5['RIDEID'].isin([1, 2])]
> 
> ```

I think it’s just a problem with the data filtering. Try changes the [1,2] from integers to strings like this:

```auto
df_filterd = df5[df5['RIDEID'].isin(["1","2"])]

```

these numbers are set as strings inside your df5.

One thing you can do to debug these errors is try to print your df more often. For example:

```auto
def update_data(chosen_rows, piedropval, linedropval):
    if len(chosen_rows)==0:
        df_filterd = df5[df5['RIDEID'].isin([1, 2])]
        print(df_filterd[:5])

```

---

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [May 14, 2020, 6:34pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/5 "2020-05-14T18:34:32Z")

</div>

Hi Lorenzo,  
Did you see my post on the forum. The problem is that you are trying to plot an empty dataframe. DF5 doesn’t have anything inside because of how you filtered it.

Try to add more prints inside your function after every df that you filter, to make sure you are filtering correctly.

Sincerely,  
Adam

---

<div class="post-metadata">

**Author:** ![Lorenz78](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Lorenz78](https://community.plotly.com/u/Lorenz78)\
**Post date:** [May 14, 2020, 7:09pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/6 "2020-05-14T19:09:06Z")

</div>

Hi adam! thx for your answer,

probably i cannot understand the problem… at the beginning i have no selection in table, so i’m in case

```
if **len(chosen_rows)==0**:
    df_filterd = df5[df5['RIDEID'].isin([1, 2, 4])]
    print(df_filterd[:5])

```

I’d expect to have neither pie neither line chart, being my df\_filterd empy. this should be ok.

I don’t understand why i get an error for a “normal” case, and how to solve it…

bye  
Lorenzo

---

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [May 14, 2020, 8:49pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/7 "2020-05-14T20:49:24Z")

</div>

I think your error is inside the px.line():

```auto
 line_chart = px.line(
            data_frame=df_line,
            x='WK',
            y=linedropval,
            color='RIDEID',
            labels={'RIDEID1':'RIDEID', 'WK':'WK'}
            )

```

You are asking Dash to plot a line chart with columns that don’t exist. That’s why you’re getting the error. “RIDEID” and “WK” don’t exist because you didn’t filter the df correctly.

Try doing this:

```auto
  line_chart = px.line(
            data_frame=df_line,
            # x='WK',
            # y=linedropval,
            # color='RIDEID',
            # labels={'RIDEID1':'RIDEID', 'WK':'WK'}
            )

```

Now, you see you don’t get an error, you just get an empty line plot. Make sure you filter your dataframe correctly inside the function.

---

<div class="post-metadata">

**Author:** ![Lorenz78](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Lorenz78](https://community.plotly.com/u/Lorenz78)\
**Post date:** [May 15, 2020, 9:35am UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/8 "2020-05-15T09:35:27Z")

</div>

Ok, in this way i don’t get error, but once I select the rows in table obviously the line chart (left side) doesn’t refresh (see figure)

 ![selection](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/4/4d69ccc2b40f2a15cc9193f8cba6eaececd6f20a.jpeg)

I cannot understand why in case of presence of two graphs my len(chosen row) == 0 condition is “not seen” by dash and so respected neither for line chart, neither for pie chart.

```
if len(chosen_rows)==0:
    df_filterd = df5[df5['RIDEID'].isin([1, 2, 4])]
    print(df_filterd[:5])

```

If I run the script just with pie chart that condition is respected: at first running (so without any selection of row in the table) the web app follows the condition behaving as I chose that RIDEID.

Thx again

Lorenzo

---

<div class="post-metadata">

**Author:** ![adamschroeder](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/adamschroeder/32/17280_2.png) [@adamschroeder](https://community.plotly.com/u/adamschroeder)\
**Post date:** [May 15, 2020, 4:45pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/9 "2020-05-15T16:45:06Z")

</div>

@Lorenz78  
I’m not sure I understand your question. If I use `df_filterd = df5[df5['RIDEID'].isin([1,2,4])` as integers, then, no chart is plotted. But if I use `[...].isin(["1","2","4"])` as strings, then only the pie chart is plotted because you plot it with `df_filtered`. The line chart you are plotting with `df_line` which is not filtered correctly.

---

<div class="post-metadata">

**Author:** ![Lorenz78](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@Lorenz78](https://community.plotly.com/u/Lorenz78)\
**Post date:** [May 15, 2020, 5:21pm UTC](https://community.plotly.com/t/dash-plotly-error-callbacks/39342/10 "2020-05-15T17:21:08Z")

</div>

So:  
if i put df\_filterd = df5[df5[‘RIDEID’].isin([“1”,“2”,“4”]) so as strings the print(df\_filterd[:5]) **works** (i get table)

![string](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/3/31416bb42ab2c38ba1dd1e3e2c45c69fda43de4b.jpeg)

if i put df\_filterd = df5[df5[‘RIDEID’].isin([1,2,4]) as integers the print(df\_filterd[:5]) **doesn’t work** (i don’t get table).

**In any case in my dash the error is the same.** So i don’t think the problem is there.

In my previous post i meant that if I run the same script but just with piechart return the case if len(chosen\_rows)==0 is correctly managed in web app.

it is showed what i’m expecting (the case with len(chose rows ==0, so df\_filterd = df5[df5[‘RIDEID’].isin([“1”, “2”, “4”])]) and there is no any error …

hope to explain 🙂
