Skip to content

Commit

Permalink
Fixed quotes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alyxion committed May 10, 2024
1 parent 04be96a commit a112465
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
19 changes: 10 additions & 9 deletions nicegui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@ async def decorated(*dec_args, **dec_kwargs) -> Response:
dec_kwargs['client'] = client
if any(p.name == 'request_data' for p in inspect.signature(func).parameters.values()):
url = request.url
dec_kwargs['request_data'] = {"client":
{"host": request.client.host,
"port": request.client.port},
"cookies": request.cookies,
"url":
{"path": url.path,
"query": url.query,
"username": url.username, "password": url.password,
"fragment": url.fragment}}
dec_kwargs['request_data'] = {'client':
{'host': request.client.host,
'port': request.client.port},
'cookies': request.cookies,
'url':
{'path': url.path,
'query': url.query,
'username': url.username,
'password': url.password,
'fragment': url.fragment}}
result = func(*dec_args, **dec_kwargs)
if helpers.is_coroutine_function(func):
async def wait_for_result() -> None:
Expand Down
46 changes: 19 additions & 27 deletions nicegui/single_page_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def __init__(self, path: str, builder: Callable, title: Union[str, None] = None)
"""
:param path: The path of the route
:param builder: The builder function which is called when the route is opened
:param title: Optional title of the page
"""
:param title: Optional title of the page"""
self.path = path
self.builder = builder
self.title = title
Expand All @@ -42,8 +41,7 @@ def create_path_mask(path: str) -> str:
/site/{value}/{other_value} --> /site/*/*
:param path: The path to convert
:return: The mask with all path parameters replaced by a wildcard
"""
:return: The mask with all path parameters replaced by a wildcard"""
return re.sub(r'{[^}]+}', '*', path)


Expand All @@ -60,16 +58,14 @@ def __init__(self,
page_template: Optional[Callable[[], Generator]] = None,
on_instance_created: Optional[Callable] = None,
**kwargs) -> None:
"""
:param path: the base path of the single page router.
""":param path: the base path of the single page router.
:param browser_history: Optional flag to enable or disable the browser history management. Default is True.
:param parent: The parent router of this router if this router is a nested router.
:param page_template: Optional page template generator function which defines the layout of the page. It
needs to yield a value to separate the layout from the content area.
:param on_instance_created: Optional callback which is called when a new instance is created. Each browser tab
or window is a new instance. This can be used to initialize the state of the application.
:param kwargs: Additional arguments for the @page decorators
"""
:param kwargs: Additional arguments for the @page decorators"""
super().__init__()
self.routes: Dict[str, SinglePageRouterEntry] = {}
self.base_path = path
Expand All @@ -82,17 +78,17 @@ def __init__(self,
self.parent_router = parent
if self.parent_router is not None:
self.parent_router._register_child_router(self)
self.child_routers: List["SinglePageRouter"] = []
self.child_routers: List['SinglePageRouter'] = []
self.page_kwargs = kwargs

def setup_pages(self, force=False) -> Self:
for key, route in Client.page_routes.items():
if route.startswith(
self.base_path.rstrip("/") + "/") and route.rstrip("/") not in self.included_paths:
self.base_path.rstrip('/') + '/') and route.rstrip('/') not in self.included_paths:
self.excluded_paths.add(route)
if force:
continue
if self.base_path.startswith(route.rstrip("/") + "/"): # '/sub_router' after '/' - forbidden
if self.base_path.startswith(route.rstrip('/') + '/'): # '/sub_router' after '/' - forbidden
raise ValueError(f'Another router with path "{route.rstrip("/")}/*" is already registered which '
f'includes this router\'s base path "{self.base_path}". You can declare the nested '
f'router first to prioritize it and avoid this issue.')
Expand All @@ -102,10 +98,10 @@ def setup_pages(self, force=False) -> Self:
async def root_page(request_data=None):
initial_url = None
if request_data is not None:
initial_url = request_data["url"]["path"]
query = request_data["url"].get("query", {})
initial_url = request_data['url']['path']
query = request_data['url'].get('query', {})
if query:
initial_url += "?" + query
initial_url += '?' + query
self.build_page(initial_url=initial_url)

return self
Expand All @@ -115,42 +111,39 @@ def add_view(self, path: str, builder: Callable, title: Optional[str] = None) ->
:param path: The path of the route, including FastAPI path parameters
:param builder: The builder function (the view to be displayed)
:param title: Optional title of the page
"""
:param title: Optional title of the page"""
path_mask = SinglePageRouterEntry.create_path_mask(path.rstrip('/'))
self.included_paths.add(path_mask)
self.routes[path] = SinglePageRouterEntry(path, builder, title).verify()

def add_router_entry(self, entry: SinglePageRouterEntry) -> None:
"""Adds a fully configured SinglePageRouterEntry to the router
:param entry: The SinglePageRouterEntry to add
"""
:param entry: The SinglePageRouterEntry to add"""
self.routes[entry.path] = entry.verify()

def resolve_target(self, target: Union[Callable, str]) -> SinglePageTarget:
"""Tries to resolve a target such as a builder function or an URL path w/ route and query parameters.
:param target: The URL path to open or a builder function
:return: The resolved target. Defines .valid if the target is valid
"""
:return: The resolved target. Defines .valid if the target is valid"""
if isinstance(target, Callable):
for target, entry in self.routes.items():
if entry.builder == target:
return SinglePageTarget(entry=entry, router=self)
else:
resolved = None
path = target.split("#")[0].split("?")[0]
path = target.split('#')[0].split('?')[0]
for cur_router in self.child_routers:
# replace {} placeholders with * to match the fnmatch pattern
mask = SinglePageRouterEntry.create_path_mask(cur_router.base_path.rstrip("/") + "/*")
mask = SinglePageRouterEntry.create_path_mask(cur_router.base_path.rstrip('/') + '/*')
if fnmatch(path, mask) or path == cur_router.base_path:
resolved = cur_router.resolve_target(target)
if resolved.valid:
target = cur_router.base_path
if "*" in mask:
if '*' in mask:
# isolate the real path elements and update target accordingly
target = "/".join(path.split("/")[:len(cur_router.base_path.split("/"))])
target = '/'.join(path.split('/')[:len(cur_router.base_path.split('/'))])
break
result = SinglePageTarget(target, router=self).parse_url_path(routes=self.routes)
if resolved is not None:
Expand All @@ -161,8 +154,7 @@ def navigate_to(self, target: Union[Callable, str, SinglePageTarget], server_sid
"""Navigate to a target
:param target: The target to navigate to
:param server_side: Optional flag which defines if the call is originated on the server side
"""
:param server_side: Optional flag which defines if the call is originated on the server side"""
org_target = target
if not isinstance(target, SinglePageTarget):
target = self.resolve_target(target)
Expand Down Expand Up @@ -228,6 +220,6 @@ def insert_content_area(self,
content.navigate_to(initial_url, _server_side=False, sync=True)
return content

def _register_child_router(self, router: "SinglePageRouter") -> None:
def _register_child_router(self, router: 'SinglePageRouter') -> None:
"""Registers a child router to the parent router"""
self.child_routers.append(router)

0 comments on commit a112465

Please sign in to comment.