flet-model


Nameflet-model JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
SummaryA Model-based router for Flet applications that simplifies the creation of multi-page applications
upload_time2024-11-28 08:26:02
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT License Copyright (c) 2024 Fasil Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords flet framework gui model mvc navigation router ui
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flet Model

A Model-based router for Flet applications that simplifies the creation of multi-page applications with built-in state management and navigation.

## Installation

```bash
pip install flet-model
```

## Core Features

- Model-based view architecture
- Automatic route handling and nested routes
- Event binding with caching for improved performance
- Built-in view state management
- Navigation handling (drawers, bottom bar, FAB)
- Thread-safe initialization hooks
- Support for keyboard and scroll events
- View caching system

## Basic Usage

```python
import flet as ft
from flet_model import Model, Router


class HomeModel(Model):
    route = 'home'

    # Layout configuration
    vertical_alignment = ft.MainAxisAlignment.CENTER
    horizontal_alignment = ft.CrossAxisAlignment.CENTER
    padding = 20
    spacing = 10

    # UI Components
    appbar = ft.AppBar(
        title=ft.Text("Home"),
        center_title=True,
        bgcolor=ft.Colors.SURFACE_CONTAINER_HIGHEST
    )

    controls = [
        ft.Text("Welcome to Home Page", size=24),
        ft.ElevatedButton("Go to Profile", on_click="navigate_to_profile")
    ]

    def navigate_to_profile(self, e):
        self.page.go('/home/profile')


class ProfileModel(Model):
    route = 'profile'

    # Layout configuration
    vertical_alignment = ft.MainAxisAlignment.CENTER
    horizontal_alignment = ft.CrossAxisAlignment.CENTER
    padding = 20
    spacing = 10

    # UI Components
    appbar = ft.AppBar(
        title=ft.Text("Profile"),
        center_title=True,
        bgcolor=ft.Colors.SURFACE_CONTAINER_HIGHEST
    )

    controls = [
        ft.Text("Welcome to Profile Page", size=24),
    ]


def main(page: ft.Page):
    page.title = "Flet Model Demo"
    Router(
        {'home': HomeModel(page)},
        {'profile': ProfileModel(page)},
    )
    page.go(page.route)


ft.app(target=main)
```

## Advanced Features

### 1. Route Data Passing

```python
# Navigate with data
self.page.go('/products#id=123&category=electronics')

class ProductModel(Model):
    def init(self):
        # Access route data
        product_id = self.route_data.get('id')
        category = self.route_data.get('category')
```

### 2. Navigation Drawers

```python
class DrawerModel(Model):
    drawer = ft.NavigationDrawer(
        controls=[
            ft.NavigationDrawerDestination(
                icon=ft.Icons.HOME,
                label="Home",
                selected_icon=ft.Icons.HOME_OUTLINED
            )
        ]
    )

    end_drawer = ft.NavigationDrawer(
        controls=[
            ft.NavigationDrawerDestination(
                icon=ft.Icons.SETTINGS,
                label="Settings"
            )
        ]
    )

    controls = [
        ft.ElevatedButton('Open Drawer', on_click=lambda e: e.control.page.open(e.control.data), data=drawer),
        ft.ElevatedButton('Open End Drawer', on_click=lambda e: e.control.page.open(e.control.data), data=end_drawer)
    ]
```

### 3. Event Handlers and Lifecycle Hooks

```python
class EventModel(Model):
    def init(self):
        # Called before view creation
        self.load_data()
    
    def post_init(self):
        # Called after view creation
        self.setup_listeners()
    
    def on_keyboard_event(self, e: ft.KeyboardEvent):
        if e.key == "Enter":
            self.handle_enter()
    
    def on_scroll(self, e: ft.OnScrollEvent):
        if e.pixels >= e.max_scroll_extent - 100:
            self.load_more_data()
```

### 4. Floating Action Button

```python
class FABModel(Model):
    floating_action_button = ft.FloatingActionButton(
        icon=ft.Icons.ADD,
        on_click="add_item"
    )
    floating_action_button_location = ft.FloatingActionButtonLocation.END_DOCKED
```

### 5. Bottom Navigation

