Skip to content

Commit 241215a

Browse files
Michael IkemannAlyxion
Michael Ikemann
authored andcommitted
Made single-page multi-user rdy
1 parent b3d73f1 commit 241215a

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

nicegui/single_page.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import asyncio
12
import inspect
23
from typing import Callable, Dict, Union
34

45
from fastapi.routing import APIRoute
56

6-
from nicegui import background_tasks, helpers, ui, core, Client
7+
from nicegui import background_tasks, helpers, ui, core, Client, app
78
from nicegui.app import AppConfig
89

910

@@ -19,16 +20,17 @@ def __init__(self, path: str, **kwargs) -> None:
1920
super().__init__()
2021

2122
self.routes: Dict[str, Callable] = {}
22-
self.content: Union[ui.element, None] = None
23+
# async access lock
2324
self.base_path = path
2425
self.find_api_routes()
2526

26-
print("Configuring SinglePageRouter with path:", path)
27-
2827
@ui.page(path, **kwargs)
2928
@ui.page(f'{path}' + '{_:path}', **kwargs) # all other pages
30-
def root_page():
31-
self.frame()
29+
async def root_page(client: Client):
30+
await client.connected()
31+
if app.storage.session.get('__pageContent', None) is None:
32+
content: Union[ui.element, None] = RouterFrame(self.base_path).on('open', lambda e: self.open(e.args))
33+
app.storage.session['__pageContent'] = content
3234

3335
def find_api_routes(self):
3436
page_routes = set()
@@ -39,7 +41,7 @@ def find_api_routes(self):
3941
Client.single_page_routes[route] = self
4042
self.routes[route] = key
4143

42-
for route in core.app.routes:
44+
for route in core.app.routes.copy():
4345
if isinstance(route, APIRoute):
4446
if route.path in page_routes:
4547
core.app.routes.remove(route)
@@ -68,18 +70,13 @@ def open(self, target: Union[Callable, str], server_side=False) -> None:
6870
if server_side:
6971
ui.run_javascript(f'window.history.pushState({{page: "{target}"}}, "", "{target}");')
7072

71-
if self.content is None:
72-
return
73-
74-
async def build() -> None:
75-
with self.content:
73+
async def build(content_element) -> None:
74+
with content_element:
7675
result = builder()
7776
if helpers.is_coroutine_function(builder):
7877
await result
7978

80-
self.content.clear()
81-
background_tasks.create(build())
79+
content = app.storage.session['__pageContent']
80+
content.clear()
8281

83-
def frame(self) -> ui.element:
84-
self.content = RouterFrame(self.base_path).on('open', lambda e: self.open(e.args))
85-
return self.content
82+
background_tasks.create(build(content))

0 commit comments

Comments
 (0)