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

FastAPI instead of built-in Fletd server. Mixed async/sync apps. #2700

Merged
merged 34 commits into from
Feb 29, 2024

Conversation

FeodorFitsner
Copy link
Contributor

@FeodorFitsner FeodorFitsner commented Feb 21, 2024

  • runtime is async by default, Flet is async-first framework.
  • two types of backend: fast api, socket server
  • diagram with event loop, pool threads.
  • mix sync and async handlers.
  • no need to use _async methods in the most cases.
  • UserControl is deprecated.
  • page.run_thread() and page.run_task() and page.executor and page.loop.
  • Run with any ASGI web server:
app = ft.app(main, export_asgi_app=True)

Run with Hypercorn:

hypercorn path.to.your.main:app --bind 0.0.0.0:8000

API changes

  • Replace import flet_fastapi with import flet.fastapi as flet_fastapi.
  • ft.app(export_asgi_app). How assets, upload folders are resolved?
  • Control.did_mount_async() and Control.will_unmount_async() are not called anymore. Use Control.did_mount() and Control.will_unmount() instead.
  • Use .update() and .add() in both sync and async handlers.
  • Control._before_build_command() replaced with Control.before_update().
  • Control._is_isolated() replaced with Control.is_isolated().
  • OAuth handler URL endpoint changed from /api/oauth/redirect to /oauth_callback. Use FLET_OAUTH_CALLBACK_HANDLER_ENDPOINT env var to customize.

Deprecated methods

  • Audio.pause_async
  • Audio.play_async
  • Audio.release_async
  • Audio.resume_async
  • Audio.seek_async
  • AudioRecorder.pause_recording_async
  • AudioRecorder.resume_recording_async
  • AudioRecorder.start_recording_async
  • Canvas.clean_async
  • Column.clean_async
  • DatePicker.pick_date_async
  • Dismissible.confirm_dismiss_async
  • Dropdown.focus_async
  • ElevatedButton.focus_async
  • FilePicker.get_directory_path_async
  • FilePicker.pick_files_async
  • FilePicker.save_file_async
  • FilePicker.upload_async
  • GridView.clean_async
  • HapticFeedback.heavy_impact_async
  • HapticFeedback.light_impact_async
  • HapticFeedback.medium_impact_async
  • HapticFeedback.vibrate_async
  • IconButton.focus_async
  • ListView.clean_async
  • MenuItemButton.focus_async
  • OutlinedButton.focus_async
  • Page.add_async
  • Page.clean_async
  • Page.close_banner_async
  • Page.close_bottom_sheet_async
  • Page.close_dialog_async
  • Page.close_drawer_async
  • Page.close_end_drawer_async
  • Page.close_in_app_web_view_async
  • Page.error_async
  • Page.get_upload_url_async
  • Page.go_async
  • Page.insert_async
  • Page.launch_url_async
  • Page.logout_async
  • Page.remove_async
  • Page.remove_at_async
  • Page.scroll_to_async
  • Page.set_clipboard_async
  • Page.show_banner_async
  • Page.show_bottom_sheet_async
  • Page.show_dialog_async
  • Page.show_drawer_async
  • Page.show_end_drawer_async
  • Page.show_snack_bar_async
  • Page.update_async
  • Page.window_center_async
  • Page.window_close_async
  • Page.window_destroy_async
  • Page.window_to_front_async
  • Pagelet.close_drawer_async
  • Pagelet.close_end_drawer_async
  • Pagelet.show_drawer_async
  • Pagelet.show_end_drawer_async
  • PubSub.send_all_async
  • PubSub.send_all_on_topic_async
  • PubSub.send_others_async
  • PubSub.send_others_on_topic_async
  • PubSub.subscribe_async
  • PubSub.subscribe_topic_async
  • PubSub.unsubscribe_all_async
  • PubSub.unsubscribe_async
  • PubSub.unsubscribe_topic_async
  • ResponsiveRow.clean_async
  • Row.clean_async
  • ScrollableControl.scroll_to_async
  • SearchBar.close_view_async
  • SearchBar.open_view_async
  • SubmenuButton.focus_async
  • TextButton.focus_async
  • TextField.focus_async
  • TimePicker.pick_time_async
  • Video.next_async
  • Video.pause_async
  • Video.play_async
  • Video.play_or_pause_async
  • Video.playlist_add_async
  • Video.playlist_remove_async
  • Video.previous_async
  • Video.stop_async

