New Code showing old dashboard on http://127.0.0.1:8050/

Hi All,

I am new to Python and learning it to create Interactive dashboards using Plotly and dash. I was going through a tutorial and created a sample choropleth dash board and with that learning, I am creating a new choropleth dash board using my own data. I ran my new code in Jupyter notebook and it ran without errors but when I open the URL http://127.0.0.1:8050/ it is opening the old dash board instead of creating a new dashboard which I want to create using new data. I tried to kill using Ctrl + C started again and also rebooted my laptop but it is still showing the old dashboard instead of creating a new dashboard. Am I missing anything in the code or do I need to do anything to give me new dashboard. Also, gone through all the discussions, didn’t find any similar to my issue.

Any information is highly appreciated.

Thanks
Sri M

Hey @smadderla,
Can you please share us the screenshot of Dashboard of what you are looking?
And what do you mean by old Dashboard? Does it shows graph from previous code? How much do you change in new code lines?
Can you please elaborate or show us the previous and new code lines?

Hi @matsujju,

Thank you.

Yes, it shows old graph from previous code. In the new code, I am using new data frame, and changed the required labels.

Old code:
import pandas as pd
import plotly.express as px # (version 4.7.0)
import plotly.graph_objects as go

import dash # (version 1.12.0) pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(name)

------------------------------------------------------------------------------

Import and clean data (importing csv into pandas)

df = pd.read_csv(“intro_bees.csv”)

df = df.groupby([‘State’, ‘ANSI’, ‘Affected by’, ‘Year’, ‘state_code’])[[‘Pct of Colonies Impacted’]].mean()
df.reset_index(inplace=True)
print(df[:5])

------------------------------------------------------------------------------

App layout

app.layout = html.Div([

html.H1("Web Application Dashboards with Dash", style={'text-align': 'center'}),

dcc.Dropdown(id="slct_year",
             options=[
                 {"label": "2015", "value": 2015},
                 {"label": "2016", "value": 2016},
                 {"label": "2017", "value": 2017},
                 {"label": "2018", "value": 2018}],
             multi=False,
             value=2015,
             style={'width': "30%"}
             ),

html.Div(id='output_container', children=[]),
html.Br(),

dcc.Graph(id='my_bee_map', figure={})

])

------------------------------------------------------------------------------

Connect the Plotly graphs with Dash Components

@app.callback(
[Output(component_id=‘output_container’, component_property=‘children’),
Output(component_id=‘my_bee_map’, component_property=‘figure’)],
[Input(component_id=‘slct_year’, component_property=‘value’)]
)
def update_graph(option_slctd):
print(option_slctd)
print(type(option_slctd))

container = "The year chosen by user was: {}".format(option_slctd)

dff = df.copy()
dff = dff[dff["Year"] == option_slctd]
dff = dff[dff["Affected by"] == "Varroa_mites"]

# Plotly Express
fig = px.choropleth(
    data_frame=dff,
    locationmode='USA-states',
    locations='state_code',
    scope="usa",
    color='Pct of Colonies Impacted',
    hover_data=['State', 'Pct of Colonies Impacted'],
    color_continuous_scale=px.colors.sequential.YlOrRd,
    labels={'Pct of Colonies Impacted': '% of Bee Colonies'},
    template='plotly_dark'
)

app.scripts.config.serve_locally = True
app.css.config.serve_locally = True
# Plotly Graph Objects (GO)
# fig = go.Figure(
#     data=[go.Choropleth(
#         locationmode='USA-states',
#         locations=dff['state_code'],
#         z=dff["Pct of Colonies Impacted"].astype(float),
#         colorscale='Reds',
#     )]
# )
#
# fig.update_layout(
#     title_text="Bees Affected by Mites in the USA",
#     title_xanchor="center",
#     title_font=dict(size=24),
#     title_x=0.5,
#     geo=dict(scope='usa'),

return container, fig

------------------------------------------------------------------------------

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

New Code:
import pandas as pd
import plotly.express as px # (version 4.7.0)
import plotly.graph_objects as go

import dash # (version 1.12.0) pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(name)

------------------------------------------------------------------------------

Import and clean data (importing csv into pandas)

EPMdf = pd.read_csv(“EPM_Flash.csv”,encoding=‘latin-1’)

EPMdf = EPMdf.groupby([‘Vertical’,‘state_code’,‘State’])[[‘Net Revenue’]].sum()
EPMdf.reset_index(inplace=True)
print(EPMdf[:5])

#EPMdf1=EPMdf.groupby([‘state_code’,‘State’])[[‘Net Revenue’]].sum()
#EPMdf1.reset_index(inplace=True)
#print(EPMdf1[:5])

------------------------------------------------------------------------------

App layout

app.layout = html.Div([

html.H1("EPM Net Revenue Dashborad for 2018", style={'text-align': 'center'}),

dcc.Dropdown(id="slct_vertical",
             options=[
                 {"label": "BFS", "value": 'BFS'},
                 {"label": "CMT", "value": 'CMT'},
                 {"label": "HC", "value": 'HC'},
                 {"label": "INS", "value": "INS"},
                 {"label": "LS", "value": "LS"},
                 {"label": "MLEU", "value": "MLEU"},
                 {"label": "RCG", "value": "RCG"},
                 {"label": "RHCG", "value": "RHCG"},
                 {"label": "T&H", "value": "T&H"}],
             multi=False,
             value="RCG",
             style={'width': "40%"}
             ),

html.Div(id='output_container', children=[]),
html.Br(),

dcc.Graph(id='my_net_rev_map', figure={})

])

