Skip to content

By means of functions

Adding pages to the main app without using decorators

For this we will require the add_routes method of the object created by the FletEasy class.

Soporta async

App structure

FletEasy

Example using functions

index.py
import flet_easy as fs
import flet as ft

def page_index(data: fs.Datasy):
    return ft.View(
        controls=[
            ft.Text('Index'),
        ],
        vertical_alignment="center",
        horizontal_alignment="center"

    )

Example using classes

Available since version 0.2.4

More information

Create new pages (View) by using classes, you don't need to inherit from any other class to add the page, you just need to:

  • The constructor must have as mandatory parameter data:fs.Datasy and if it receives a parameter bymeans of the url it must be used as parameter.
  • That the class to use must have a mandatory method called build that will return View from flet, itcan be async if necessary. This build method does not receive any parameter.

🤔 why use a class?

The class can have several benefits, such as inheritance which is useful to avoid repeating code, among others.

test.py
import flet_easy as fs
import flet as ft

class PageTest:
    def __init__(self, data:fs.Datasy, id:int, name:str):
        self.data = data
        self.id = id
        self.name = name

    def build(self):
        return ft.View(
            controls=[
                ft.Text('Text'),
                ft.Text(f'Id: {self.id}'),
                ft.Text(f'Name: {self.name}'),
                ft.FilledButton(
                            "Go index",
                            on_click=self.data.go("/index"),
                        ),
            ],
            vertical_alignment="center",
            horizontal_alignment="center"
    )

Add routes

We import the functions or classes from the views folder, then we use the add_routes method of the FletEasy instance, in which we will add a list of Pagesy classes where we will configure the routes and the functions or classes to be used in addition to others.

main.py
# Import functions from a `views` folder
from views.user import users
from views.index import page_index
from views.test import PageTest
import flet_easy as fs

# Add routes without the use of decorators
app.add_routes(add_views=[
    fs.Pagesy('/index', page_index, tilte='index'),
    fs.Pagesy('/user/task', users, tilte='users'),
    fs.Pagesy(
              '/test/{id:d}/user/{name:l}',
              PageTest,
              title='test counter',
              protected_route=True
            ),
])

Pagesy

📑 The class Pagesy, it requires the following parameters:

  • route: text string of the url, for example('/index').
  • view: Stores the page function.
  • title : Define the title of the page.
  • clear: Removes the pages from the page.views list of flet. (optional)
  • share_data : It is a boolean value, which is useful if you want to share data between pages, in a morerestricted way. (optional) [See more]
  • protected_route: Protects the route of the page, according to the configuration of the login decoratorof the FletEasy class. (optional) [See more]
  • custom_params: To add validation of parameters in the custom url using a dictionary, where the key is the nameof the parameter validation and the value is the custom function that must report a boolean value. [See more]
  • middleware : It acts as an intermediary between different software components, intercepting andprocessing requests and responses. They allow adding functionalities to an application in a flexible andmodular way. (optional) [See more]