|
| 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 | + ) |
0 commit comments