@ui.page('/sub_path') decorator shows page at "/" rather than at "/sub_path" #3981
-
Questionissue is: from pathlib import Path
from nicegui import ui, app
class MyClass:
def __init__(self):
self.create_page()
@ui.page('/sub_path')
def create_page(self):
ui.label("Here")
if __name__ in {"__main__", "__mp_main__"}:
app.on_startup(MyClass)
uvicorn_args = {
'uvicorn_reload_dirs': str(Path(__file__).parent.resolve()),
'port': 8080,
'show': False,
'title': 'my_gui',
'favicon': '🤖'
}
ui.run(**uvicorn_args) |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Nov 12, 2024
Replies: 1 comment 2 replies
-
Hi @belalhmedan90, Let's break it down: class MyClass:
def __init__(self):
self.create_page()
@ui.page('/sub_path')
def create_page(self):
ui.label("Here")
MyClass() Now two things happen:
To fix this, you can nest the page function so that it is decorated but not called on startup: class MyClass:
def __init__(self):
self.create_page()
def create_page(self):
@ui.page('/sub_path')
def sub_page():
ui.label('Here')
MyClass() Or you call the decorator explicitly, maybe inside the initializer like this: class MyClass:
def __init__(self):
ui.page('/sub_path')(self.sub_page)
def sub_page(self):
ui.label('Here')
MyClass() |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
belalhmedan90
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @belalhmedan90,
Let's break it down:
Now two things happen:
ui.page
decorator you define a new route at "/sub_path" which will call thecreate_page
method. Note that this function has a parameterself
. So when accessing the route "/sub_path?self=something", the method is called and you see "Here".MyClass()
, which callscreate_page()
and creates a label in the current context, which is the auto-index page. So when accessing the route "/", you also see "Here".To fix this, you can nest the p…