1

I want to build a chat app with flet in python.

For routing I defined a Router class which handles all the routing. But I am not able to figure a way out for implementing dynamic routes such as "/:username".

How can I acheive dynamic routing in flet without introducing a third party dependency?

router.py

import flet as ft

from routes.login import LoginPage
from routes.register import RegisterPage
from routes.home import HomePage
from routes.user_profile import UserProfilePage

class Router:
    def __init__(self, page: ft.Page):
        self.page = page
        self.routes = {
            "/login": LoginPage(page),
            "/register": RegisterPage(page),
            "/": HomePage(page),
            "/:username": UserProfilePage(page)
        }
        self.body = ft.Container(
            content=self.routes["/login"]
        )

    def change_route(self, event: ft.RouteChangeEvent):
        self.body.content = self.routes[event.route]
        self.body.content.init_page()
        self.page.update()

1 Answer 1

0

You will find this community package (flet-route) very useful when dealing with - complex - routing in flet. It has no additional requirement... just Flet (the repath lib mentioned in its repo is actually a Flet requirement).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.