Hi everyone, i have a dash code that has two pages (login page and graficos page) the user only can access the graficos page if he is logged, in case he is not logged he is redirected to the login page. My problem is that after the user tries to see the graficos page without beign logged and logging in the layout doesn’t change, and i find out that is because the callback don’t see the url changing, but it’s changing, does anyone know how can i make to trigger a callback even if the Input value apparently don’t change?
Here is the code:
app.py
from dash import Dash
from dash_bootstrap_components import themes
import os
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin
app = Dash(__name__, external_stylesheets=[themes.BOOTSTRAP])
server = app.server
app.config.suppress_callback_exceptions = True
server.config.update(
SECRET_KEY=os.urandom(12),
SQLALCHEMY_DATABASE_URI="mysql://root:Bambam&xx12@localhost/mobbi",
SQLALCHEMY_TRACK_MODIFICATIONS=False
)
db = SQLAlchemy(server)
login_manager = LoginManager(server)
class Usuarios(db.Model, UserMixin):
__tablename__ = "usuarios"
id = db.Column(db.Integer, primary_key=True)
u_nome = db.Column(db.String(50), nullable=False)
p_nome = db.Column(db.String(50), nullable=False)
login = db.Column(db.String(50), nullable=False)
senha_hash = db.Column(db.String(255), nullable=False)
@login_manager.user_loader
def load_user(user_id):
return Usuarios.query.get(int(user_id))
login.py
import dash_bootstrap_components as dbc
from dash import Output, Input, State, dcc
from flask_login import login_user
from app import app, Usuarios
import hashlib
layout = dbc.Form([
dcc.Location(id="url_login"),
dbc.Label("Login"),
dbc.Input(type="text", id="login-field", placeholder="Digite seu login"),
dbc.Label("Senha"),
dbc.Input(type="password", id="password-field", placeholder="Digite sua senha"),
dbc.Button("Login", id="login-button", n_clicks=0)
])
@app.callback(
Output("url_login", "pathname"),
Input("login-button", "n_clicks"),
[
State("login-field", "value"),
State("password-field", "value")
]
)
def make_login(n_clicks, login, password):
usuario = Usuarios.query.filter_by(login=login).first()
if n_clicks != 0:
if usuario:
check_password = usuario.senha_hash
hashed_password = hashlib.sha256(password.encode()).hexdigest()
if check_password == hashed_password:
login_user(usuario)
print("tá logando")
return "/graficos"
else:
print("senha errada")
else:
print("usuário não cadastrado")
main.py
from dash import html, dcc, Output, Input
from app import app
from pages import login, graficos
from flask_login import current_user
app.layout = html.Div([
dcc.Location(id="url", refresh=False),
html.Div(id="page-content")
])
@app.callback(
Output("page-content", "children"),
Input("url", "pathname")
)
def change_page(pathname):
print(f"{'==='*30}\nUsuário autenticado: {current_user.is_authenticated}")
if pathname == "/":
print("entrou na página de login")
return login.layout
elif pathname == "/graficos":
print("entrou na página de gráficos")
if current_user.is_authenticated:
print("logou")
return graficos.layout
else:
print("não logou")
return login.layout
else:
return "404"
if __name__ == "__main__":
app.run_server(debug=True)