Explorar o código

remove `shared` argument for page decorator

Falko Schindler %!s(int64=2) %!d(string=hai) anos
pai
achega
f33cf4bd56
Modificáronse 3 ficheiros con 20 adicións e 100 borrados
  1. 0 38
      nicegui/page_builder.py
  2. 10 44
      tests/test_pages.py
  3. 10 18
      website/api_docs_and_examples.py

+ 0 - 38
nicegui/page_builder.py

@@ -1,38 +0,0 @@
-import asyncio
-from dataclasses import dataclass
-from typing import TYPE_CHECKING, Awaitable, Callable, Optional
-
-import justpy as jp
-from starlette.requests import Request
-
-from .task_logger import create_task
-
-if TYPE_CHECKING:
-    from .page import Page
-
-
-@dataclass
-class PageBuilder:
-    function: Callable[[], Awaitable['Page']]
-    shared: bool
-    favicon: Optional[str] = None
-
-    _shared_page: Optional['Page'] = None
-
-    async def build(self) -> None:
-        assert self.shared
-        self._shared_page = await self.function()
-
-    async def route_function(self, request: Request) -> 'Page':
-        if self.shared:
-            while self._shared_page is None:
-                await asyncio.sleep(0.05)
-            page = self._shared_page
-        else:
-            page: Page = await self.function(request)
-        return await page._route_function(request)
-
-    def create_route(self, route: str) -> None:
-        if self.shared:
-            create_task(self.build())
-        jp.Route(route, self.route_function)

+ 10 - 44
tests/test_pages.py

@@ -21,16 +21,6 @@ def test_page(screen: Screen):
     screen.should_contain('Hello, world!')
 
 
-def test_shared_page(screen: Screen):
-    @ui.page('/', shared=True)
-    def page():
-        ui.label('Hello, world!')
-
-    screen.open('/')
-    screen.should_contain('NiceGUI')
-    screen.should_contain('Hello, world!')
-
-
 def test_auto_index_page(screen: Screen):
     ui.label('Hello, world!')
 
@@ -96,24 +86,22 @@ def test_creating_new_page_after_startup(screen: Screen):
 def test_shared_and_individual_pages(screen: Screen):
     @ui.page('/individual_page')
     def individual_page():
-        ui.label(f'individual page with uuid {uuid4()}')
+        ui.label(f'private page with uuid {uuid4()}')
 
-    @ui.page('/shared_page', shared=True)
-    def shared_page():
-        ui.label(f'shared page with uuid {uuid4()}')
+    ui.label(f'shared page with uuid {uuid4()}')
 
-    screen.open('/shared_page')
+    screen.open('/private_page')
+    uuid1 = screen.find('private page').text.split()[-1]
+    screen.open('/private_page')
+    uuid2 = screen.find('private page').text.split()[-1]
+    assert uuid1 != uuid2
+
+    screen.open('/')
     uuid1 = screen.find('shared page').text.split()[-1]
-    screen.open('/shared_page')
+    screen.open('/')
     uuid2 = screen.find('shared page').text.split()[-1]
     assert uuid1 == uuid2
 
-    screen.open('/individual_page')
-    uuid1 = screen.find('individual page').text.split()[-1]
-    screen.open('/individual_page')
-    uuid2 = screen.find('individual page').text.split()[-1]
-    assert uuid1 != uuid2
-
 
 def test_on_page_ready_event(screen: Screen):
     '''This feature was introduced to fix #50; see https://github.com/zauberzeug/nicegui/issues/50#issuecomment-1210962617.'''
@@ -172,17 +160,6 @@ def test_customized_page(screen: Screen):
     assert trace == ['init', 'connected', 'before_content', 'content', 'after_content']
 
 
-def test_shared_page_with_request_parameter_raises_exception(screen: Screen):
-    @ui.page('/', shared=True)
-    def page(request: Request):
-        ui.label('Hello, world!')
-
-    screen.open('/')
-    screen.should_contain('500')
-    screen.should_contain('Server error')
-    screen.assert_py_logger('ERROR', 'Cannot use `request` argument in shared page')
-
-
 def test_adding_elements_in_on_page_ready_event(screen: Screen):
     @ui.page('/', on_page_ready=lambda: ui.markdown('Hello, world!'))
     def page():
@@ -223,17 +200,6 @@ def test_pageready_after_yield_on_non_async_page(screen: Screen):
     screen.should_contain('ws://localhost:3392/')
 
 
-def test_pageready_after_yield_on_shared_page_raises_exception(screen: Screen):
-    @ui.page('/', shared=True)
-    def page():
-        yield
-
-    screen.open('/')
-    screen.should_contain('500')
-    screen.should_contain('Server error')
-    screen.assert_py_logger('ERROR', 'Yielding for page_ready is not supported on shared pages')
-
-
 def test_exception_before_yield_on_async_page(screen: Screen):
     @ui.page('/')
     async def page() -> Generator[None, PageEvent, None]:

+ 10 - 18
website/api_docs_and_examples.py

@@ -575,34 +575,26 @@ Note: You can also pass a `functools.partial` into the `on_click` property to wr
         ui.link('Visit other page', other_page)
         ui.link('Visit dark page', dark_page)
 
-    @example('''#### Shared and Private Pages
+    @example('''#### Auto-index page
 
-By default, pages created with the `@ui.page` decorator are "private".
+Pages created with the `@ui.page` decorator are "private".
 Their content is re-created for each client.
-Thus, in the example to the right, the displayed ID changes when the browser reloads the page.
+Thus, in the example to the right, the displayed ID on the private page changes when the browser reloads the page.
 
-With `shared=True` you can create a shared page.
-Its content is created once at startup and each client sees the *same* elements.
-Here, the displayed ID remains constant when the browser reloads the page.
-
-#### Index Page
-
-All elements that are not created within a decorated page function are automatically added to a new, *shared* index page at route "/".
-To make it "private" or to change other attributes like title, favicon etc. you can wrap it in a page function with `@ui.page('/', ...)` decorator.
-''', skip=True)
-    def shared_and_private_pages_example():
+UI elements that are not wrapped in a decorated page function are placed on an automatically generated index page at route "/".
+This auto-index page is created once on startup and *shared* across all clients that might connect.
+Thus, each connected client will see the *same* elements.
+In the example to the right, the displayed ID on the auto-index page remains constant when the browser reloads the page.
+''')
+    def auto_index_page():
         from uuid import uuid4
 
         @ui.page('/private_page')
         async def private_page():
             ui.label(f'private page with ID {uuid4()}')
 
-        @ui.page('/shared_page', shared=True)
-        async def shared_page():
-            ui.label(f'shared page with ID {uuid4()}')
-
+        ui.label(f'shared auto-index page with ID {uuid4()}')
         ui.link('private page', private_page)
-        ui.link('shared page', shared_page)
 
     @example('''#### Pages with Path Parameters