Hello All,
I have a multi pages dash app, on which I read multi dataframe. Some pages use the same dataframe.
I want to know how store all dataframe and how share, access it from other pages.
My app tree is:
myapp
- app.py
- sidebar.py
- pages
- first_page
- second_page
etc
The idea is open all dataframe from db in app.py or other and in pages use the store.
I read a lot of different solution, but they use all a button to read the data and I don 't want.
app.py:
from dash import Dash
from sidebar import serve_layoutapp = Dash(name, use_pages=True)
server = app.server
app.layout = serve_layout
if name == ‘main’:
app.run_server(host=‘0.0.0.0’, port=‘8888’, debug=True)
sidebar.py:
def serve_layout():
layout = dbc.Container([
dbc.Row([
dbc.Col([offcanvas], class_name=“col-menu”),
],class_name=“row-menu”),
dbc.Row([
dbc.Col([dash.page_container])
]),
], class_name=“dbc”)return layout
and a random page:
import dash
from dash import callback, Input, Output
from dash import html, dcc, dash_table
import pandas as pd
import dash_bootstrap_components as dbc
from get_rawdata_db.get_rawdata_db import read_from_db
import configparsertry:
config = configparser.ConfigParser()
config.read(‘settings.ini’)
except Exception as e:
print(e)host = config[‘db’][‘host’]
db = config[‘db’][‘db’]
usr = config[‘db’][‘usr’] if config[‘db’][‘usr’] else os.getenv(‘TUXPRDSQL_DB_USR’)
psw = config[‘db’][‘psw’] if config[‘db’][‘psw’] else os.getenv(‘TUXPRDSQL_DB_PSW’)
table = config[‘db’][‘table’]dash.register_page(name, path=‘/report’, order=0)
title = html.Div(html.H4(“Report”),className=“title-menu”)
report = read_from_db(host, db, usr, psw, table)
table = html.Div([
#dcc.Interval(‘refresh’, interval = 30000, n_intervals = 0),
dash_table.DataTable(
id=‘table-home’,
columns=[{“name”: i, “id”: i, “hideable”: True} for i in report.columns],
data=report.to_dict(‘records’),
sort_action=‘native’,
sort_mode=“multi”,
hidden_columns=[‘fqdn’, ‘product_name’,‘cms_product_id’, ‘ip’, ‘powerstate’],
filter_action=‘native’,
filter_options={‘case’:‘insensitive’},
page_action=“native”,
page_size= 30,
export_format=“xlsx”,
#virtualization=True,
#style_table={‘minWidth’: ‘100%’},
#fixed_columns={‘headers’: True, ‘data’: 2},
)],className=“dbc dbc-row-selectable”)layout = html.Div([
dbc.Row(title),
dbc.Row(table)])@callback(
dash.dependencies.Output(‘table-home’,‘data’),
[dash.dependencies.Input(‘refresh’, ‘n_intervals’)])def updateTable(n):
report = read_from_db(host, db, usr, psw, table)return report.to_dict('records')
Someone can help me ?