-
Can we access the top container of the current page? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Yes. When the Lines 75 to 77 in 76b1cbd |
Beta Was this translation helpful? Give feedback.
-
@falkoschindler, can you elaborate on def on_add():
with client.layout:
ui.label('add a label to the bottom of main page')
ui.button('Add', on_click=on_add)
ui.run() |
Beta Was this translation helpful? Give feedback.
-
@falkoschindler, I found a solution: from nicegui import ui
def on_add(e):
log.push(str(e.client.layout))
log.push(str(e.client.layout.slots))
log.push(str(e.client.layout.default_slot))
log.push(str(e.client.layout.default_slot.children))
log.push(str(e.client.layout.default_slot.children[0].tag))
log.push(str(e.client.layout.default_slot.children[0].default_slot.children[0].tag))
log.push(str(e.client.layout.default_slot.children[0].default_slot.children[0].default_slot.children[0].tag))
with e.client.layout.default_slot.children[0].default_slot.children[0].default_slot.children[0]:
ui.label('add a label to the bottom of main page')
ui.button('Add', on_click=on_add)
log = ui.log().classes('w-full h-96')
ui.run() I am not too familiar with the concept of 'slot'. Is the default_slot the way of navigating through the layout? |
Beta Was this translation helpful? Give feedback.
-
@smojef your example code does not use page creators. Therefore NiceGUI can not provide you with a client object. If you want to stick with the globally shared index page, you can access its client via from nicegui import ui, globals
def on_add():
with globals.index_client.content:
ui.label('add a label to the bottom of main page')
ui.button('Add', on_click=on_add)
ui.run() While this works, I generally suggest to move to page-generators when building more complex applications. Then each browser gets its own user interface. In your example this would result in new labels only being shown to the specific user, not everyone who accesses the server. Here is an rewrite of the above: from nicegui import ui, Client
@ui.page('/')
def main_page(client: Client):
def on_add():
with client.content:
ui.label('add a label to the bottom of main page')
ui.button('Add', on_click=on_add)
ui.run() In this short example there is actually no need to use @ui.page('/')
def main_page():
def on_add():
ui.label('add a label to the bottom of main page')
ui.button('Add', on_click=on_add) |
Beta Was this translation helpful? Give feedback.
-
@rodja Thanks for your answer. I understand the difference. from nicegui import ui, globals
def on_add(e):
ui.label('add to default context')
with globals.index_client.content:
ui.label('add to global content')
with ui.card():
ui.label('CARD')
ui.button('Add from card', on_click=on_add)
with ui.card():
ui.label('INNER CARD')
ui.button('Add from inner card', on_click=on_add)
ui.button('Add from page', on_click=on_add)
ui.run() So I was stuck with elements showing in my cards instead of the page. |
Beta Was this translation helpful? Give feedback.
@smojef your example code does not use page creators. Therefore NiceGUI can not provide you with a client object. If you want to stick with the globally shared index page, you can access its client via
globals.index_client
. Also most of the time you do not need to work with the QLayout but rather use thecontent
property of the client. Thats why you need to navigate through the tree to via default_slots from layout to content. A working example would be:While this works, I generally suggest to move to…