Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Implement redirecting in solara routing #576

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions solara/autorouting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
solara_root = Path(solara.__file__).parent

DefaultLayout = solara.AppLayout
_redirects: Dict[str, str] = {}


def source_to_module(path: Path, initial_namespace={}) -> ModuleType:
Expand Down Expand Up @@ -89,12 +90,17 @@ def arg_cast(args: List[str], f: Callable):
def RoutingProvider(children: List[reacton.core.Element] = [], routes: List[solara.Route] = [], pathname: str = ""):
"""Wraps the app, adds extra context, like navigation/routing."""
path, set_path = solara.use_state(pathname, key="solara-context-path")

def set_path_with_redirect(path):
path = _redirects.get(path, path)
set_path(path)

# TODO: since we provide a cross filter context here, I don't think name `RoutingProvider` is a good name
# we might want to change/refactor this.
solara.provide_cross_filter()
nav = solara.Navigator(location=path, on_location=set_path)
solara.routing._location_context.provide(solara.routing._Location(path, set_path))
solara.routing.router_context.provide(solara.routing.Router(path, routes=routes, set_path=set_path))
nav = solara.Navigator(location=path, on_location=set_path_with_redirect)
solara.routing._location_context.provide(solara.routing._Location(path, set_path_with_redirect))
solara.routing.router_context.provide(solara.routing.Router(path, routes=routes, set_path=set_path_with_redirect))

def get_nav_widget():
# not sure why get_widget(nav) does not work
Expand Down
25 changes: 18 additions & 7 deletions solara/website/pages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib

import solara
from solara import autorouting
from solara.alias import rv
from solara.components.title import Title
from solara.server import server
Expand All @@ -13,7 +14,7 @@
route_order = ["/", "showcase", "documentation", "apps", "contact", "changelog"]


server._redirects = {
_redirects = {
"/docs": "/documentation/getting_started/introduction",
"/docs/installing": "/documentation/getting_started/installing",
"/docs/quickstart": "/documentation/getting_started",
Expand Down Expand Up @@ -111,12 +112,18 @@
"/api/use_thread": "/documentation/api/hooks/use_thread",
"/api/use_trait_observe": "/documentation/api/hooks/use_trait_observe",
"/examples/fullscreen": "/documentation/examples/fullscreen",
"/examples/fullscreen/authorization": "/documentation/examples/fullscreen/authorization",
"/examples/fullscreen/layout_demo": "/documentation/examples/fullscreen/layout_demo",
"/examples/fullscreen/multipage": "/documentation/examples/fullscreen/multipage",
"/examples/fullscreen/scatter": "/documentation/examples/fullscreen/scatter",
"/examples/fullscreen/scrolling": "/documentation/examples/fullscreen/scrolling",
"/examples/fullscreen/tutorial_streamlit": "/documentation/examples/fullscreen/tutorial_streamlit",
"/examples/fullscreen/authorization": "/apps/authorization",
"documentation/examples/fullscreen/authorization": "/apps/authorization",
"/examples/fullscreen/layout-demo": "/apps/layout-demo",
"/documentation/examples/fullscreen/layout-demo": "/apps/layout-demo",
"/examples/fullscreen/multipage": "/apps/multipage",
"/documentation/examples/fullscreen/multipage": "/apps/multipage",
"/examples/fullscreen/scatter": "apps/scatter",
"/documentation/examples/fullscreen/scatter": "/apps/scatter",
"/examples/fullscreen/scrolling": "/apps/scrolling",
"/documentation/examples/fullscreen/scrolling": "/apps/scrolling",
"/examples/fullscreen/tutorial-streamlit": "/apps/tutorial-streamlit",
"/documentation/examples/fullscreen/tutorial-streamlit": "/apps/tutorial-streamlit",
"/api/route": "/documentation/api/routing/route",
"/api/route/kiwi": "/documentation/api/routing/route/kiwi",
"/api/route/banana": "/documentation/api/routing/route/banana",
Expand Down Expand Up @@ -207,6 +214,10 @@
}


server._redirects = _redirects
autorouting._redirects = _redirects


@solara.component
def Page():
solara.Markdown("should not see me")
Expand Down
Loading