Environment variables

  • FLET_FORCE_WEB_SERVER
  • FLET_SERVER_PORT - 8000 is program is run on a Linux server or FLET_FORCE_WEB_SERVER is set; otherwise random port.
  • FLET_SERVER_IP, bounds to all server IPs by default.
  • FLET_ASSETS_DIR
  • FLET_UPLOAD_DIR
  • FLET_MAX_UPLOAD_SIZE, bytes. Unlimited if not specified.
  • FLET_SECRET_KEY - for uploads
  • FLET_WEB_APP_PATH
  • FLET_SESSION_TIMEOUT, seconds. Default is 3600.
  • FLET_OAUTH_STATE_TIMEOUT, seconds. Default is 600.
  • FLET_WEB_RENDERER - canvaskit (default), html or auto.
  • FLET_WEB_USE_COLOR_EMOJI. true, or True or 1.
  • FLET_WEB_ROUTE_URL_STRATEGY - path (default) or hash.
  • FLET_WEBSOCKET_HANDLER_ENDPOINT - default /ws.
  • FLET_UPLOAD_HANDLER_ENDPOINT - default /upload.
  • FLET_OAUTH_CALLBACK_HANDLER_ENDPOINT - default /oauth_callback.

Sync countdown example

import time

import flet as ft


class Countdown(ft.UserControl):
    def __init__(self, seconds):
        super().__init__()
        self.seconds = seconds

    def did_mount(self):
        self.running = True
        self.page.run_in_thread(self.update_timer)

    def will_unmount(self):
        self.running = False

    def update_timer(self):
        while self.seconds and self.running:
            mins, secs = divmod(self.seconds, 60)
            self.countdown.value = "{:02d}:{:02d}".format(mins, secs)
            self.update()
            time.sleep(1)
            self.seconds -= 1

    def build(self):
        self.countdown = ft.Text()
        return self.countdown


def main(page: ft.Page):
    page.add(Countdown(120), Countdown(60))


ft.app(main)

Async countdown example

import asyncio

import flet as ft


class Countdown(ft.UserControl):
    def __init__(self, seconds):
        super().__init__()
        self.seconds = seconds

    def did_mount(self):
        self.running = True
        self.page.run_task(self.update_timer)

    def will_unmount(self):
        self.running = False

    async def update_timer(self):
        while self.seconds and self.running:
            mins, secs = divmod(self.seconds, 60)
            self.countdown.value = "{:02d}:{:02d}".format(mins, secs)
            self.update()
            await asyncio.sleep(1)
            self.seconds -= 1

    def build(self):
        self.countdown = ft.Text()
        return self.countdown


def main(page: ft.Page):
    page.add(Countdown(120), Countdown(60))


ft.app(main)

commit fd2e80788e27cd022fce509b5499d1951630e4a9
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Fri Feb 23 09:38:59 2024 -0800

    Removed blocking decorator

commit 7d1f925392f63a85a7f60fcb07afa3374cb565b5
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Fri Feb 23 09:25:56 2024 -0800

    Added pool to app manager
commit 08b9ab0
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Sun Feb 25 14:31:56 2024 -0800

    invoke_method() refactored

commit 9a1e37c
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Sun Feb 25 13:55:18 2024 -0800

    `Control._is_isolated()` replaced with `Control.is_isolated()`

    Close #2721

commit 1a3d82d
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Sun Feb 25 13:14:33 2024 -0800

    Fix deps for tests

commit 4f8326c
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Sun Feb 25 13:11:05 2024 -0800

    `Control. _before_build_command()` renamed to `Control.before_update()`

commit 80a2ab0
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Sun Feb 25 13:06:32 2024 -0800

    Deprecate controls' async methods

commit 77f7989
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Sat Feb 24 18:34:13 2024 -0800

    async methods from connection removed

commit d7edea0
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Fri Feb 23 14:24:52 2024 -0800

    `page.go` re-worked, EventHandler re-worked

commit 679144d
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Fri Feb 23 13:17:18 2024 -0800

    PubSubHub moved to flet_core

commit 5103072
Author: Feodor Fitsner <feodor@appveyor.com>
Date:   Fri Feb 23 12:51:51 2024 -0800

    PubSub sync and some async page functions removed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant