Friends, how lost I am in this concept!
Iāll paste the code as short as possible but it will be a little big.
If anyone can help me I would be very pleased. Iām really not understanding this concept.
This is the code where I want to show the content of df_data_generais (My dataframe)
from datetime import date
import plotly.graph_objs as go
import dash
from dash import html, dcc, Input, Output, State, dash_table
import dash_bootstrap_components as dbc
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
from dash_bootstrap_templates import ThemeSwitchAIO
# Importei meu dataframe do arquivo movimento_geral.py
from movimento_geral import df_dados_gerais
# Formatação das tabelas
formatted = {'specifier': ',.2f', 'locale': {'group': '.', 'decimal': ',', }}
# ================================================================== #
from flask import Flask
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
server = Flask(__name__)
app = dash.Dash(__name__, server=server, suppress_callback_exceptions=True,
external_stylesheets=[dbc.themes.BOOTSTRAP, dbc_css])
# ============================Styles================================ #
tab_card = {'height': '100%'}
main_config = {
'hovermode': 'x unified',
'legend': {
'yanchor': 'top',
'y': 0.9,
'xanchor': 'left',
'x': 0.1,
'title': {'text': None},
'font': {'color': 'white'},
'bgcolor': 'rgba(0,0,0,0.0)'},
'margin': {'l': 0, 'r': 0, 't': 20, 'b': 0}
}
template_theme1 = 'cyborg'
template_theme2 = 'spacelab'
url_theme1 = dbc.themes.CYBORG
url_theme2 = dbc.themes.SPACELAB
# To dict - Para salvar no dcc.store
df_store = df_dados_gerais.to_dict()
app.layout = dbc.Container([
dcc.Store(id='store-data', data=[], storage_type='memory'),
dcc.Interval(
id='interval-component',
interval=10*1000, # in milliseconds
n_intervals=0
),
dbc.Row([
dbc.Col([
dash_table.DataTable(
id='datatable-data',
editable=True,
fixed_rows={'headers': True},
style_cell_conditional=[
{
'if': {'column_id': 'CODIGO'},
'textAlign': 'left'
},
{
'if': {'column_id': 'CODIGO'},
'width': '80px'
},
{
'if': {'column_id': 'ESTOQUE'},
'width': '130px'
},
{
'if': {'column_id': 'UNIDADE'},
'width': '80px'
},
{
'if': {'column_id': 'DESCRICAO'},
'textAlign': 'left', 'width': '230px'
},
{
'if': {'column_id': 'GRUPO'},
'textAlign': 'left'
},
{
'if': {'column_id': 'FAMILIA'},
'textAlign': 'left'
},
{
'if': {'column_id': 'UNIDADE'},
'textAlign': 'left'
},
],
style_data={
'color': 'black',
'backgroundColor': 'white'
},
style_data_conditional=[
{
'if': {'row_index': 'odd'},
'backgroundColor': 'rgb(220, 220, 220)',
}
],
#'backgroundColor': 'white',
style_header={'textAlign': 'center',
'backgroundColor': 'rgb(210, 210, 210)',
'color': 'black',
'fontWeight': 'bold'},
style_table={'height': '1000px', 'overflowY': 'auto'},
filter_action='native',
sort_action="native",
page_size=1000,
)
], sm=12, lg=12)
], className='g-2 my-auto', style={'margin-top': '7px'})
], fluid=True, style={'height': '100vh'})
# ======== Callbacks ========== #
# Na aba TABELAS, conteĆŗdo da tabela.
@app.callback(
Output('datatable-data', 'data'),
Output('datatable-data', 'columns'),
Input('store-data', 'data')
)
def update_table(d):
print('---------------------- DATA update_table n ----------------------')
print(d)
mask = (df_dados_gerais['ANO'] == 2022) & (df_dados_gerais['OPERACAO'] == 'V') & \
(df_dados_gerais['MES'].isin(11)) & (df_dados_gerais['CANCELADO'] != '*')
print('---------------------- update_table MASK ----------------------')
print(mask)
df_pivot = pd.pivot_table(
df_dados_gerais.loc[mask], index=['CODIGO', 'DESCRICAO',
'ESTOQUE', 'GRUPO',
'FAMILIA', 'UNIDADE',],
values='QUANTIDADE',
columns='MES',
aggfunc=sum).reset_index().fillna(0)
df_pivot = df_pivot.rename(
{1: 'JAN', 2: 'FEV', 3: 'MAR', 4: 'ABR', 5: 'MAI', 6: 'JUN',
7: 'JUL', 8: 'AGO', 9: 'SET', 10: 'OUT', 11: 'NOV', 12: 'DEZ'}, axis=1)
cols = []
textCols = ['CODIGO', 'DESCRICAO', 'GRUPO', 'FAMILIA', 'UNIDADE']
for i in df_pivot.columns:
if i not in textCols:
cols.append({"name": str(i),
"id": str(i),
"type": "numeric",
"format": formatted})
else:
cols.append({"name": str(i),
"id": str(i),
"type": "text"})
return df_pivot.to_dict('records'), cols
# Load data from PostgreSQL, you have to change database name, user and password.
@app.callback(Output('store-data', 'data'),
Input('interval-component', 'n_intervals'))
def update_data(n_intervals):
print(f'---------------------- FUNĆĆO update_data {n_intervals} ----------------------')
#print(df_dados_gerais.to_dict())
return df_dados_gerais.to_dict()
if __name__ == '__main__':
app.run_server(debug=True)
What I intend to do is read this data, store it in
dcc.Store(id=āstore-dataā, data=, storage_type=āmemoryā),
And display in the table.
Every 10 seconds update dcc.Store and table