Hello,
I wanna make some dashboard.
If a user connected my server, client can see some graphs.
This graphs is made of dataframe.
User can click some button to redraw graphs.
If data is fetched from DB every button clicks, It is slow.
I don’t know best way for fetching DB one time.
I just use global variables.
So I created code like below.
main.py
import dash
import dash_bootstrap_components as dbc
from app import serve_layout
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRA],
meta_tags=[{'name': 'viewport', 'content': 'width=device-width, initial-scale=1.0'}])
app.layout = serve_layout
if __name__ == "__main__":
app.run_server(debug=True)
app.py
import datetime
import pandas as pd
import dash_bootstrap_components as dbc
from dash import html
from dash import Input, Ouput, callback
def serve_lauyout():
global df
df = pd.read_csv("some.csv") # this is example. It can be replaced fetch from DB
layout = html.Div(
children=[
html.P(f"{df['COLUMN'].iloc[0]}"),
html.P(f"{datetime.datetime.now()}"),
html.Div(id='page'),
dbc.Button('Press', id='press', n_clicks=0)
]
)
return layout
@callback(
Output('page', 'children'),
Input('press', 'n_clicks')
)
def button_pressed(btn):
global df
return html.P(f"{df['COLUMN'].iloc[0]}, {datetime.datetime.now()}")
some.csv
COLUMN,DATA
A,B
This code works fine. But if other user change df variable, my view is also changed.
I searched some documents and finds dcc.Store. But It is only used some events. It seems that it cannnot save data itself.
Help me please.