How can I create a page from a list? #2810
-
Questionfor dir in os.walk("data"):
for file in dir[2]:
if file.endswith(".json"):
page = str(file).replace(".json", "")
pages.append(page)
with ui.card().classes("absolute-center"):
for page in pages:
print(f"1:{page}")
ui.button(page, on_click=lambda : ui.open(f"ana/{page}")).classes("w-full")
@ui.page(f"/ana/{page}")
def ana_page():
file = f"data/{page}.json"
if not os.path.exists(file):
ana_messages = file_not_found_error(file)
else:
with open(file, 'r') as f:
ana_messages = json.load(f)
number = random.randint(0, len(ana_messages) - 1)
print(number)
ana_messages = ana_messages[number]
id = ana_messages["id"]
msg = ana_messages["msg"]
author = ana_messages["author"]
time = ana_messages["time"] This code will always make the button and page's values last in the list. But ui. page() seems to be fine. pages = ["a", "b", "c"]
for page in pages:
ui.button(page, on_click=lambda : ui.open(f"ana/{page}")).classes("w-full") # the first page = a, but ui.open(f"ana/{page}") = c
@ui.page(f"/ana/{page}") # It seems to be fine
def ana_page():
······
file = f"data/{page}.json" # page = c What should I do if I want to create a page based on the values of a list? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @SHDocter , I suppose you mean to create individual pages, for each item from the list? I think this is the same late binding issue that we will find in some of the other discussions. If you force the loop variable inside your page function, it should work. So something like this below: pages = ['a', 'b', 'c', 'd']
for page in pages:
@ui.page(f'/{page}')
def page_def(page=page):
ui.label(f'Welcome to page {page}') So if I use Thanks, |
Beta Was this translation helpful? Give feedback.
Hi @SHDocter ,
I suppose you mean to create individual pages, for each item from the list? I think this is the same late binding issue that we will find in some of the other discussions.
If you force the loop variable inside your page function, it should work. So something like this below:
So if I use
page=page
inside the page function, it forces python to use the correct page value, so if you go to any page, now the label value should work correctly.Thanks,
Anindya