Handle global dataframe

Hi,

I’d like to develop a dashboard for real time handling for multi data.
The code is spitted in few files

#index.py

import os
import base64
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import plotly.subplots as psp
from app import app
from dash.dependencies import Input, Output
from data.prepare_data import df
from pathlib import Path
from itertools import groupby
from operator import itemgetter

app.layout = html.Div([    
    dcc.Interval(id='graph-update', interval=1000),
    html.Div([
        html.Div([
            html.H3("Path", style={'text-align': 'center'}),
            dcc.Graph(id='Path', figure={})],
            className="six columns"),  # see https://codepen.io/chriddyp/pen/bWLwgP

        html.Div([
            html.H3("Angles", style={'text-align': 'center'}),
            dcc.Graph(id='Angles', figure={})],
            className="six columns"),
    ],
        className="row"),
])


@ app.callback([Output('Path', 'figure'),
                Output('Angles', 'figure'),],
               [Input('graph-update', 'n_intervals')])
def update(n):
    print(n)

	update_data()
    #####################################################
    # Path
    #####################################################
    fig1 = psp.make_subplots(rows=1,
                             cols=1,
                             shared_xaxes=True)

    fig1.append_trace({'x': df.Long,
                       'y': df.Lat,
                       'type': 'scatter',
                       'name': 'myPath'},
                      1, 1)    
    fig1.update_scenes(aspectmode="cube")

    #####################################################
    # Angle points
    #####################################################
    fig2 = px.scatter(df,
                      x='angle1',
                      y='angle2',
                      range_x=[0, 360],
                      range_y=[0, 180],
                      )

    fig2.add_layout_image(
        x=0,
        sizex=360,
        y=180,
        sizey=180,
        layer="below",
        xref="x",
        yref="y",
        sizing="stretch",
        source='data:image/png;base64,{}'.format(image1.decode())
    )

    fig2.update_xaxes(tickvals=[0, 30, 60, 90, 120,
                                150, 180, 210, 240, 270, 300, 330, 360])
    fig2.update_yaxes(tickvals=[0, 10, 20, 30, 40, 50, 60, 70,
                                80, 90, 100, 110, 120, 10.3, 140, 150, 160, 170, 180])

    return fig1, fig2


if __name__ == '__main__':
    initialize_df()
    app.run_server()
#prepare_data.py

def initiazlie_df():
     global df
     df=pd.DataFrame({ ... some data...
         })

def update_data():
    df.fillna(method='pad')
... do some othe ractions...

#ReadData.py
read data from network and parse it

On the df.fillna(method='pad') I’m receiving an error local variable 'df' referenced before assignment. However, it was initialized.

  1. how to solve this issue

The use of global variables is discouraged in Dash, i.e. the recommended solution is to restructure the app not to use them.

So what options do i have for real time data update?

Hi @BMWE

I think this server side caching can help: