Creating a Single Page Application (SPA) with NiceGUI #349
Replies: 7 comments 10 replies
-
Very nice. Build-in SPA support is surely a good thing for NiceGUI in the long run. Your code shows a first implementation. I like the introduction of from nicegui import ui
def show_one():
content.clear()
with content:
ui.label('Content One').classes('text-2xl')
def show_two():
content.clear()
with content:
ui.label('Content Two').classes('text-2xl')
def show_three():
content.clear()
with content:
ui.label('Content Three').classes('text-2xl')
with ui.row():
ui.button('One', on_click=show_one)
ui.button('Two', on_click=show_two)
ui.button('Three', on_click=show_three)
content = ui.column()
show_one()
ui.run() While it works nicely and is also quite short, it has by no means any framework to support history, location path updates and other things which can easily be integrated in your solution. Maybe the build-in support can be based on your code? Another approach would be to look at Vue.js router and use that as foundation... |
Beta Was this translation helpful? Give feedback.
-
Damn, I didn't know you could do I used it as a base here, and I rewrote the Redirect.run function. No more errors now, thank you. |
Beta Was this translation helpful? Give feedback.
-
We just merged an very simple SPA example into main: https://github.com/zauberzeug/nicegui/tree/main/examples/single_page_app |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm relatively new to coding and NiceGUI, and I'm attempting to learn by dissecting your code. I've replicated the files you've provided here, but upon running the code, I encountered an error: ModuleNotFoundError: No module named 'components'. Could someone please guide me on how to troubleshoot this issue? Is 'components' supposed to be another module, or is it something that needs to be installed via the terminal? |
Beta Was this translation helpful? Give feedback.
-
Ficou muito massa, mano! Parabéns pelo trabalho. Sabes me dizer se você ainda continua utilizando o nicegui para criar essas ferramentas? Te atende bem? |
Beta Was this translation helpful? Give feedback.
-
beautiful example , |
Beta Was this translation helpful? Give feedback.
-
Just for reference: in #2811 @Alyxion created a beautiful pull-request to provide direct API support for SPAs in NiceGUI. Hopefully it will be merged in the next few weeks. |
Beta Was this translation helpful? Give feedback.
-
Example.of.the.System.Working.mp4
Using that as a base, I created the following module:
routes.py
Note: it will always report an error when changing pages, because the Temporary Element no longer exists, but the rendering loop will still try to render it. This does not break anything in the application.
To use the system, just define some pages, like the examples below:
sales.py
Note: always remember to structure the first element as ui.column, as it is the element with the closest behavior to the base @ui.page.
And in the main of your application, define the root page “/”, with the following initial structure:
main.py
That's it, now with buttons like the ones below, you can easily navigate between your pages, updating only the necessary components:
layout.py
Beta Was this translation helpful? Give feedback.
All reactions