✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Getting error after edit the datatable value of a graph

Hi, I want to create a web app that users can upload a file and it will show an editable datatable, users can choose which two columns to plot the graph. I add a pandas groupby function to calculate the sum of the data. The web app can run without error but if I edit the value of the column I choose to plot I will get the error below:

Callback error updating datatable-upload-graph.figure
KeyError: 'married_status_id'    (the name of the column I choose to plot)

Below is my code:

import base64
import io
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
from dash.exceptions import PreventUpdate
import plotly.graph_objs as go


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config.suppress_callback_exceptions = True
app.layout = html.Div([
    dcc.Upload(
        id='datatable-upload',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '100%', 'height': '60px', 'lineHeight': '60px',
            'borderWidth': '1px', 'borderStyle': 'dashed',
            'borderRadius': '5px', 'textAlign': 'center', 'margin': '10px'
        },
    ),
    dash_table.DataTable(id='datatable-upload-container'),
    dcc.Dropdown(
        id='data_selector1',
        options=[
            {'label': '', 'value': ''}
        ],
        value=[],
    ),
    dcc.Dropdown(
        id='data_selector2',
        options=[
            {'label': '', 'value': ''}
        ],
        value=[]
    ),
    html.Div(id='output-data-upload', className='datatable'),
    dcc.Graph(id='datatable-upload-graph'),
    dcc.Store(id='local'),
])


def parse_contents(contents, filename):
    content_type, content_string = contents.split(',')
    decoded = base64.b64decode(content_string)
    if 'csv' in filename:
        # Assume that the user uploaded a CSV file
        return pd.read_csv(
            io.StringIO(decoded.decode('utf-8')))
    elif 'xls' in filename:
        # Assume that the user uploaded an excel file
        return pd.read_excel(io.BytesIO(decoded))

@app.callback(Output('output-data-upload', 'children'),
               [Input('datatable-upload', 'contents')],
               [State('datatable-upload', 'filename')])
def update_output(contents, filename):
    if contents is None:
        return []
    df = parse_contents(contents, filename)
    return html.Div([
        dash_table.DataTable(
            id='table',
            style_data={
                'whiteSpace': 'normal',
                'height': 'auto'
            },
            style_table={'overflowX': 'scroll',
                         'maxHeight': '300',
                         'overflowY': 'scroll'},
            style_cell={
                'minWidth': '150px', 'maxWidth': '180px',
                'whiteSpace': 'normal',
                'textAlign': 'left'
            },
            style_header={
                'fontWeight': 'bold',
            },
            fixed_rows={'headers': True, 'data': 0},
            columns=[{"name": i, "id": i, 'deletable': True, 'renamable': True} for i in df.columns],
            data=df.to_dict("records"),
            row_deletable=True,
            filter_action="native",
            sort_action="native",
            sort_mode='multi',
            editable=True,

        )
    ])

@app.callback(Output('data_selector1', 'options'),
              [Input('local', 'data')])
def update_dropdown(rows):
    if rows is None:
        raise PreventUpdate
    df = pd.DataFrame(rows)
    print('updating menus')
    columns = df.columns
    col_labels = [{'label': k, 'value': k} for k in columns]
    return col_labels


@app.callback(Output('data_selector2', 'options'),
              [Input('local', 'data')])
def update_dropdown1(rows):
    if rows is None:
        raise PreventUpdate
    df = pd.DataFrame(rows)
    print('updating menus')
    columns = df.columns
    col_labels = [{'label': k, 'value': k} for k in columns]
    return col_labels



@app.callback(Output('local', 'data'),
              [Input('table', 'data')])
def savedata(data):
    if data is None:
        raise PreventUpdate
    return data

@app.callback(Output('datatable-upload-graph', 'figure'),
              [Input('data_selector1', 'value'),
               Input('data_selector2', 'value')],
              [State('local', 'data')])
def display_graph(value1, value2, rows):
    df = pd.DataFrame(rows)
    if (df.empty or len(df.columns) < 1):
        return {
            'data': [{
                'x': [],
                'y': [],
                'type': 'bar'
            }]
        }
    df1 = df.groupby(value1)
    df2 = df1.sum()
    df3 = df2[value2]
    trace = go.Bar(x=df3.index, y=df3)
    return {
        'data': [trace]
    }


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

Can anyone teach me how to fix the error?