------------------------------------------------------------------------------

Connect the Plotly graphs with Dash Components

@app.callback(
[Output(component_id=‘output_container’, component_property=‘children’),
Output(component_id=‘my_net_rev_map’, component_property=‘figure’)],
[Input(component_id=‘slct_vertical’, component_property=‘value’)]
)
def update_graph(option_slctd):
print(option_slctd)
print(type(option_slctd))

container = "The vertical chosen by user was: {}".format(option_slctd)

dff = EPMdf.copy()
dff = dff[dff["Vertical"] == option_slctd]


# Plotly Express
fig = px.choropleth(
    data_frame=dff,
    locationmode='USA-states',
    locations='state_code',
    scope="usa",
    color='Net Revenue',
    hover_data=['State', 'Net Revenue'],
    color_continuous_scale=px.colors.sequential.YlOrRd,
    labels={'Net Revenue': 'Total Net Revenue'},
    template='plotly_dark'
)

app.scripts.config.serve_locally = True
app.css.config.serve_locally = True

# Plotly Graph Objects (GO)
# fig = go.Figure(
#     data=[go.Choropleth(
#         locationmode='USA-states',
#         locations=dff['state_code'],
#         z=dff["Pct of Colonies Impacted"].astype(float),
#         colorscale='Reds',
#     )]
# )
#
# fig.update_layout(
#     title_text="Bees Affected by Mites in the USA",
#     title_xanchor="center",
#     title_font=dict(size=24),
#     title_x=0.5,
#     geo=dict(scope='usa'),
# )

return container, fig

------------------------------------------------------------------------------

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

Thank you.
Sri M.

I think the problem is with update_graph function though I can’t see with proper indentation here. Can you put the sections of code in proper format as in inside ``` so that i can see what you are doing with function?

@matsujju Thank you. Here is the new code with proper format.

Data Frame:
Vertical state_code State Net Revenue
0 BFS AL Alabama 190713.085708
1 BFS AZ Arizona 50347.477240
2 BFS CA California 74854.200351
3 BFS CO Colorado 30979.590020
4 BFS CT Connecticut 211093.677005

import pandas as pd
import plotly.express as px  # (version 4.7.0)
import plotly.graph_objects as go

import dash  # (version 1.12.0) pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

# ------------------------------------------------------------------------------
# Import and clean data (importing csv into pandas)
EPMdf = pd.read_csv("EPM_Flash.csv",encoding='latin-1')

EPMdf = EPMdf.groupby(['Vertical','state_code','State'])[['Net Revenue']].sum()
EPMdf.reset_index(inplace=True)
print(EPMdf[:5])

# ------------------------------------------------------------------------------
# App layout
app.layout = html.Div([

    html.H1("EPM Net Revenue Dashborad for 2018", style={'text-align': 'center'}),

    dcc.Dropdown(id="slct_vertical",
                 options=[
                     {"label": "BFS", "value": "BFS"},
                     {"label": "CMT", "value": "CMT"},
                     {"label": "HC", "value": "HC"},
                     {"label": "INS", "value": "INS"},
                     {"label": "LS", "value": "LS"},
                     {"label": "MLEU", "value": "MLEU"},
                     {"label": "RCG", "value": "RCG"},
                     {"label": "RHCG", "value": "RHCG"},
                     {"label": "T&H", "value": "T&H"}],
                 multi=False,
                 value="RCG",
                 style={'width': "40%"}
                 ),

    html.Div(id='output_container', children=[]),
    html.Br(),

    dcc.Graph(id='my_net_rev_map', figure={})

])


# ------------------------------------------------------------------------------
# Connect the Plotly graphs with Dash Components
@app.callback(
    [Output(component_id='output_container', component_property='children'),
     Output(component_id='my_net_rev_map', component_property='figure')],
    [Input(component_id='slct_vertical', component_property='value')]
)
def update_graph(option_slctd):
    print(option_slctd)
    print(type(option_slctd))

    container = "The vertical chosen by user was: {}".format(option_slctd)

    dff = EPMdf.copy()
    dff = dff[dff["Vertical"] == option_slctd]
   

    # Plotly Express
    fig = px.choropleth(
        data_frame=dff,
        locationmode='USA-states',
        locations='state_code',
        scope="usa",
        color='Net Revenue',
        hover_data=['State', 'Net Revenue'],
        color_continuous_scale=px.colors.sequential.YlOrRd,
        labels={'Net Revenue': 'Total Net Revenue'},
        template='plotly_dark'
    )
    
    app.scripts.config.serve_locally = True
    app.css.config.serve_locally = True



    return container, fig


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

@matsujju Thank you.

After posting the code. I rebooted my laptop again and ran the code and it started working. Thanks a lot for your time. Is there anything, I need to add to the code so that the old page is terminated when I run new code instead of rebooting the laptop?

Thanks
Sri M.

As I looked through your code, I found out that you are using multiple Outputs in single call backs which is not a good thing to do…I will advise you to seperate this output to different callbacks. I think the issue was due to this only and it will certainly arise in future also.

@matsujju Thank you. I will work on your suggestions.

Just to clarify one of the posts… there is no problem with having multiple outputs in a call back, and it can even improve performance in some cases.

See the “Dash App with Multiple Outputs” example here: https://dash.plotly.com/basic-callbacks

@AnnMarieW Thank you.