Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with pages being read twice #14

Closed
EasyDevv opened this issue Apr 21, 2024 · 6 comments
Closed

Issues with pages being read twice #14

EasyDevv opened this issue Apr 21, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@EasyDevv
Copy link

EasyDevv commented Apr 21, 2024

code :

import flet as ft
import flet_easy as fs

app = fs.FletEasy(route_init="/login", route_login="/login")

test_int = 0


@app.page(route="/login", title="Login")
def test_page(data: fs.Datasy):
    global test_int
    test_int += 1

    print(f"test_int: {test_int}")

    return ft.View(
        controls=[
            ft.Text(test_int, size=30),
        ],
        vertical_alignment=ft.MainAxisAlignment.CENTER,
        horizontal_alignment=ft.CrossAxisAlignment.CENTER,
    )


app.run()

result :

test_int: 1
test_int: 2

requirements:

flet==0.22.0
flet-easy==0.2.0

os

windows 11

When I loaded the page, I noticed that the log was called twice,
so I checked with my test code. Is this a bug?

@Daxexs
Copy link
Owner

Daxexs commented Apr 22, 2024

It is a bug that occurs in non-async functions, thanks for reporting it. If another bug is found, please report it so we can add the fix along with it.

Daxexs added a commit that referenced this issue Apr 22, 2024
@Daxexs
Copy link
Owner

Daxexs commented Apr 22, 2024

Fixed at https://pypi.org/project/flet-easy/0.2.1.dev10/

@EasyDevv
Copy link
Author

EasyDevv commented Apr 22, 2024

It works fine. Thank you

Fixed at https://pypi.org/project/flet-easy/0.2.1.dev10/

@jundaychan
Copy link

Fixed at https://pypi.org/project/flet-easy/0.2.1.dev10/修复于 https://pypi.org/project/flet-easy/0.2.1.dev10/

not work, i use
@app.view
def view(data: fs.Datasy):
page = data.page
page.title = TITLE
page.window_min_width = 600
page.window_min_height = 800
theme = ft.Theme(font_family="Microsoft Yahei")
theme.page_transitions.windows = ft.PageTransitionTheme.NONE
page.theme = theme
page.scroll= ft.ScrollMode.AUTO
def modify_theme():
if page.theme_mode == ft.ThemeMode.DARK:
page.theme_mode = ft.ThemeMode.LIGHT
else:
page.theme_mode = ft.ThemeMode.DARK

def theme(e):
    if page.theme_mode == ft.ThemeMode.SYSTEM:
        modify_theme()
    modify_theme()
    page.update()

def go_home(e):
    page.go(data.route_init)

nav = Sidebar().view()
app_bar = ft.AppBar(
        title=ft.Image(src="baolaailogo.png",height=50),
        center_title=False,
        leading_width=40,
        bgcolor=ft.colors.SURFACE_VARIANT,
        actions=[
            ft.IconButton(ft.icons.WB_SUNNY_OUTLINED, on_click=theme),
            ft.PopupMenuButton(
                items=[
                    ft.PopupMenuItem(text="🔥 Home", on_click=go_home),
                ]
            ),
            ft.IconButton(ft.icons.TIPS_AND_UPDATES_OUTLINED, tooltip="去更新或者提出想法",
                          url=DOWNLOAD_URL),
        ],
    )

controls = [
    ft.Row(
    controls=[
        nav,
        ft.VerticalDivider(width=1),
    ],
    vertical_alignment=ft.CrossAxisAlignment.START,
    alignment=ft.MainAxisAlignment.START,
    expand=True
)]
page.appbar = app_bar


return fs.Viewsy(
    navigation_bar=nav,
    appbar=app_bar,
    controls=controls,
    vertical_alignment=ft.MainAxisAlignment.START,
    horizontal_alignment=ft.CrossAxisAlignment.START,
)

image

@Daxexs
Copy link
Owner

Daxexs commented Apr 23, 2024

Check this please:

If you don't find a solution, create a new Issue, with a more detailed example of the errors it presents, please.

@Daxexs
Copy link
Owner

Daxexs commented Apr 23, 2024

This is a way to use the view decorator
You can improve the code even further, depending on your preferences.

import flet as ft
import flet_easy as fs

app = fs.FletEasy(route_init="/home")

""" Generate configuration of the app through the page property, for greater order in the code. """

