FastAPI @app.get inside NiceGUI page #507
-
Hi, first of all, I want to thank you for this framework and the work of all its contributors, it's really wonderful. So this is my main.py file: from nicegui import app, ui
import home_page
@ui.page('/home')
def home() -> None:
home_page.content()
ui.button('Login', on_click= lambda: ui.open('/home?success=true')).props('icon=login')
ui.run(port=8585) And this is my home_page.py file: from nicegui import ui
def content() -> None:
ui.markdown('###Home Page') The thing I try to achieve is when the URL localhost:8585/home?success=true is opened the page will show together with a notification: 'Successfully logged in', in the opposite when the URL localhost:8585/home?success=false is opened then only page content will show without any notification. I tried changing the home_page.py code to the version shown below: from nicegui import app, ui
def content() -> None:
@app.get('/home')
def check(success: bool):
if success:
ui.notify('Successfully logged in')
ui.markdown('###Home Page')
else:
ui.markdown('###Home Page') but it didn't work. So is it possible to call a function inside the page .py file, based on query parameters parsed together with a page URL, so it will affect the way the GUI renders? If something I've mentioned here isn't clear or understandable feel free to ask me. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yes, that's possible. But you need to handle the query parameter in the page function and pass it to def content(success: bool) -> None:
ui.markdown('### Home Page')
if success:
ui.label('Successfully logged in')
@ui.page('/home')
def home(success: bool) -> None:
content(success) Note that this example uses a label instead of from nicegui import Client
def content(success: bool) -> None:
ui.markdown('### Home Page')
if success:
ui.notify('Successfully logged in')
@ui.page('/home')
async def home(success: bool, client: Client) -> None:
await client.connected()
content(success) |
Beta Was this translation helpful? Give feedback.
Yes, that's possible. But you need to handle the query parameter in the page function and pass it to
content
. Here is an example:Note that this example uses a label instead of
ui.notify
. To show a notification immediately after opening a page, we need to wait for the client connection. Therefore we add aclient
parameter and await itsconnected
method. Then we can callcontent
and the notification is successfully sent to the client: