Dash callback fires before initial dataframes load

Hi there, I’m having an issue using a for loop to load initial dataframes that are used in a callback whose output is a graph. The code looks something like:

from dash import Dash
import glob
import pandas as pd

app = Dash(__name__)

datapath = '~/user/project/data/'
dataframes = {}
for file in glob.glob(datapath + '*.csv'):
    dataframes[file] = pd.read_csv(file)

static_data = 'a random string'

@app.callback(Output('plot', 'figure'), Input('dropdown', 'value'))
def update_graph(dropdown_value):
    df = dataframes[dropdown_value]

    #rest of the graph creation

It seems (based on print statements) that everything in the code preceding the callback, including the static_data definition, except the for loop that loads my data runs before the callback fires. This results in dataframes being an empty dict when attempting to define df. Any ideas on why this would be the case?

You should check to make sure that the loop loading dataframes is working as expected. For example, maybe add debugging output to show for each file loaded the file name and the length of the loaded dataframe.

2 Likes

I tried that - if I put a print statement inside the dataframe-loading for loop, it never executes

That suggests that glob.glob(datapath + '*.csv') is returning an empty list - certainly the first thing to check.