✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Can't access the global DF

Hi,
I have been trying to create two line graphs based on the inputs from dropdowns and daterange. The data is fetched through APIs. In the first method for the first graph i am getting all the data wherein the global df is updated. I am trying to access the df in the second method for the second graph. It does not look like i am able to.

import json
import pandas as pd
global df
app = dash.Dash()
app.layout = html.Div([
dcc.Graph(id='graph1', figure={
        'data':[
            {'x':[1,1], 'y':[1,1]}
        ]
    }),
    dcc.Graph(id = 'graph2', figure={
        'data':[
            {'x':[1,1], 'y':[1,1]}
        ]
    })
@app.callback(Output('graph1','figure'),
[Input ('AAA','figure'])
def update_graph1 (input params): 
'''Data retrieval happens here'''
df['A'] = series1
df['B'] = series2

return fig1

@app.callback(Output('graph2','figure'),
[Input ('BBB','figure'])
def update_graph2 (input params): 
'''the df['A'] and df['B'] can not be accessed here - I don't want to repeat the data retrieval
here as it would be computationally expensive'''


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

Any guidance would be highly appreciated
Thanks

If I understand your problem correct you now have all your data collecting/analysis inside the “graph1” and want to grab some of the data under “graph1” so you can use it in “graph2”.
That sounds like a bad setup, so let’s fix that.

I suggest that you place a button or something that, when you press it, retrieves the data you need and perform the analysis you need. You could have the button return the data either into a dcc.Store component, or as a JSON format into a hidden Div (should be able to find by searching the forum how to do this).

Then you could have the dcc.Store or JSON formatted data in the hidden div be inputs to your graph callbacks.

Hi Blaceus,
Thank you very much for your response.
The data analysis/collection happens inside the graph1. The graph2 will be using the same data as used for graph1 (it may sound stupid but graph-1 gives the resampled data on monthly basis. whereas the graph2 gives the variation rate).
I have implemented the solution by creating a different method for analysis/collection which is being called twice for graph1 and graph2. The solution works however, it looks like it will be computationally expensive.