-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d79831
commit 88ec72f
Showing
11 changed files
with
150 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import ClassVar | ||
|
||
from reactpy_router.resolvers import ConversionInfo, ReactPyResolver | ||
|
||
|
||
# Create a custom resolver that uses the following pattern: "{name:type}" | ||
class CustomResolver(ReactPyResolver): | ||
# Match parameters that use the "<name:type>" format | ||
param_pattern: str = r"<(?P<name>\w+)(?P<type>:\w+)?>" | ||
|
||
# Enable matching for the following types: int, str, any | ||
converters: ClassVar[dict[str, ConversionInfo]] = { | ||
"int": ConversionInfo(regex=r"\d+", func=int), | ||
"str": ConversionInfo(regex=r"[^/]+", func=str), | ||
"any": ConversionInfo(regex=r".*", func=str), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from example.resolvers import CustomResolver | ||
|
||
from reactpy_router.routers import create_router | ||
|
||
# This can be used in any location where `browser_router` was previously used | ||
custom_router = create_router(CustomResolver) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from reactpy_router.resolvers import ReactPyResolver | ||
|
||
|
||
class CustomResolver(ReactPyResolver): ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
# Custom Router | ||
Custom routers can be used to define custom routing logic for your application. This is useful when you need to implement a custom routing algorithm or when you need to integrate with an existing URL routing system. | ||
|
||
Under construction 🚧 | ||
--- | ||
|
||
## Step 1: Creating a custom resolver | ||
|
||
You may want to create a custom resolver to allow ReactPy to utilize an existing routing syntax. | ||
|
||
To start off, you will need to create a subclass of `#!python ReactPyResolver`. Within this subclass, you have two attributes which you can modify to support your custom routing syntax: | ||
|
||
- `#!python param_pattern`: A regular expression pattern that matches the parameters in your URL. This pattern must contain the regex named groups `name` and `type`. | ||
- `#!python converters`: A dictionary that maps a `type` to it's respective `regex` pattern and a converter `func`. | ||
|
||
=== "resolver.py" | ||
|
||
```python | ||
{% include "../../examples/python/custom_router_easy_resolver.py" %} | ||
``` | ||
|
||
## Step 2: Creating a custom router | ||
|
||
Then, you can use this resolver to create your custom router... | ||
|
||
=== "resolver.py" | ||
|
||
```python | ||
{% include "../../examples/python/custom_router_easy_router.py" %} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.