I have two pages in my main.py
pages = {
“/”: root_md,
“Page-MAP”: page1_md,
“Page-DASHBOARD”: page2_md,
}
How can I, after a clic on my Page-MAP, receive an information, a variable on my Page-DASHBOARD redirection?
The objectif is to update my page after a clic on an other page.
Clic on my Page-MAP:
def on_change(state, var_name, var_value):
navigate(state, to=’Page-DASHBOARD’)
I have two pages in my main.py
pages = {
“/”: root_md,
“Page-MAP”: page1_md,
“Page-DASHBOARD”: page2_md,
}
Clic on my Page-MAP:
def on_change(state, var_name, var_value):
navigate(state, to=’Page-DASHBOARD’)
Jérémie Marchand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
To exchange information between pages in Taipy and update the content of Page-DASHBOARD
based on actions performed on Page-MAP
, you can use a combination of state management and page navigation. Here’s a step-by-step approach to achieve this:
1. Define a State Variable
You need a shared state variable that can be accessed and modified across different pages. This variable will hold the information you want to pass between pages.
2. Update State on Page-MAP
In the Page-MAP
page, update this state variable when a user clicks or performs an action. Use the on_change
function to handle this event.
3. Use State in Page-DASHBOARD
In the Page-DASHBOARD
page, access the updated state variable to dynamically update the content.
Here’s a detailed example:
1. Define a State Variable in main.py
from taipy import Taipy
# Initialize Taipy app
app = Taipy()
# Define shared state
state = {
"shared_variable": None # This will hold the data you want to pass
}
# Define pages
pages = {
"/": root_md,
"Page-MAP": page1_md,
"Page-DASHBOARD": page2_md
}
2. Update State in Page-MAP
Define the on_change
function to update the state variable and navigate to Page-DASHBOARD
.
def on_change(state, var_name, var_value):
# Update the shared state
state["shared_variable"] = var_value # Assign the value you want to pass
# Navigate to Page-DASHBOARD
navigate(state, to='Page-DASHBOARD')
Attach this function to the action (e.g., button click) on Page-MAP
.
# Example button click action
button = Button("Click me", on_change=on_change)
3. Use State in Page-DASHBOARD
Access the shared state variable in Page-DASHBOARD
and update the page content accordingly.
def page2_md(state):
# Use the shared state variable
shared_value = state["shared_variable"]
# Update content based on the shared variable
content = f"Received value: {shared_value}"
return f"""
<h1>Dashboard Page</h1>
<p>{content}</p>
"""
Summary
- Define a shared state variable in your
main.py
file. - Update this state variable on
Page-MAP
using theon_change
function. - Access and use the state variable in
Page-DASHBOARD
to update the content.
This approach allows you to pass information between pages and dynamically update content based on user interactions.
gamika sanjana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.