This repository was archived by the owner on Apr 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute.py
More file actions
58 lines (41 loc) · 1.57 KB
/
route.py
File metadata and controls
58 lines (41 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Manage routing for the application."""
import dataclasses
import inspect
from collections.abc import Callable
import reflex as rx
from reflex.event import EventType
@dataclasses.dataclass(kw_only=True)
class Route:
"""A page route."""
# The path of the route.
path: str
# The page title.
title: str | rx.Var | None = None
# The page description.
description: str | None = None
# The page image.
image: str | None = None
# The page extra meta data.
meta: list[dict[str, str]] | None = None
# Background color for the page.
background_color: str | None = None
# The component to render for the route.
component: Callable[[], rx.Component]
# whether to add the route to the app's pages. This is typically used
# to delay adding the 404 page(which is explicitly added in reflex_blog.py).
# https://github.com/reflex-dev/reflex-web/pull/659#pullrequestreview-2021171902
add_as_page: bool = True
# The on_load function to call when the page is loaded.
on_load: EventType[()] | None = None
def get_path(component_fn: Callable, route: str):
"""Get the path for a page based on the file location.
Args:
component_fn: The component function for the page.
route: The route path for the page.
"""
module = inspect.getmodule(component_fn)
if module is None:
msg = f"Could not find module for {component_fn}"
raise ValueError(msg)
# Create a path based on the module name.
return module.__name__.replace(".", "/").replace("_", "-").split(route)[1] + "/"