Black Lives Matter. Please consider donating to Black Girls Code today.

Structuring a Multi-Page App with multiple trace figures and for loops

I have a simple multi-page app that works well except when I have to build a multi-trace figure. I have an app.py, a data.py for my dataframe work, layouts.py, and a callbacks.py. My challenge is I cannot put into the callbacks.py any graph that has multiple traces and “call” it from the layouts.py. I can build the figure, add traces, use a for loop if I put the code directly into the layouts.py, but I would prefer to put all graphs into the layouts.py. Here is more detail.

I can get the other two types of graphs to work using the callbacks.py does work. There is the traditional callback method which works well. And for graphs that do not have a callback, I can define them as I normally do like this:

state_graph = go.Figure(
[go.Bar(
x=state_df.state,
y=state_df.sales,
width=.2,
name=“State”
)
],
go.Layout(
xaxis={
‘title’: ‘State’
},
yaxis={
‘title’: ‘Sales’
},
margin=dict(
b=10, #bottom margin 40px
l=40, #left margin 40px
r=40, #right margin 20px
t=40, #top margin 20px
)
)
)

I am able to retrieve the state_graph in a layouts.py. Great.

The challenge is what to do about graphs with multiple traces and code like this:

for i in range(1, st_count+1):
state_line.add_trace(go.Scatter(
x=ptstatemo_df.month,
y=ptstatemo_df.iloc[:,i],
name=headers1[i],
mode = ‘lines’
))
state_line.update_layout(
autosize=True,
margin=dict(
autoexpand=False,
l=40,
r=80,
t=40,
b=40
),
height=250,
plot_bgcolor=‘white’
)

This works well when it is in one py file that has the dataframes, the layout. and the above code in the same py file. But when I put the above code into the callbacks.py, I cannot figure out how to “call” sate_line or use it in the layouts.py. The error I get is:
state_line.add_trace(go.Scatter(
NameError: name ‘state_line’ is not defined

I have tried using a def like this:

def add_traces(app):
state_line= go.Figure()
for i in range(1, st_count+1):
state_line.add_trace(go.Scatter(
x=ptstatemo_df.month,
y=ptstatemo_df.iloc[:,i],…

along with the appropriate code in my layout, but it does not work.

The way I see it, the callback method and the regular way of assigning a variable to the graph allows me to “pass” the graph code back to layouts.py. I do not see how to do this when I have to build a multi-trace figure.

Does anyone know a solution to this? I would love to have all of my figure definitions in my callbacks.py if possible.

Thanks for any help,

James

After much experimentation, I solved it.

In the code below, I did not need the def line. What I did not spot was that the line “state_line= go.Figure()” was all I needed.

def add_traces(app):
state_line= go.Figure()
for i in range(1, st_count+1):
state_line.add_trace(go.Scatter(
x=ptstatemo_df.month,
y=ptstatemo_df.iloc[:,i],…

The solution was:

state_line= go.Figure()
for i in range(1, st_count+1):
state_line.add_trace(go.Scatter(
x=ptstatemo_df.month,
y=ptstatemo_df.iloc[:,i],…

Simple.