Skip to content

Commit 79edf35

Browse files
committed
Pages modularization
1 parent 8db9a12 commit 79edf35

6 files changed

Lines changed: 101 additions & 2 deletions

File tree

src/TryNiceGUI/home_page.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Home Page."""
2+
3+
from nicegui import ui
4+
5+
6+
def content() -> None:
7+
"""Home Page Content."""
8+
ui.label('Hello NiceGUI!').classes('text-h4 font-bold text-grey-8')

src/TryNiceGUI/main.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
"""Main file."""
22

3-
from nicegui import ui
3+
4+
from nicegui import app, ui
5+
6+
from . import home_page, theme
7+
from .pages import with_router, delay_creation
8+
9+
10+
def populate_ui():
11+
"""Insert everything in the UI."""
12+
13+
@ui.page('/')
14+
def index_page() -> None:
15+
"""Index."""
16+
with theme.frame('Homepage'):
17+
home_page.content()
18+
19+
delay_creation.create()
20+
app.include_router(with_router.router)
421

522

623
def run():
724
"""Run the application."""
8-
ui.label('Hello NiceGUI!')
25+
populate_ui()
926
ui.run(
1027
reload=False,
1128
dark=True,

src/TryNiceGUI/menu.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Menu for all the pages."""
2+
from nicegui import ui
3+
4+
5+
def menu() -> None:
6+
"""Create Menu."""
7+
ui.link('Home', '/').classes(replace='text-white')
8+
ui.link('A', '/a').classes(replace='text-white')
9+
ui.link('B', '/b').classes(replace='text-white')
10+
ui.link('C', '/c').classes(replace='text-white')
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Pages created when `create` is called."""
2+
from .. import theme
3+
4+
from nicegui import ui
5+
6+
7+
def create() -> None:
8+
"""Create Pages."""
9+
10+
@ui.page('/a')
11+
def example_page_a():
12+
with theme.frame('- Example A -'):
13+
ui.label('Example A').classes('text-h4 text-grey-8')
14+
15+
@ui.page('/b')
16+
def example_page_b():
17+
with theme.frame('- Example B -'):
18+
ui.label('Example B').classes('text-h4 text-grey-8')
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Pages added to route dynamically."""
2+
3+
from .. import theme
4+
5+
from nicegui import APIRouter, ui
6+
7+
router = APIRouter(prefix='/c')
8+
9+
10+
@router.page('/')
11+
def example_page():
12+
"""List Items."""
13+
with theme.frame('- Example C -'):
14+
ui.label('Example C').classes('text-h4 text-grey-8')
15+
for i in range(1, 4):
16+
ui.link(
17+
f'Item {i}',
18+
f'/c/items/{i}',
19+
).classes('text-xl text-grey-8')
20+
21+
22+
@router.page('/items/{id}', dark=True)
23+
def item(id: str):
24+
"""Display Item Page."""
25+
with theme.frame(f'- Example C{id} -'):
26+
ui.label(f'Item #{id}').classes('text-h4 text-grey-8')
27+
ui.link('go back', router.prefix).classes('text-xl text-grey-8')

src/TryNiceGUI/theme.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Common Theme for all the pages."""
2+
3+
from contextlib import contextmanager
4+
5+
from .menu import menu
6+
7+
from nicegui import ui
8+
9+
10+
@contextmanager
11+
def frame(navtitle: str):
12+
"""Share the same styling and behavior across all pages."""
13+
with ui.header().classes('justify-between text-white'):
14+
ui.label("Theme Header")
15+
ui.label(navtitle)
16+
with ui.row():
17+
menu()
18+
with ui.column().classes('absolute-center items-center'):
19+
yield

0 commit comments

Comments
 (0)