Code Monkey home page Code Monkey logo

Comments (16)

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

Hi. Thanks for reporting the error. Can you please show me which path you used for this route?

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

Hi. Thanks for reporting the error. Can you please show me which path you used for this route?

i just try you code example

https://github.com/50Bytes-dev/flet-router/blob/main/samples/app.py

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

with flet version this

C:\Users\LENOVO>flet --version
0.21.2

from flet-router.

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

with flet version this

C:\Users\LENOVO>flet --version 0.21.2

Which python version?

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

with flet version this
C:\Users\LENOVO>flet --version 0.21.2

Which python version?

C:\Users\LENOVO>python --version
Python 3.11.7

from flet-router.

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

Corrected. Try it now. Just make sure you've installed the latest commit from the repository

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

why still not work . i paste again
pip install git+https://github.com/50Bytes-dev/flet-router.git

but still erorr

C:\Users\LENOVO\Pictures\mainan\fletaps\fletcon>flet main.py
Traceback (most recent call last):
  File "C:\Users\LENOVO\Pictures\mainan\fletaps\fletcon\main.py", line 59, in <module>
    @router.route(
     ^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\flet_router\routing.py", line 165, in decorator
    self.add_route(
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\flet_router\routing.py", line 148, in add_route
    route = FletRoute(
            ^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\flet_router\routing.py", line 49, in __init__
    self.pattern = re.compile(
                   ^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\__init__.py", line 227, in compile
    return _compile(pattern, flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\__init__.py", line 294, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\_compiler.py", line 745, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\_parser.py", line 994, in parse
    raise source.error("unbalanced parenthesis")
re.error: unbalanced parenthesis at position 31

from flet-router.

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

Use hash code for install last commit
https://stackoverflow.com/questions/13685920/install-specific-git-commit-with-pip

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

i try install again
pip install git+https://github.com/50Bytes-dev/flet-router.git@db9e1ef145f8e031bd8be968b2e02b9dec344079

and run you code sample but i get error


C:\Users\LENOVO\Pictures\fletapp\ft>flet main.py
Traceback (most recent call last):
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\flet_router\routing.py", line 51, in __init__
    self.pattern = re.compile("^" + patter_path + "$")
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\__init__.py", line 227, in compile
    return _compile(pattern, flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\__init__.py", line 294, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\_compiler.py", line 745, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\re\_parser.py", line 994, in parse
    raise source.error("unbalanced parenthesis")
re.error: unbalanced parenthesis at position 31

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\LENOVO\Pictures\fletapp\ft\main.py", line 59, in <module>
    @router.route(
     ^^^^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\flet_router\routing.py", line 170, in decorator
    self.add_route(
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\flet_router\routing.py", line 153, in add_route
    route = FletRoute(
            ^^^^^^^^^^
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\flet_router\routing.py", line 53, in __init__
    raise ValueError(f"Invalid path pattern: {path}")
ValueError: Invalid path pattern: \app\second\{variable}\value

code

import flet as ft
import flet_router as fr

router = fr.FletRouter(
    prefix="/app",
)


@router.route(
    name="home_page",
)
async def home_page(
    router: fr.FletRouter,
    page: ft.Page,
):

    def on_click_1(e):
        router.go_push("/app/second/123/value?query_variable=hello")

    def on_click_2(e):
        router.go_push(
            {
                "name": "second_page",
                "params": {"variable": 123},
                "query": {"query_variable": "hello"},
            }
        )

    def on_click_3(e):
        router.go_push(
            fr.Location(
                name="second_page",
                params={"variable": 123},
                query={"query_variable": "hello"},
            )
        )

    def on_click_4(e):
        router.go_push("/app/protected")

    def on_change(e: ft.ControlEvent):
        page.session.set("allow_access", e.data == "true")

    return ft.View(
        controls=[
            ft.ElevatedButton("Go to second page by path", on_click=on_click_1),
            ft.ElevatedButton("Go to second page by dict", on_click=on_click_2),
            ft.ElevatedButton("Go to second page by Location", on_click=on_click_3),
            ft.ElevatedButton("Go to protected page", on_click=on_click_4),
            ft.Switch(
                label="Allow access to protected page",
                on_change=on_change,
                value=page.session.get("allow_access"),
            ),
        ],
    )


@router.route(
    name="second_page",
    path="/second/{variable}/value",
)
async def second_page(
    router: fr.FletRouter,
    variable: int,
    query_variable: str = "Not defined",
):

    def on_back(e):
        router.back()

    def on_click(e):
        router.go_push("/app/protected")

    return ft.View(
        controls=[
            ft.Text("Second page"),
            ft.Text(f"Variable: {type(variable)}, {variable}"),
            ft.Text(f"Query Variable: {query_variable}"),
            ft.ElevatedButton("Go back", on_click=on_back),
            ft.ElevatedButton("Go to protected page", on_click=on_click),
        ],
    )


async def protected_middleware(
    from_route: fr.FletRoute,
    to_route: fr.FletRoute,
    page: ft.Page,
):
    if page.session.get("allow_access"):
        return True
    else:
        if from_route.name == "home_page":
            return False
        elif from_route.name == "second_page":
            return fr.Location(name="home_page")


@router.route(
    name="protected_page",
    path="/protected",
    middlewares=[protected_middleware],
)
async def protected_page(
    router: fr.FletRouter,
    page: ft.Page,
):

    def on_click(e):
        router.back()

    return ft.View(
        controls=[
            ft.Text(f"Session ID: {page.session_id}"),
            ft.Text("Protected page"),
            ft.ElevatedButton("Go back", on_click=on_click),
        ],
    )


async def main(page: ft.Page):
    page.theme = ft.Theme(
        page_transitions=ft.PageTransitionsTheme(
            android=ft.PageTransitionTheme.NONE,
            ios=ft.PageTransitionTheme.NONE,
            linux=ft.PageTransitionTheme.NONE,
            macos=ft.PageTransitionTheme.NONE,
            windows=ft.PageTransitionTheme.NONE,
        )
    )

    app_router = fr.FletRouter.mount(
        page,
        routes=router.routes,
    )

    app_router.go_root("/app")


app = ft.app(main, export_asgi_app=True)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "app:app",
        host="0.0.0.0",
        port=8550,
        reload=True,
    )

from flet-router.

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

Fixed it. I didn't think you had Windows, so I couldn't understand why you had an error there.

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

no error . but white screen no text or button

Screenshot 2024-04-02 112932

from flet-router.

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

Open the /app path. In the example, this is the start path. Or change it

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

white screen . I just copy pasted the example code. but it's a white screen

Screenshot 2024-04-02 122045

from flet-router.

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

Fixed url path builder. Check it please

from flet-router.

bobwatcherx avatar bobwatcherx commented on June 12, 2024

how to set 404 not found route . is there example

from flet-router.

50Bytes-dev avatar 50Bytes-dev commented on June 12, 2024

how to set 404 not found route . is there example

Add the last route as a regular expression. Something like @router.route(path=".*")

from flet-router.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.