Name | flet-fastapi JSON |
Version |
0.20.2
JSON |
| download |
home_page | |
Summary | Flet for Fast API - serve Flet app with Fast API server |
upload_time | 2024-02-18 00:57:46 |
maintainer | |
docs_url | None |
author | Appveyor Systems Inc. |
requires_python | >=3.7,<4.0 |
license | Apache-2.0 |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Flet - a better UI for FastAPI
Flet for FastAPI allows adding interactive real-time dashboards to your FastAPI app as well as host any Flet web app inside FastAPI with production-grade reliability.
## Installation
```
pip install flet-fastapi
```
## First app
Create `counter.py` with the following content:
```python
import flet as ft
import flet_fastapi
async def main(page: ft.Page):
counter = ft.Text("0", size=50, data=0)
async def add_click(e):
counter.data += 1
counter.value = str(counter.data)
await counter.update_async()
page.floating_action_button = ft.FloatingActionButton(
icon=ft.icons.ADD, on_click=add_click
)
await page.add_async(
ft.Container(counter, alignment=ft.alignment.center, expand=True)
)
app = flet_fastapi.app(main)
```
That's a simple app displaying a counter and a button at the right bottom to increment that counter.
`flet_fastapi.app()` configures a single Flet app at the root of FastAPI app with `main()` sessions handler and the following endpoints:
`/ws` (WS) - WebSocket handler for the Flet app.
`/upload` (PUT) - file uploads handler.
`/oauth_callback` (GET) - OAuth flow callback handler.
`/` (GET) - Flet app static files with SPA catch-all handler.
## Running the app locally
Install [Uvicorn](https://www.uvicorn.org/) web server:
```
pip install uvicorn
```
Start `uvicorn` with:
```
uvicorn counter:app
```
Open the browser and navigate to http://127.0.0.1:8000 to see the app running.
## Hosting multiple Flet apps under the same domain
```python
import flet as ft
import flet_fastapi
async def root_main(page: ft.Page):
await page.add_async(ft.Text("This is root app!"))
async def sub_main(page: ft.Page):
await page.add_async(ft.Text("This is sub app!"))
app = flet_fastapi.FastAPI()
app.mount("/sub-app", flet_fastapi.app(sub_main))
app.mount("/", flet_fastapi.app(root_main))
```
Sub-apps must be mapped before the root Flet app as it configures catch-all `index.html` for SPA.
Run the app with `uvicorn` and visit http://127.0.0.1:8000 and then http://127.0.0.1:8000/sub-app/ to see both Flet apps running. Notice the trailing slash in `/sub-app/` URL - without the slash the request will be routed to a root app.
## Adding Flet to the existing FastAPI app
```python
from contextlib import asynccontextmanager
import flet as ft
import flet_fastapi
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
await flet_fastapi.app_manager.start()
yield
await flet_fastapi.app_manager.shutdown()
app = FastAPI(lifespan=lifespan)
async def main(page: ft.Page):
await page.add_async(ft.Text("Hello, Flet!"))
app.mount("/flet-app", flet_fastapi.app(main))
```
When adding Flet app to the existing FastAPI app you need to call `flet_fastapi.app_manager.start()` on app start and `flet_fastapi.app_manager.shutdown()` on shutdown. Use the way that best suites you: lifespan (in the example above) or app events.
`app_manager.start()` method starts background tasks cleaning up expired sessions and OAuth flow states.
`app_manager.shutdown()` method removes any temporary files created by a Flet app.
## Running the app in production
It is recommended to run FastAPI in production with [Hypercorn](https://github.com/pgjones/hypercorn/) which is ASGI web server, but it is also possible to run FastAPI apps with [Gunicorn](https://gunicorn.org/) which is a WSGI server, but has more features, like passing proxy headers.
To install Gunicorn:
```
pip install gunicorn
```
Start `gunicorn` with:
```
gunicorn -k uvicorn.workers.UvicornWorker counter:app
```
## Reference
### Environment variables
`FLET_SECRET_KEY` - secret key to sign upload requests. Must be set if upload directory is configured.
`FLET_SESSION_TIMEOUT` - the number of seconds to keep session alive after user has disconnected. Default is 3,600 seconds.
`FLET_OAUTH_STATE_TIMEOUT` - OAuth state lifetime, in seconds, which is a maximum allowed time between starting OAuth flow and redirecting to OAuth callback URL. Default is 600 seconds.
`FLET_MAX_UPLOAD_SIZE` - max allowed size of an uploaded file, bytes.
Raw data
{
"_id": null,
"home_page": "",
"name": "flet-fastapi",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Appveyor Systems Inc.",
"author_email": "hello@flet.dev",
"download_url": "https://files.pythonhosted.org/packages/1a/eb/0b0852a1a01519d19a665a9af40c160ae7a68724bfb27392711e091aaaef/flet_fastapi-0.20.2.tar.gz",
"platform": null,
"description": "# Flet - a better UI for FastAPI\n\nFlet for FastAPI allows adding interactive real-time dashboards to your FastAPI app as well as host any Flet web app inside FastAPI with production-grade reliability.\n\n## Installation\n\n```\npip install flet-fastapi\n```\n\n## First app\n\nCreate `counter.py` with the following content:\n\n```python\nimport flet as ft\nimport flet_fastapi\n\nasync def main(page: ft.Page):\n counter = ft.Text(\"0\", size=50, data=0)\n\n async def add_click(e):\n counter.data += 1\n counter.value = str(counter.data)\n await counter.update_async()\n\n page.floating_action_button = ft.FloatingActionButton(\n icon=ft.icons.ADD, on_click=add_click\n )\n await page.add_async(\n ft.Container(counter, alignment=ft.alignment.center, expand=True)\n )\n\napp = flet_fastapi.app(main)\n```\n\nThat's a simple app displaying a counter and a button at the right bottom to increment that counter.\n\n`flet_fastapi.app()` configures a single Flet app at the root of FastAPI app with `main()` sessions handler and the following endpoints:\n\n`/ws` (WS) - WebSocket handler for the Flet app.\n\n`/upload` (PUT) - file uploads handler.\n\n`/oauth_callback` (GET) - OAuth flow callback handler.\n\n`/` (GET) - Flet app static files with SPA catch-all handler.\n\n## Running the app locally\n\nInstall [Uvicorn](https://www.uvicorn.org/) web server:\n\n```\npip install uvicorn\n```\n\nStart `uvicorn` with:\n\n```\nuvicorn counter:app\n```\n\nOpen the browser and navigate to http://127.0.0.1:8000 to see the app running.\n\n## Hosting multiple Flet apps under the same domain\n\n```python\nimport flet as ft\nimport flet_fastapi\n\n\nasync def root_main(page: ft.Page):\n await page.add_async(ft.Text(\"This is root app!\"))\n\n\nasync def sub_main(page: ft.Page):\n await page.add_async(ft.Text(\"This is sub app!\"))\n\n\napp = flet_fastapi.FastAPI()\n\n\napp.mount(\"/sub-app\", flet_fastapi.app(sub_main))\napp.mount(\"/\", flet_fastapi.app(root_main))\n```\n\nSub-apps must be mapped before the root Flet app as it configures catch-all `index.html` for SPA.\n\nRun the app with `uvicorn` and visit http://127.0.0.1:8000 and then http://127.0.0.1:8000/sub-app/ to see both Flet apps running. Notice the trailing slash in `/sub-app/` URL - without the slash the request will be routed to a root app.\n\n## Adding Flet to the existing FastAPI app\n\n```python\nfrom contextlib import asynccontextmanager\n\nimport flet as ft\nimport flet_fastapi\nfrom fastapi import FastAPI\n\n@asynccontextmanager\nasync def lifespan(app: FastAPI):\n await flet_fastapi.app_manager.start()\n yield\n await flet_fastapi.app_manager.shutdown()\n\napp = FastAPI(lifespan=lifespan)\n\nasync def main(page: ft.Page):\n await page.add_async(ft.Text(\"Hello, Flet!\"))\n\napp.mount(\"/flet-app\", flet_fastapi.app(main))\n```\n\nWhen adding Flet app to the existing FastAPI app you need to call `flet_fastapi.app_manager.start()` on app start and `flet_fastapi.app_manager.shutdown()` on shutdown. Use the way that best suites you: lifespan (in the example above) or app events.\n\n`app_manager.start()` method starts background tasks cleaning up expired sessions and OAuth flow states.\n\n`app_manager.shutdown()` method removes any temporary files created by a Flet app.\n\n## Running the app in production\n\nIt is recommended to run FastAPI in production with [Hypercorn](https://github.com/pgjones/hypercorn/) which is ASGI web server, but it is also possible to run FastAPI apps with [Gunicorn](https://gunicorn.org/) which is a WSGI server, but has more features, like passing proxy headers.\n\nTo install Gunicorn:\n\n```\npip install gunicorn\n```\n\nStart `gunicorn` with:\n\n```\ngunicorn -k uvicorn.workers.UvicornWorker counter:app\n```\n\n## Reference\n\n### Environment variables\n\n`FLET_SECRET_KEY` - secret key to sign upload requests. Must be set if upload directory is configured.\n\n`FLET_SESSION_TIMEOUT` - the number of seconds to keep session alive after user has disconnected. Default is 3,600 seconds.\n\n`FLET_OAUTH_STATE_TIMEOUT` - OAuth state lifetime, in seconds, which is a maximum allowed time between starting OAuth flow and redirecting to OAuth callback URL. Default is 600 seconds.\n\n`FLET_MAX_UPLOAD_SIZE` - max allowed size of an uploaded file, bytes.\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Flet for Fast API - serve Flet app with Fast API server",
"version": "0.20.2",
"project_urls": {
"documentation": "https://flet.dev/docs",
"homepage": "https://flet.dev",
"repository": "https://github.com/flet-dev/flet"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "30e66d44d9500a51a11c734f16a5d818793ef8555b3b2d46798fd4c1252de83a",
"md5": "d4e7d45fa0e2a70a83f0e6fb4f962384",
"sha256": "efe83eae7000cfb468792dc330edd6d3bade28592511a88a7c7be540868bf7d4"
},
"downloads": -1,
"filename": "flet_fastapi-0.20.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d4e7d45fa0e2a70a83f0e6fb4f962384",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 15857,
"upload_time": "2024-02-18T00:57:37",
"upload_time_iso_8601": "2024-02-18T00:57:37.297047Z",
"url": "https://files.pythonhosted.org/packages/30/e6/6d44d9500a51a11c734f16a5d818793ef8555b3b2d46798fd4c1252de83a/flet_fastapi-0.20.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1aeb0b0852a1a01519d19a665a9af40c160ae7a68724bfb27392711e091aaaef",
"md5": "2d2a8a047bddc01e465563fe3fed7a16",
"sha256": "cd3a9c97af9f7f28ac9d86110d9e95f82c1e57f0cbc24287ae6383308bdda93d"
},
"downloads": -1,
"filename": "flet_fastapi-0.20.2.tar.gz",
"has_sig": false,
"md5_digest": "2d2a8a047bddc01e465563fe3fed7a16",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 13793,
"upload_time": "2024-02-18T00:57:46",
"upload_time_iso_8601": "2024-02-18T00:57:46.119080Z",
"url": "https://files.pythonhosted.org/packages/1a/eb/0b0852a1a01519d19a665a9af40c160ae7a68724bfb27392711e091aaaef/flet_fastapi-0.20.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-18 00:57:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "flet-dev",
"github_project": "flet",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"appveyor": true,
"lcname": "flet-fastapi"
}