After clicking the login button, the application does not redirect to the intended page, and no action appears to occur. It seems that the button click event is either not being handled correctly or the logic to navigate to the next page is missing or not functioning as expected.
Below is the code details app.py
# app.py
from taipy.gui import Gui
from pages.login_page import login_page, username, password, login_error, on_login_action
from pages.overview_page import overview_page
from pages.analysis_page import analysis_page
# App state
logged_in = False
page = "login"
# Pages dictionary with tgb pages
pages = {
"login": login_page,
"overview": overview_page,
"analysis": analysis_page
}
def before_page_display(state, page_name):
if not state.logged_in and page_name != "login":
state.page = "login"
gui = Gui(pages=pages)
gui.run(title="Secure Taipy App with Builder",
port=5000,
use_reloader=True,
before_page_display=before_page_display)
login_page.py
# pages/login_page.py
import taipy.gui.builder as tgb
from taipy.gui import notify
VALID_USERNAME = "admin"
VALID_PASSWORD = "1234"
username = ""
password = ""
login_error = ""
def on_login_action(state):
if state.username == VALID_USERNAME and state.password == VALID_PASSWORD:
state.logged_in = True
state.login_error = ""
state.page = "overview"
else:
state.login_error = "Invalid username or password."
notify(state, "error", state.login_error)
with tgb.Page() as login_page:
tgb.text("🔒 Login", mode="md")
with tgb.layout(columns="1 1 1"):
tgb.text("Username")
tgb.input(value="{username}")
with tgb.layout(columns="1 1 1"):
tgb.text("Password")
tgb.input(value="{password}", password=True)
tgb.button("Login", on_action=on_login_action)
tgb.text("{login_error}", color="red")
analysis_page.py
# pages/analysis_page.py
import taipy.gui.builder as tgb
with tgb.Page() as analysis_page:
tgb.text("📈 Analysis", mode="md")
tgb.text("This is the analysis page content.")
overview_page.py
# pages/overview_page.py
import taipy.gui.builder as tgb
with tgb.Page() as overview_page:
tgb.text("📊 Overview", mode="md")
tgb.text("This is the overview page content.")
Could you help identify the issue with this code and suggest the necessary changes to ensure that the login button functions as intended?
It needs to use taipu.gui.navigate(state, to="page_name")
Doc: navigate
from taipy.gui import notify, navigate
def on_login_action(state):
if state.username == VALID_USERNAME and state.password == VALID_PASSWORD:
state.logged_in = True
state.login_error = ""
#notify(state, "Info", "Loggged in")
navigate(state, to="overview")
else:
state.login_error = "Invalid username or password."
notify(state, "error", state.login_error)
Frankly app.py
also doesn't work as you expect. It loads first page from dictionary pages
. If I put overview
as first in dictionary then it will load it.
And before_page_display
is never executed because it seems run() doesn't have parameter before_page_display
But Gui() has on_navigate
and on_page_load
and maybe this could be used to check if logged in
is True
EDIT:
This works for me.
After adding on_navigate()
(without assigning it to any parameter in Gui
) it automatically checks logged_in
even if I manually try to open page overview
at start, and it redirects to login
page.
from taipy.gui import Gui, navigate
from pages.login_page import login_page, username, password, login_error, on_login_action
from pages.overview_page import overview_page
from pages.analysis_page import analysis_page
# App state
logged_in = False
page = "login"
# Pages dictionary with tgb pages
pages = {
"login": login_page,
"overview": overview_page,
"analysis": analysis_page
}
def on_navigate(state, page_name):
#print(f"[DEBUG] {page_name = }")
if not state.logged_in and page_name != "login":
#print("[DEBUG] inside 'if'")
#state.page = "login" # <--- doesn't work
navigate(state, to="login")
return page_name
gui = Gui(pages=pages)
gui.run(title="Secure Taipy App with Builder",
port=5000,
use_reloader=True)
# before_page_display=before_page_display) # <-- it seems useless