diff --git a/examples/python/app.py b/examples/python/app.py index 9d3a800..2f15bfb 100644 --- a/examples/python/app.py +++ b/examples/python/app.py @@ -1,5 +1,5 @@ -from shiny import App, ui -from shiny_router import route_link, router_ui, route, router_server +from shiny import App, ui, render, reactive +from shiny_router import route_link, router_ui, route, router_server, get_query_param tags = ui.tags @@ -25,6 +25,7 @@ def page(title, content): # Make output for our router in main UI of Shiny app. app_ui = ui.page_fluid( + ui.output_ui("param"), router_ui( route("/", root_page), route("other", other_page), @@ -36,4 +37,14 @@ def page(title, content): def server(input, output, session): router_server(input, output, session) + @render.ui + def param(): + query = get_query_param(session = session) + id = get_query_param("id", session = session) + return ui.TagList( + ui.p("No query" if not query else str(query)), + ui.p("No id" if not id else str(id)), + ) + + app = App(app_ui, server) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index bbc5e57..2de1101 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "shiny_router" -version = "0.1.3" +version = "0.1.4" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" diff --git a/src/shiny_router/__init__.py b/src/shiny_router/__init__.py index 52ba3ea..8dd6301 100644 --- a/src/shiny_router/__init__.py +++ b/src/shiny_router/__init__.py @@ -1,4 +1 @@ -from .router import route_link, router_ui, route, router_server - -def hello() -> str: - return "Hello from shiny_router!" +from .router import route_link, router_ui, route, router_server, get_query_param diff --git a/src/shiny_router/router.py b/src/shiny_router/router.py index fff116b..808ac0a 100644 --- a/src/shiny_router/router.py +++ b/src/shiny_router/router.py @@ -1,4 +1,4 @@ -from shiny import ui, reactive +from shiny import ui, reactive, session from htmltools import HTMLDependency from pathlib import PurePath from urllib.parse import urlparse, parse_qs @@ -6,6 +6,15 @@ log_msg = print PAGE_404_ROUTE = "404" +def get_query_param(field = None, session = session.get_current_session()): + page_details = session.input.shiny_router_page() + + if page_details: + if field in page_details["query"]: + return page_details["query"][field][0] + else: + return page_details["query"] + def page404(page=None, message404=None): if page is None: # Return a default "Not found" message or a custom 404 message @@ -34,7 +43,7 @@ def router_callback(input, output, session=None, **kwargs): input.shiny_router_page = reactive.value(dict( path = root, - query = None, + query = {}, unparsed = root )) diff --git a/uv.lock b/uv.lock index ceb8609..816abf6 100644 --- a/uv.lock +++ b/uv.lock @@ -339,7 +339,7 @@ wheels = [ [[distribution]] name = "shiny-router" -version = "0.1.2" +version = "0.1.3" source = { editable = "." } dependencies = [ { name = "pandas" },