{"id":22689154,"url":"https://github.com/flokapi/flet-fastapi-example","last_synced_at":"2025-04-12T05:30:56.597Z","repository":{"id":196875662,"uuid":"697353479","full_name":"flokapi/flet-fastapi-example","owner":"flokapi","description":"Example of a Flet + FastAPI App","archived":false,"fork":false,"pushed_at":"2023-10-23T13:40:19.000Z","size":43,"stargazers_count":15,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T00:51:21.775Z","etag":null,"topics":["fastapi","flet","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flokapi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-27T14:56:52.000Z","updated_at":"2025-02-17T18:47:15.000Z","dependencies_parsed_at":"2023-09-28T02:12:51.415Z","dependency_job_id":"0a6d3ef2-47bc-4f31-b0fa-df69e8da4e4b","html_url":"https://github.com/flokapi/flet-fastapi-example","commit_stats":null,"previous_names":["flokapi/flet-fastapi-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fflet-fastapi-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fflet-fastapi-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fflet-fastapi-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fflet-fastapi-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flokapi","download_url":"https://codeload.github.com/flokapi/flet-fastapi-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248523592,"owners_count":21118529,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","flet","python"],"created_at":"2024-12-10T00:17:50.261Z","updated_at":"2025-04-12T05:30:56.515Z","avatar_url":"https://github.com/flokapi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About\n\nThis is an example of how to combine Flet and FastAPI for complex applications which require both an API and a user interface. In this architecture, the API and the user interface are defined in a single app, while being clearly separated. As a result, the back end and the front end can be implemented and published all in one.\n\nWhen using FastAPI to publish Flet Apps, the Python code is running on the server side and the browser is only displaying it. As a result, the Flet app can access the API internally as a function and doesn't need HTTP requests to communicate with the server.\n\n\n\n# The Code\n\n### Accessing to API functions from the Flet GUI\n\nEach function used by the API can be registered using the  `@api.register` decorator\n\n```python\n@api.register\n@app.get(f'{path}/set-value')\nasync def set_value(value: int):\n    global counter\n    counter = value\n    return {'message': f'Updated counter value to {value}'}\n```\n\n`api.get()` will then produce a dictionary of functions which is passed over to the Flet GUI when creating it.\n\nThe Flet app can then save the API functions dictionary as an attribute and make a call to the API whenever needed.\n\n```python\nawait self.api['set_value'](30)\n```\n\nThis is equivalent to `https://example.com/counter-dockup/set-value?value=30`\n\n\n\n### Serving a Flet App from FastAPI\n\n```python\n@asynccontextmanager\nasync def lifespan(app: FastAPI):\n    await flet_fastapi.app_manager.start()\n    yield\n    await flet_fastapi.app_manager.shutdown()\n\n\nasync def main(page: ft.Page):\n    await gui.init(page, api.get())\n\napp.mount(f'{path}/', flet_fastapi.app(main))\n```\n\n\n\n### Loop functions\n\nA loop function in the GUI can be created using `asyncio.create_task` .\n\nThis can be done both\n\n- in the API (`main.py`)\n- in the Flet App (`gui.py`), each Flet App instance (opened Flet Application) running it's own loop function\n\n\n\nIn `gui/gui.py`:\n\n```python\nasync def init(page, api):\n    counter = Counter(api)\n    await page.add_async(counter)\n    await asyncio.create_task(counter.loop())\n```\n\n\n\n# Development\n\nThe following files are required:\n\n- `main.py`: containing the FastAPI app\n\n- `requirements.txt`\n\n- `dockup.yml`\n\n    - example\n\n        ```\n        name: counter-dockup\n        path: /counter-dockup\n        type: flet_abs\n        ```\n\n    - `name` must match the parent folder name\n    - `path` is the url at which the app will be available.\n    - `type`: must be `flet_abs` for this type of application\n\n\n\n# Testing and Deployment\n\n## Launch the app in development mode\n\n```\ncd apps/counter-dockup\nuvicorn main:app --reload --port 8004\n```\n\nThe app will be available at `localhost:8004/counter-dockup/`. \n\nIt will reload itself automatically every time you save the code.\n\n\n\n## Publish the app using Dockup\n\n### About Dockup\n\nDockup is a CLI tool and Python module which allows to effortlessly publish applications as Docker container.\n\nPlease note that you must first install Dockup and a reverse proxy before publishing your app. \n\nCheck https://github.com/flokapi/dockup for the installation\n\n\n\n### Publish locally\n\nLocally\n\n```\ncd apps\ntar -czf counter-dockup.tar.gz counter-dockup\npython3 -m dockup install counter-dockup.tar.gz\n```\n\nThe app will be available at `http://localhost/counter-dockup/`\n\n\n\n### Publish on your server\n\nLocally\n\n```\ncd apps\ntar -czf counter-dockup.tar.gz counter-dockup\n```\n\nOn your server, once the the archive has been copied\n\n```\npython3 -m dockup install counter-dockup.tar.gz\n```\n\nThe app will be available at `https://example.com/counter-dockup/`\n\n\n\n# Resources\n\nFlet documentation\n\n- https://flet.dev/docs/guides/python/deploying-web-app/running-flet-with-fastapi/\n- https://flet.dev/docs/guides/python/async-apps/\n\n\n\nDockup\n\n- https://github.com/flokapi/dockup\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflokapi%2Fflet-fastapi-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflokapi%2Fflet-fastapi-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflokapi%2Fflet-fastapi-example/lists"}