It didn’t work.
I’m thinking it might be something related to how I organized the page layout, or how each screen loads. Below is my main file, where I configured the routes and the layout. Could you take a look to see if everything is correct here?
import warnings
from dash import Input, Output, html, dcc, dash
import dash_bootstrap_components as dbc
import pandas as pd
import locale
from app import app
import connect
from views import cadastro, simulacao, editar_beneficios, editar_funcao, editar_colaboradores, export_caju, profile, \
login
from flask_login import current_user, logout_user
# Defina a localidade para o Brasil (pt_BR)
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
warnings.filterwarnings("ignore")
df_funcao = pd.read_sql_query("SELECT * FROM df_funcao", connect.engine_2)
funcao_aux = df_funcao.to_dict(orient='records')
df_filhos = pd.read_sql_query("SELECT * FROM df_filhos", connect.engine_2)
filhos_colaboradores_aux = df_filhos.to_dict()
df_cadastro = pd.read_sql_query("SELECT * FROM df_cadastro", connect.engine_2)
colaboradores_aux = df_cadastro.to_dict()
df_certificacao = pd.read_sql_query("SELECT * FROM df_certificacao", connect.engine_2)
df_salario = pd.read_sql_query("SELECT * FROM df_salario", connect.engine_2)
df_beneficio = pd.read_sql_query("SELECT * FROM df_beneficio", connect.engine_2)
cadastro_beneficios_aux = df_beneficio.to_dict()
# ========= Layout_sidebar ========= #
layout_sidebar = dbc.Container([
dbc.Row([
dbc.Col([
html.H1("Cadastro de funcionarios", className="text-primary"),
html.Hr(),
dbc.Card([
dbc.CardBody([
dbc.Nav(
[
dbc.NavLink("Cadastro", href="/cadastro", active="exact"),
dbc.NavLink("Simulação", href="/simulacao", active="exact"),
dbc.NavLink('Beneficios', href='/beneficios', active='exact'),
dbc.NavLink('Funções', href='/funcoes', active='exact'),
dbc.NavLink('Colaboradores', href='/colaboradores', active='exact'),
dbc.NavLink('CAJU', href='/exportar_caju', active='exact'),
dbc.NavLink('Configurações', href='/configuracoes', active='exact'),
], vertical=True, pills=True, id='nav_buttons', style={'flex': 1, 'flex-grow': 1}),
dbc.Col([
dcc.Store(id='codigo_revenda', storage_type='session'),
dcc.Store(id='revenda', storage_type='session'),
dcc.Location(id='url_atual')
]),
]),
]),
], id='sidebar_completa', style={'padding-top': '10px'})
])
], fluid=True)
# ========= Layout Configuracoes =========== #
layout_configuracoes = html.Div([
dbc.Row([
dbc.Col([
dbc.Card([
dbc.CardHeader('Configurações'),
dbc.CardBody([
dbc.Row([
dbc.Col([
])
]),
dbc.Row([
dbc.Col([
], style={'margin-top': '10px'})
]),
dbc.Row([
dbc.Col([
])
])
])
])
], style={'padding-right': '10px', 'padding-top': '10px'}, className='g-0')
]),
])
# ========= Layout_total =========== #
navBar = dbc.Navbar(id='navBar',
sticky='top',
color='primary',
class_name='ml-auto',
expand=True)
# Main Layout
app.layout = html.Div([
dcc.Location(id='url', refresh=False),
dcc.Location(id='redirect', refresh=True),
dcc.Store(id='login-status', storage_type='session'),
html.Div(id='user-status-div'),
navBar,
html.Div(id='page-content'),
])
home = html.Div([
dcc.Store(id='store_colaboradores', data=colaboradores_aux),
dcc.Store(id='store_colaboradores_nome'),
dcc.Store(id='store_colaboradores_id'),
dcc.Store(id='store_colaboradores_id_2'),
dcc.Store(id='store_funcoes', data=funcao_aux),
dcc.Store(id='store_funcoes_nome'),
dcc.Store(id='store_cadastro_beneficios', data=cadastro_beneficios_aux),
dbc.Container(children=[
dbc.Row([
dbc.Col([
layout_sidebar
], width=2, className='g-0'),
dbc.Col([
html.Div(id="page-content-2")
], width=10),
]),
], fluid=True)])
@app.callback(Output('navBar', 'children'),
Output('login-status', 'data'),
[Input('url', 'pathname')])
def login_status(url):
''' callback to display login/logout link in the header '''
if current_user.is_authenticated:
navBarContents = [
dbc.DropdownMenu(
nav=True,
in_navbar=True,
label='Usuário',
children=[
dbc.DropdownMenuItem('Profile', href='/profile'),
dbc.DropdownMenuItem(divider=True),
dbc.DropdownMenuItem('Logout', href='/logout'),
],
align_end=True,
style={'margin-left': 'auto', 'margin-right': '20px'}
)
]
return navBarContents, current_user.get_id()
else:
return '', 'loggedout'
# Main router
@app.callback(Output('page-content', 'children'), Output('redirect', 'pathname'),
[Input('url', 'pathname')])
def display_page(pathname):
''' callback to determine layout to return '''
view = None
url = dash.no_update
if pathname == '/login':
view = login.layout_login
elif pathname == '/success':
if current_user.is_authenticated:
view = home
elif pathname == '/logout':
if current_user.is_authenticated:
logout_user()
view = login.layout_login
else:
view = login.layout_login
url = '/login'
elif pathname == '/profile':
if current_user.is_authenticated:
view = profile.user
else:
view = login.layout_login
url = '/login'
else:
if current_user.is_authenticated:
view = home
else:
view = login.layout_login
url = '/login'
# You could also return a 404 "URL not found" page here
return view, url
@app.callback(Output('page-content-2', 'children'),
[Input('url', 'pathname')])
def display_page(pathname):
view = None
if pathname == "/" or pathname == "/cadastro":
view = cadastro.layout_cadastro
elif pathname == '/simulacao':
if current_user.is_authenticated:
view = simulacao.layout_simulacao
elif pathname == '/beneficios':
if current_user.is_authenticated:
view = editar_beneficios.layout_beneficios
elif pathname == '/configuracoes':
if current_user.is_authenticated:
view = layout_configuracoes
elif pathname == '/funcoes':
if current_user.is_authenticated:
view = editar_funcao.layout_funcoes
elif pathname == '/colaboradores':
if current_user.is_authenticated:
view = editar_colaboradores.layout_colaboradores
elif pathname == '/exportar_caju':
if current_user.is_authenticated:
view = export_caju.layout_caju
return view
try:
import conf
if __name__ == '__main__':
app.run_server(debug=True, port=80)
except:
if __name__ == '__main__':
app.run_server(debug=False, host='0.0.0.0', port=80)