```python
class NavigationModel(Model):
    navigation_bar = ft.NavigationBar(
        destinations=[
            ft.NavigationDestination(icon=ft.Icons.HOME, label="Home"),
            ft.NavigationDestination(icon=ft.Icons.PERSON, label="Profile")
        ],
        on_change="handle_navigation"
    )
```

### 6. Overlay Controls

```python
class OverlayModel(Model):
    overlay_controls = [
        ft.Banner(
            open=True,
            content=ft.Text("Important message!"),
            actions=[
                ft.TextButton("Dismiss", on_click="dismiss_banner")
            ]
        )
    ]

    def dismiss_banner(self, e):
        self.page.close(e.control.parent)
```

### 7. Fullscreen Dialogs

```python
class DialogModel(Model):
    route = "dialog"
    fullscreen_dialog = True

    controls = [
        ft.Text("Dialog Content"),
        ft.ElevatedButton("Close", on_click="close_dialog")
    ]

    def close_dialog(self, e):
        self.page.views.pop()
        self.page.go(self.page.views[-1].route)
```

## Real-world Example

Here's a complete example of a todo application using Flet Model:

```python
import flet as ft
from flet_model import Model, Router
from typing import List


class TodoItem:
    def __init__(self, title: str, completed: bool = False):
        self.title = title
        self.completed = completed


class TodoModel(Model):
    route = "todo"
    todos: List[TodoItem] = []

    appbar = ft.AppBar(
        title=ft.Text("Todo List"),
        center_title=True
    )

    def get_controls(self):
        return [
            ft.TextField(
                hint_text="Add new todo",
                on_submit="add_todo",
                autofocus=True
            ),
            ft.Column(controls=self.get_todo_control())
        ]

    def get_todo_control(self):
        return [
            ft.Checkbox(
                label=todo.title,
                value=todo.completed,
                on_change=lambda e, t=todo: self.toggle_todo(e, t)
            ) for todo in self.todos
        ]

    controls = [
            ft.TextField(
                hint_text="Add new todo",
                on_submit="add_todo",
                autofocus=True
            ),
            ft.Column()
        ]

    def add_todo(self, e):
        if e.control.value:
            self.todos.append(TodoItem(e.control.value))
            e.control.value = ""
            self.controls[-1].controls = self.get_todo_control()
            self.update()
            e.control.focus()

    def toggle_todo(self, e, todo):
        todo.completed = e.control.value
        self.update()


def main(page: ft.Page):
    Router(
        {'todo': TodoModel(page)}
    )
    page.go('todo')


ft.app(target=main)
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License.
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "flet-model",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "flet, framework, gui, model, mvc, navigation, router, ui",
    "author": null,
    "author_email": "Fasil <fasilwdr@hotmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/dd/07/ba8ccf98c98565a847bd14872f1a935689112c7dc41c8bca989d954fd666/flet_model-0.1.4.tar.gz",
    "platform": null,
    "description": "# Flet Model\n\nA Model-based router for Flet applications that simplifies the creation of multi-page applications with built-in state management and navigation.\n\n## Installation\n\n```bash\npip install flet-model\n```\n\n## Core Features\n\n- Model-based view architecture\n- Automatic route handling and nested routes\n- Event binding with caching for improved performance\n- Built-in view state management\n- Navigation handling (drawers, bottom bar, FAB)\n- Thread-safe initialization hooks\n- Support for keyboard and scroll events\n- View caching system\n\n## Basic Usage\n\n```python\nimport flet as ft\nfrom flet_model import Model, Router\n\n\nclass HomeModel(Model):\n    route = 'home'\n\n    # Layout configuration\n    vertical_alignment = ft.MainAxisAlignment.CENTER\n    horizontal_alignment = ft.CrossAxisAlignment.CENTER\n    padding = 20\n    spacing = 10\n\n    # UI Components\n    appbar = ft.AppBar(\n        title=ft.Text(\"Home\"),\n        center_title=True,\n        bgcolor=ft.Colors.SURFACE_CONTAINER_HIGHEST\n    )\n\n    controls = [\n        ft.Text(\"Welcome to Home Page\", size=24),\n        ft.ElevatedButton(\"Go to Profile\", on_click=\"navigate_to_profile\")\n    ]\n\n    def navigate_to_profile(self, e):\n        self.page.go('/home/profile')\n\n\nclass ProfileModel(Model):\n    route = 'profile'\n\n    # Layout configuration\n    vertical_alignment = ft.MainAxisAlignment.CENTER\n    horizontal_alignment = ft.CrossAxisAlignment.CENTER\n    padding = 20\n    spacing = 10\n\n    # UI Components\n    appbar = ft.AppBar(\n        title=ft.Text(\"Profile\"),\n        center_title=True,\n        bgcolor=ft.Colors.SURFACE_CONTAINER_HIGHEST\n    )\n\n    controls = [\n        ft.Text(\"Welcome to Profile Page\", size=24),\n    ]\n\n\ndef main(page: ft.Page):\n    page.title = \"Flet Model Demo\"\n    Router(\n        {'home': HomeModel(page)},\n        {'profile': ProfileModel(page)},\n    )\n    page.go(page.route)\n\n\nft.app(target=main)\n```\n\n## Advanced Features\n\n### 1. Route Data Passing\n\n```python\n# Navigate with data\nself.page.go('/products#id=123&category=electronics')\n\nclass ProductModel(Model):\n    def init(self):\n        # Access route data\n        product_id = self.route_data.get('id')\n        category = self.route_data.get('category')\n```\n\n### 2. Navigation Drawers\n\n```python\nclass DrawerModel(Model):\n    drawer = ft.NavigationDrawer(\n        controls=[\n            ft.NavigationDrawerDestination(\n                icon=ft.Icons.HOME,\n                label=\"Home\",\n                selected_icon=ft.Icons.HOME_OUTLINED\n            )\n        ]\n    )\n\n    end_drawer = ft.NavigationDrawer(\n        controls=[\n            ft.NavigationDrawerDestination(\n                icon=ft.Icons.SETTINGS,\n                label=\"Settings\"\n            )\n        ]\n    )\n\n    controls = [\n        ft.ElevatedButton('Open Drawer', on_click=lambda e: e.control.page.open(e.control.data), data=drawer),\n        ft.ElevatedButton('Open End Drawer', on_click=lambda e: e.control.page.open(e.control.data), data=end_drawer)\n    ]\n```\n\n### 3. Event Handlers and Lifecycle Hooks\n\n```python\nclass EventModel(Model):\n    def init(self):\n        # Called before view creation\n        self.load_data()\n    \n    def post_init(self):\n        # Called after view creation\n        self.setup_listeners()\n    \n    def on_keyboard_event(self, e: ft.KeyboardEvent):\n        if e.key == \"Enter\":\n            self.handle_enter()\n    \n    def on_scroll(self, e: ft.OnScrollEvent):\n        if e.pixels >= e.max_scroll_extent - 100:\n            self.load_more_data()\n```\n\n### 4. Floating Action Button\n\n```python\nclass FABModel(Model):\n    floating_action_button = ft.FloatingActionButton(\n        icon=ft.Icons.ADD,\n        on_click=\"add_item\"\n    )\n    floating_action_button_location = ft.FloatingActionButtonLocation.END_DOCKED\n```\n\n### 5. Bottom Navigation\n\n```python\nclass NavigationModel(Model):\n    navigation_bar = ft.NavigationBar(\n        destinations=[\n            ft.NavigationDestination(icon=ft.Icons.HOME, label=\"Home\"),\n            ft.NavigationDestination(icon=ft.Icons.PERSON, label=\"Profile\")\n        ],\n        on_change=\"handle_navigation\"\n    )\n```\n\n### 6. Overlay Controls\n\n```python\nclass OverlayModel(Model):\n    overlay_controls = [\n        ft.Banner(\n            open=True,\n            content=ft.Text(\"Important message!\"),\n            actions=[\n                ft.TextButton(\"Dismiss\", on_click=\"dismiss_banner\")\n            ]\n        )\n    ]\n\n    def dismiss_banner(self, e):\n        self.page.close(e.control.parent)\n```\n\n### 7. Fullscreen Dialogs\n\n```python\nclass DialogModel(Model):\n    route = \"dialog\"\n    fullscreen_dialog = True\n\n    controls = [\n        ft.Text(\"Dialog Content\"),\n        ft.ElevatedButton(\"Close\", on_click=\"close_dialog\")\n    ]\n\n    def close_dialog(self, e):\n        self.page.views.pop()\n        self.page.go(self.page.views[-1].route)\n```\n\n## Real-world Example\n\nHere's a complete example of a todo application using Flet Model:\n\n```python\nimport flet as ft\nfrom flet_model import Model, Router\nfrom typing import List\n\n\nclass TodoItem:\n    def __init__(self, title: str, completed: bool = False):\n        self.title = title\n        self.completed = completed\n\n\nclass TodoModel(Model):\n    route = \"todo\"\n    todos: List[TodoItem] = []\n\n    appbar = ft.AppBar(\n        title=ft.Text(\"Todo List\"),\n        center_title=True\n    )\n\n    def get_controls(self):\n        return [\n            ft.TextField(\n                hint_text=\"Add new todo\",\n                on_submit=\"add_todo\",\n                autofocus=True\n            ),\n            ft.Column(controls=self.get_todo_control())\n        ]\n\n    def get_todo_control(self):\n        return [\n            ft.Checkbox(\n                label=todo.title,\n                value=todo.completed,\n                on_change=lambda e, t=todo: self.toggle_todo(e, t)\n            ) for todo in self.todos\n        ]\n\n    controls = [\n            ft.TextField(\n                hint_text=\"Add new todo\",\n                on_submit=\"add_todo\",\n                autofocus=True\n            ),\n            ft.Column()\n        ]\n\n    def add_todo(self, e):\n        if e.control.value:\n            self.todos.append(TodoItem(e.control.value))\n            e.control.value = \"\"\n            self.controls[-1].controls = self.get_todo_control()\n            self.update()\n            e.control.focus()\n\n    def toggle_todo(self, e, todo):\n        todo.completed = e.control.value\n        self.update()\n\n\ndef main(page: ft.Page):\n    Router(\n        {'todo': TodoModel(page)}\n    )\n    page.go('todo')\n\n\nft.app(target=main)\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License.",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Fasil  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "A Model-based router for Flet applications that simplifies the creation of multi-page applications",
    "version": "0.1.4",
    "project_urls": {
        "Changelog": "https://github.com/fasilwdr/Flet-Model/releases",
        "Documentation": "https://github.com/fasilwdr/Flet-Model#readme",
        "Homepage": "https://github.com/fasilwdr/Flet-Model",
        "Issues": "https://github.com/fasilwdr/Flet-Model/issues",
        "Repository": "https://github.com/fasilwdr/Flet-Model.git"
    },
    "split_keywords": [
        "flet",
        " framework",
        " gui",
        " model",
        " mvc",
        " navigation",
        " router",
        " ui"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c9b1b91c78a1d74b59d1346531433a2fb922af9bacb4ca3e64d20f1df365f58e",
                "md5": "c5783bc2f18c8c80cd6c3f5f7c846eed",
                "sha256": "bf89329b9407e4d98fc2baa039e64b7df696e330f35cba9d6d59469d36c99450"
            },
            "downloads": -1,
            "filename": "flet_model-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c5783bc2f18c8c80cd6c3f5f7c846eed",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 8294,
            "upload_time": "2024-11-28T08:26:01",
            "upload_time_iso_8601": "2024-11-28T08:26:01.175423Z",
            "url": "https://files.pythonhosted.org/packages/c9/b1/b91c78a1d74b59d1346531433a2fb922af9bacb4ca3e64d20f1df365f58e/flet_model-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dd07ba8ccf98c98565a847bd14872f1a935689112c7dc41c8bca989d954fd666",
                "md5": "7d3c2fc3d6a4505775dc73b2546c514c",
                "sha256": "81b99c84fd0db1169e80087cfe3f71440310d7b3337ad722a18af31265745972"
            },
            "downloads": -1,
            "filename": "flet_model-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "7d3c2fc3d6a4505775dc73b2546c514c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 9170,
            "upload_time": "2024-11-28T08:26:02",
            "upload_time_iso_8601": "2024-11-28T08:26:02.235321Z",
            "url": "https://files.pythonhosted.org/packages/dd/07/ba8ccf98c98565a847bd14872f1a935689112c7dc41c8bca989d954fd666/flet_model-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-28 08:26:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fasilwdr",
    "github_project": "Flet-Model",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "flet-model"
}
        
Elapsed time: 4.10749s