@app.config
def config(data: fs.Datasy):
    page = data.page

    page.title = "test app"
    page.window_min_width = 600
    page.window_min_height = 800
    theme = ft.Theme(font_family="Microsoft Yahei")
    theme.page_transitions.windows = ft.PageTransitionTheme.NONE
    page.theme = theme


""" Configure through fs.Viewsy to be able to reuse in ft.View on each page. """


@app.view
def view(data: fs.Datasy):
    page = data.page

    def modify_theme():
        if page.theme_mode == ft.ThemeMode.DARK:
            page.theme_mode = ft.ThemeMode.LIGHT
        else:
            page.theme_mode = ft.ThemeMode.DARK

    def theme(e):
        if page.theme_mode == ft.ThemeMode.SYSTEM:
            modify_theme()
        modify_theme()
        page.update()

    nav = ft.NavigationBar(
        destinations=[
            ft.NavigationDestination(icon=ft.icons.EXPLORE, label="Explore"),
            ft.NavigationDestination(icon=ft.icons.COMMUTE, label="Commute"),
            ft.NavigationDestination(
                icon=ft.icons.BOOKMARK_BORDER,
                selected_icon=ft.icons.BOOKMARK,
                label="Explore",
            ),
        ]
    )

    app_bar = ft.AppBar(
        title=ft.Image(src="baolaailogo.png", height=50),
        center_title=False,
        leading_width=40,
        bgcolor=ft.colors.SURFACE_VARIANT,
        actions=[
            ft.IconButton(ft.icons.WB_SUNNY_OUTLINED, on_click=theme),
            ft.PopupMenuButton(
                items=[
                    ft.PopupMenuItem(text="🔥 Home", on_click=data.go(data.route_init)),
                ]
            ),
            ft.IconButton(
                ft.icons.TIPS_AND_UPDATES_OUTLINED,
                tooltip="去更新或者提出想法",
                url="https://daxexs.github.io/flet-easy/0.2/Customized-app/Control-view-configuration/",
            ),
        ],
    )

    controls = [
        ft.Row(
            controls=[
                ft.NavigationRail(
                    selected_index=0,
                    label_type=ft.NavigationRailLabelType.ALL,
                    # extended=True,
                    min_width=100,
                    min_extended_width=400,
                    leading=ft.FloatingActionButton(icon=ft.icons.CREATE, text="Add"),
                    group_alignment=-0.9,
                    destinations=[
                        ft.NavigationRailDestination(
                            icon=ft.icons.FAVORITE_BORDER,
                            selected_icon=ft.icons.FAVORITE,
                            label="First",
                        ),
                        ft.NavigationRailDestination(
                            icon_content=ft.Icon(ft.icons.BOOKMARK_BORDER),
                            selected_icon_content=ft.Icon(ft.icons.BOOKMARK),
                            label="Second",
                        ),
                        ft.NavigationRailDestination(
                            icon=ft.icons.SETTINGS_OUTLINED,
                            selected_icon_content=ft.Icon(ft.icons.SETTINGS),
                            label_content=ft.Text("Settings"),
                        ),
                    ],
                    on_change=lambda e: print(
                        "Selected destination:", e.control.selected_index
                    ),
                ),
                ft.VerticalDivider(width=1),
            ],
            vertical_alignment=ft.CrossAxisAlignment.START,
            alignment=ft.MainAxisAlignment.START,
            expand=True,
        )
    ]

    """ We return ft.Viewsy  """
    return fs.Viewsy(
        navigation_bar=nav,
        appbar=app_bar,
        controls=controls,
        auto_scroll=ft.ScrollMode.AUTO,
        vertical_alignment=ft.MainAxisAlignment.START,
        horizontal_alignment=ft.CrossAxisAlignment.START,
    )

""" Page in which we will reuse fs.Viewsy, we can access all the attributes that we have previously configured. """
@app.page(route="/home")
def login_page(data: fs.Datasy):
    view = data.view

    """ Add controls to view, since it is created. """
    view.controls[0].controls.append(ft.Text("Hello, world!"))

    return ft.View(
        controls=view.controls,
        navigation_bar=view.navigation_bar,
        appbar=view.appbar,
        vertical_alignment=view.vertical_alignment,
        horizontal_alignment=view.horizontal_alignment,
    )

app.run()

@Daxexs Daxexs added the bug Something isn't working label Apr 25, 2024
@Daxexs Daxexs closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants