✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Callback works and then stops working?

I have a callback which will be working and then just stop updating altogether, not sure why this is happening at all. Below is the code, I have took this out of my main app into a little test environment.

I have tried adding a button and changing the callback however I still end up with issues.

The callback/pie graph will work initially, but then it will just stop working, I could have moved on and just been adding some comments to the code, not touching it, and then it stops working.

#Imports
import pandas as pd
import sqlite3
import pathlib
import dash
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
from plotly import tools
import plotly.graph_objs as go
import plotly.express as px
import base64
import cufflinks as cf
from SQL import *


#Dash - App Layout
#Init Dash App
app = dash.Dash(external_stylesheets=[dbc.themes.LUX])
#app.config['suppress_callback_exceptions'] = True

#Producing Data for Month Sales Breakdown
year = "2020"
month = "February"
stypemonth = get_stype_month_data(year, month)
smpiefig = px.pie(stypemonth.to_dict(), values='Value', names='SaleType')

#Webpage Layout
app.layout = html.Div(
    [
        dbc.Row(dbc.Col(html.P(""))),
        dbc.Row(dbc.Col(dcc.Dropdown(
            id='sales-month-dropdown',
            options=[{'label':'January', 'value':'January'},{'label':'February', 'value':'February'}],
            value='January'
        ))),
        dbc.Row(dbc.Col(dcc.Dropdown(
            id='sales-year-dropdown',
            options=[{'label':'2020/21', 'value':'2020'},{'label':'2021/22', 'value':'2021'}],
            value='2020'
        ))),
        dbc.Col(dcc.Graph(figure=smpiefig,id='sales-stypemonthpie')),
        ]
    )

#This callback sometimes works? Need attention    
@app.callback(
    Output("sales-stypemonthpie","figure"),
    [Input("sales-month-dropdown", "value"),
    Input("sales-year-dropdown", "value")]
)
def update_sales_smpie(selected_month, selected_year):
    newstypemonth = get_stype_month_data(selected_month, selected_year)
    newsmpiefig = px.pie(newstypemonth.to_dict(), values='Value', names='SaleType')
    print(selected_month + selected_year)
    return newsmpiefig 


#Starting the App
if __name__ == '__main__':
    app.run_server(debug=True)

And here is the code get get_stype_month_data() just pulling from a SQL DB, it works fine for just printing the data.

#SQL to get total of each sale type per month

def get_stype_month_data(month, year):

    con = sqlite3.connect(str(DBFILE))

    stypemdata = pd.read_sql_query("SELECT Category as SaleType, SUM(SValue) as Value FROM Figures WHERE Type='Sale' AND Month=? AND TaxYear=? GROUP BY Category", con, params={month, year})

    return stypemdata

Any help would be appreciated!

You haven’t included the get_stype_month_data function, so you example can’t run.

sorry, it was in another file with the rest of my sql stuff, I have added the function in now, although I’m quite confident the issue doesn’t lie with the function.

An update, I have got it to print the dataframe to the output as well, so I can see what is going on a bit more. And what is happening is really weird, before I added the check, it was working for the 2020/21 months and didn’t work for the 2019/20 months.

However, since adding the check, it seems to have swapped around and now it doesn’t work for the 2020/21 months.

When it is printing the dataframe for the 2019/20 months it works fine and prints the values now and updates the graphs, however when it does the 2020/21 months it just produces and empty graph.

Then I just done a save to update the code and page without making any changes to the code, and now it can work for either year, but is unable to load some months data which previously is was able to load?

Is this a bug with pie charts? would i be safe trying a bar?

More of an update, Bar charts don’t work either, expected considering it seems to be not pulling some months for no reason?

Went back to pie and now January 2019/20 is the only month it won’t pull.

However, in chrome f12 I can see I am getting these errors possibly something to do with it?

Failed to load resource: net::ERR_CONNECTION_REFUSED
TypeError: Failed to fetch