Skip to content

Commit e69d41b

Browse files
Inject request object if root page wants it (zauberzeug#5263)
### Motivation Root page functions should support the same parameter injection patterns as regular `@ui.page()` decorated functions for consistency and to provide access to request details (headers, cookies, URL components, etc.). ### Implementation Reuse existing page._wrap() infrastructure rather than duplicating logic. The wrapped page function already inspects signatures and injects request when present, so we simply avoid conflicting with it. There currently is no proper documentation location for root page specifics. Therefor I decided to not do this in this PR (simplifies review) and ideally add another PR later to do comprehensive documentation for root page. ### Progress - [x] I chose a meaningful title that completes the sentence: "If applied, this PR will..." - [x] The implementation is complete. - [x] Pytests have been added. - [x] Documentation will be added in a separate PR. --------- Co-authored-by: Falko Schindler <[email protected]>
1 parent f3d2c9a commit e69d41b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

nicegui/nicegui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ async def _exception_handler_404(request: Request, exception: Exception) -> Resp
166166
param.annotation,
167167
)
168168
for name, param in inspect.signature(root).parameters.items()
169-
if name in request.query_params
169+
if name in request.query_params and name != 'request'
170170
}
171171
return await page('')._wrap(root)(request=request, **kwargs) # pylint: disable=protected-access
172172
log.warning(f'{request.url} not found')

tests/test_root_page.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,20 @@ def root(x: int = 0):
3333

3434
screen.open('/?x=42')
3535
screen.should_contain('x = 42')
36+
37+
38+
def test_root_page_with_request_injection(screen: Screen):
39+
from fastapi import Request
40+
41+
def root(request: Request):
42+
ui.label(f'path: {request.url.path}')
43+
ui.label(f'name: {request.query_params.get("name", "unknown")}')
44+
45+
screen.ui_run_kwargs['root'] = root
46+
screen.open('/')
47+
screen.should_contain('path: /')
48+
screen.should_contain('name: unknown')
49+
50+
screen.open('/?name=test')
51+
screen.should_contain('path: /')
52+
screen.should_contain('name: test')

0 commit comments

Comments
 (0)