imperative-to-declarative-flet

Convert imperative Flet Python apps to declarative components using hooks and observable state.

16.6k|685|Updated Mar 24, 2022
One-click install
npx skills add https://github.com/flet-dev/flet --skill imperative-to-declarative-flet
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: imperative-to-declarative-flet
Source: https://github.com/flet-dev/flet/tree/main/.agents/skills/imperative-to-declarative-flet
Command: npx skills add https://github.com/flet-dev/flet --skill imperative-to-declarative-flet

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires flet.

What problem does it solve?

Flet apps written imperatively mutate controls and call page.update() manually, which becomes hard to maintain as UI grows. This Skill ports such apps to Flet's declarative components mode, where UI is derived from state and manual updates are eliminated.

Core Features & Use Cases

  • Entrypoint Migration: Replaces imperative main functions with a root @ft.component App rendered via page.render(App) in components mode.
  • State Strategy Guidance: Chooses between @ft.observable models for nested app data and ft.use_state hooks for local ephemeral UI state.
  • Componentization & Routing: Moves UI chunks into src/components, centralizes route handling in App, and converts dialogs to state-driven components without explicit page.update() calls.
  • Use Case: You have a Trello-like Flet board app with drag-and-drop that calls page.update() everywhere; this Skill produces a sibling declarative folder using observable models for boards and cards.

Quick Start

Convert my imperative Flet app in the current folder to declarative components mode using flet.component, hooks, and observable state.

Frequently Asked Questions about imperative-to-declarative-flet

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I convert an imperative Flet app to declarative components?

Copy the app folder, create a root @ft.component App, and run it with ft.run(lambda page: page.render(App)). Move UI into components, use ft.use_state for local state and @ft.observable for app data, removing manual page.update() calls.

When should I use ft.observable vs ft.use_state in Flet?

Use @ft.observable for nested application data you mutate in place, such as boards, lists, or cards with drag-and-drop reordering. Use ft.use_state for local ephemeral UI state like hover flags, input text, or dialog selections.

How do I avoid page.update() in Flet dialogs?

Convert the dialog content into an @ft.component that uses ft.use_state for values like error messages or selected colors. Event handlers call state setters, and the dialog is shown via page.show_dialog(ft.AlertDialog(content=DialogContent(...))).

Why does my Flet event handler raise a typing error with components?

Event handler typing is invariant, so Event[Sub] is not assignable to Event[Base]. If on_click is declared on Button, annotate the parameter as ft.Event[ft.Button] or define the handler without a parameter.

How do I register custom fonts in a Flet components app?

Set page.fonts with a dict where the key is the font family name, for example page.fonts = {"Pacifico": "Pacifico-Regular.ttf"}, then reference font_family="Pacifico". Prefer an absolute assets_dir derived from __file__.