1

I have a Taipy application with several pages. Each page uses the "run()" method to scope the methods executed. Now, I want that one dynamically assigned State variable state.some_id = "1234abcd" is available in other pages too (to avoid that I have to re-implement the assignment again). If a state variable cannot be made available across pages, a common dynamically assigned variable some_id = "1234abcd" would also work. However, I could get it working.

I already tried experimenting with plain variables (not State variables) in main.py. These variables are available among all pages after the initial assignment, but it seems I cannot reassign them dynamically (e.g. via a on_change method). The State variables, such as state.some_id = "1234abcd", is not available in other pages - only in the page where I assign it via a control element.

1 Answers1

0

Putting a run() method in each file is unnecessary. You only need to put the Markdown string in the Markdown object.

The variables defined in the main.py can always be accessed in any functions and callbacks. It is not the case of variables defined in page.py inside the Markdown page (value in the example below). These can only be accessed through local callbacks that are being called from this page. The documentation for binding variables van be found here.

Here, value can be accessed with the change_value callback (defined for the page). Another function not defined on this page cannot access it.

*global_value * is here in the Global Scope as I import it in my main.py. It can be accessed on every page. I can define callbacks associated to it in my main.py.

It is also possible to create visual elements in the root page to interact with global_value. Elements in the root page are present on each other page (like the navbar in this example, see the doc for more explanation).

page.py:

from taipy.gui import Markdown

value = 10

global_value = 20

page_1_md = Markdown("""
<|{value}|slider|>

<|{global_value}|slider|>

<|Push|button|on_action=change_value|>
""")

def change_value(state):
    print("change value")
    state.value = 100

main.py:

I import the Markdown object of page_1 to create my application. Don't forget to import global_value if you want to use it in different pages.

from taipy.gui import Gui
from page_1 import page_1_md, global_value

pages = {
   "/":"<|navbar|>",
   "Page-1":page_1_md 
}

Gui(pages=pages).run()

Would you happen to have a simple example of what the issue is? Or would you like to suggest an improvement on this behaviour?

Florian Jacta
  • 1,143
  • 1
  • 2
  • 5
  • Sure, the idea is this. I have a selector on one page and use it so set a variable to a specific id (some_id in the example above). I want that this selection is no available to the other pages too because I don't want to re-implement the selector for each page. If I use your example, the question is how I can make "value" available in a page_2_md (which is not in your example but could be added easily). – nonome noname May 09 '23 at 17:07
  • I edited my reply to answer your comment. I hope it answers it well – Florian Jacta May 11 '23 at 11:00