Skip to content

Commit 4c588c1

Browse files
committed
init
0 parents  commit 4c588c1

9 files changed

Lines changed: 1851 additions & 0 deletions

File tree

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Python
2+
__pycache__/
3+
*.pyc
4+
*.pyo
5+
*.pyd
6+
7+
# Poetry
8+
.venv/
9+
.poetry/
10+
.poetry.lock
11+
12+
# IDEs and editors
13+
.vscode/
14+
.idea/
15+
*.sublime-project
16+
*.sublime-workspace
17+
18+
# Logs
19+
*.log
20+
21+
# Build files
22+
build/
23+
dist/
24+
*.egg-info/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 50Bytes-dev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Flet Router
2+
3+
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
4+
5+
## Overview
6+
7+
Flet Router is a lightweight and flexible routing library for Flet (Python). It provides a simple and intuitive way to define and handle routes in your Flet applications.
8+
9+
## Features
10+
11+
- Easy route definition using decorators
12+
- URL parameter extraction
13+
- Middleware support
14+
- Route grouping and prefixing
15+
16+
## Installation
17+
18+
You can install Flet Router using pip:
19+
pip install git+https://github.com/50Bytes/flet-router.git

poetry.lock

Lines changed: 1270 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[tool.poetry]
2+
name = "flet-router"
3+
version = "0.1.0"
4+
description = "Simple and fast router for Flet"
5+
authors = ["50bytes.dev <50bytes.dev@gmail.com>"]
6+
license = "MIT"
7+
readme = "README.md"
8+
packages = [{include = "src"}]
9+
10+
[tool.poetry.dependencies]
11+
python = ">=3.8,<4.0"
12+
flet = {path = "../flet/sdk/python/packages/flet", develop = true}
13+
flet-core = {path = "../flet/sdk/python/packages/flet-core", develop = true}
14+
flet-runtime = {path = "../flet/sdk/python/packages/flet-runtime", develop = true}
15+
16+
[tool.poetry.dev-dependencies]
17+
uvicorn = "^0.27.1"
18+
pydantic = "^2.6.2"
19+
20+
[build-system]
21+
requires = ["poetry-core"]
22+
build-backend = "poetry.core.masonry.api"

samples/app.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import flet as ft
2+
import src as fr
3+
4+
router = fr.FletRouter(
5+
prefix="/app",
6+
)
7+
8+
9+
@router.route(
10+
name="home_page",
11+
)
12+
async def home_page(
13+
router: fr.FletRouter,
14+
page: ft.Page,
15+
):
16+
17+
def on_click_1(e):
18+
router.go_push("/app/second/123/value?query_variable=hello")
19+
20+
def on_click_2(e):
21+
router.go_push(
22+
{
23+
"name": "second_page",
24+
"params": {"variable": 123},
25+
"query": {"query_variable": "hello"},
26+
}
27+
)
28+
29+
def on_click_3(e):
30+
router.go_push(
31+
fr.Location(
32+
name="second_page",
33+
params={"variable": 123},
34+
query={"query_variable": "hello"},
35+
)
36+
)
37+
38+
def on_click_4(e):
39+
router.go_push("/app/protected")
40+
41+
def on_change(e: ft.ControlEvent):
42+
page.session.set("allow_access", e.data == "true")
43+
44+
return ft.View(
45+
controls=[
46+
ft.ElevatedButton("Go to second page by path", on_click=on_click_1),
47+
ft.ElevatedButton("Go to second page by dict", on_click=on_click_2),
48+
ft.ElevatedButton("Go to second page by Location", on_click=on_click_3),
49+
ft.ElevatedButton("Go to protected page", on_click=on_click_4),
50+
ft.Switch(
51+
label="Allow access to protected page",
52+
on_change=on_change,
53+
value=page.session.get("allow_access"),
54+
),
55+
],
56+
)
57+
58+
59+
@router.route(
60+
name="second_page",
61+
path="/second/{variable}/value",
62+
)
63+
async def second_page(
64+
router: fr.FletRouter,
65+
variable: int,
66+
query_variable: str = "Not defined",
67+
):
68+
69+
def on_back(e):
70+
router.back()
71+
72+
def on_click(e):
73+
router.go_push("/app/protected")
74+
75+
return ft.View(
76+
controls=[
77+
ft.Text("Second page"),
78+
ft.Text(f"Variable: {type(variable)}, {variable}"),
79+
ft.Text(f"Query Variable: {query_variable}"),
80+
ft.ElevatedButton("Go back", on_click=on_back),
81+
ft.ElevatedButton("Go to protected page", on_click=on_click),
82+
],
83+
)
84+
85+
86+
async def protected_middleware(
87+
page: ft.Page,
88+
):
89+
if page.session.get("allow_access"):
90+
return True
91+
else:
92+
return False
93+
94+
95+
@router.route(
96+
name="protected_page",
97+
path="/protected",
98+
middlewares=[protected_middleware],
99+
)
100+
async def protected_page(
101+
router: fr.FletRouter,
102+
page: ft.Page,
103+
):
104+
105+
def on_click(e):
106+
router.back()
107+
108+
return ft.View(
109+
controls=[
110+
ft.Text(f"Session ID: {page.session_id}"),
111+
ft.Text("Protected page"),
112+
ft.ElevatedButton("Go back", on_click=on_click),
113+
],
114+
)
115+
116+
117+
async def main(page: ft.Page):
118+
page.theme = ft.Theme(
119+
page_transitions=ft.PageTransitionsTheme(
120+
android=ft.PageTransitionTheme.NONE,
121+
ios=ft.PageTransitionTheme.NONE,
122+
linux=ft.PageTransitionTheme.NONE,
123+
macos=ft.PageTransitionTheme.NONE,
124+
windows=ft.PageTransitionTheme.NONE,
125+
)
126+
)
127+
128+
fr.FletRouter.mount(
129+
page,
130+
routes=router.routes,
131+
)
132+
133+
134+
app = ft.app(main, export_asgi_app=True)
135+
136+
if __name__ == "__main__":
137+
import uvicorn
138+
139+
uvicorn.run(
140+
"app:app",
141+
host="0.0.0.0",
142+
port=8550,
143+
reload=True,
144+
)

src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .routing import FletRouter, FletRoute, Location

0 commit comments

Comments
 (0)