Avaiga / taipy-gui

Graphical User Interface generator for Taipy
Apache License 2.0
60 stars 20 forks source link

BUG- on_navigate() switches to the requested page content, but the URL is not updated in the browser #588

Closed Dr-Irv closed 1 year ago

Dr-Irv commented 1 year ago

Description If you use on_navigate() to change the page, the URL in the browser is not updated to the correct place.

How to reproduce

from taipy import Gui
from taipy.gui import State, notify

allow_page2 = False
last_request = "Unknown"

def on_navigate(state: State, page_name: str) -> str:
    state.last_request = page_name
    if page_name == "page2" and not allow_page2:
        notify(state, "error", "cannot go to page2 before page1")
        return "page1"
    return page_name

page1_md = """## Page 1
Last requested page is <|{last_request}|text|>
"""
page2_md = "## Page 2"
navbar = "<|navbar|lov={[('/page1', 'First Page'), ('/page2', 'Second Page')]}|" ">"

if __name__ == "__main__":
    gui = Gui()

    gui.add_pages({"/": navbar, "page1": page1_md, "page2": page2_md})

    gui.run(
        page=navbar,
        async_mode="threading",
        title="Notify Demo",
        debug=True,
        use_reloader=True,
    )

When you start the app, the URL shown in the browser is http://127.0.0.1:5000/page1. When you click on page2, it should switch to show that URL. The content does switch, but the URL doesn't change. Also, the navbar doesn't update either (but that's probably because the URL is wrong).

Expected behavior The URL in the browser should change to the requested page.

Screenshots At beginning of app, it looks like this: image

If you click on "page2", you get this. Note that the content is correct, but the URL at the top is wrong. image

Runtime environment Please specify relevant indications.

Dr-Irv commented 1 year ago

It seems that if I call navigate() in the on_navigate() function, it works, i.e.,

def on_navigate(state: State, page_name: str) -> str:
    state.last_request = page_name
    if page_name == "page2" and not allow_page2:
        notify(state, "error", "cannot go to page2 before page1")
        navigate(state, "page1")
        return "page1"
    return page_name

But I don't know if that's "safe" from a taipy perspective. If so, then the docs should be updated accordingly.

FlorianJacta commented 1 year ago

It is a great workaround for the moment to solve your issue. I agree, either way, the doc has to change or it should correctly redirect the user to the correct page. Having the on_navigate() correctly redirects the user like in the current documentation seems to me the better way.

FredLL-Avaiga commented 1 year ago

So the problem is that if the user requests /page2 but on_navigate blocks page2, we need to reflect the real page shown in the URL bar.