Skip to content

Commit

Permalink
Preparing nested outlets
Browse files Browse the repository at this point in the history
  • Loading branch information
Alyxion committed Apr 14, 2024
1 parent 3167521 commit 75a5e53
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 220 deletions.
26 changes: 24 additions & 2 deletions examples/outlet/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from nicegui import ui


ui.outlet('/')
@ui.outlet('/')
def spa1():
ui.label("spa1 header")
yield
ui.label("spa1 footer")


# SPA outlet routers can be defined side by side
ui.outlet('/spa2')
@ui.outlet('/spa2')
def spa2():
ui.label('spa2')
yield
Expand All @@ -20,11 +20,33 @@ def spa2():
def spa1_index():
ui.label('content of spa1')
ui.link('more', '/more')
ui.link('nested', '/nested')


@spa1.view('/more')
def spa1_more():
ui.label('more content of spa1')
ui.link('main', '/')
ui.link('nested', '/nested')


@spa1.outlet('/nested')
def nested():
ui.label('nested outlet')
yield


@nested.view('/')
def nested_index():
ui.label('content of nested')
ui.link('nested other', '/nested/other')


@nested.view('/other')
def nested_other():
ui.label('other nested')
ui.link('nested index', '/nested')


'''
# the view is a function upon the decorated function of the outlet (same technique as "refreshable.refresh")
Expand Down
2 changes: 1 addition & 1 deletion examples/single_page_router/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from nicegui import ui
from nicegui.page import page
from nicegui.outlet import SinglePageRouter
from nicegui.single_page_router import SinglePageRouter


def setup_page_layout(content: Callable):
Expand Down
4 changes: 2 additions & 2 deletions examples/single_page_router/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from nicegui import ui
from nicegui.page import page
from nicegui.outlet import SinglePageRouter
from nicegui.single_page_router import SinglePageRouter


@page('/', title='Welcome!')
Expand All @@ -17,5 +17,5 @@ def about():
ui.link('Index', '/')


router = SinglePageRouter('/').setup_page_routes()
router = SinglePageRouter('/').reroute_pages()
ui.run()
10 changes: 5 additions & 5 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

if TYPE_CHECKING:
from .page import page
from .outlet import Outlet as outlet
from .elements.router_frame import RouterFrame

templates = Jinja2Templates(Path(__file__).parent / 'templates')

Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self, page: page, *, shared: bool = False) -> None:

self.page = page
self.storage = ObservableDict()
self.outlets: Dict[str, "outlet"] = {}
self.single_page_router_frame: Optional[RouterFrame] = None

self.connect_handlers: List[Union[Callable[..., Any], Awaitable]] = []
self.disconnect_handlers: List[Union[Callable[..., Any], Awaitable]] = []
Expand Down Expand Up @@ -227,10 +227,10 @@ async def send_and_wait():
def open(self, target: Union[Callable[..., Any], str], new_tab: bool = False) -> None:
"""Open a new page in the client."""
path = target if isinstance(target, str) else self.page_routes[target]
for cur_outlet in self.outlets.values():
target = cur_outlet.resolve_target(target)
for cur_spr in self.single_page_routes.values():
target = cur_spr.resolve_target(target)
if target.valid:
cur_outlet.navigate_to(path)
cur_spr.navigate_to(path)
return
self.outbox.enqueue_message('open', {'path': path, 'new_tab': new_tab}, self.id)

Expand Down
2 changes: 1 addition & 1 deletion nicegui/elements/router_frame.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Union, Callable, Tuple, Any, Optional, Self

from nicegui import ui, helpers, context, background_tasks, core
from nicegui.router_frame_url import SinglePageTarget
from nicegui.single_page_target import SinglePageTarget


class RouterFrame(ui.element, component='router_frame.js'):
Expand Down
Loading

0 comments on commit 75a5e53

Please sign in to comment.