Skip to content

Commit

Permalink
Add upload_endpoint_path into flet.fastapi.app (flet-dev#2954)
Browse files Browse the repository at this point in the history
* Update app.py

* Add description for upload_endpoint_path
  • Loading branch information
timok19 authored and 50Bytes-dev committed May 18, 2024
1 parent 4e94bad commit c4a239d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions sdk/python/packages/flet/src/flet/fastapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from typing import Awaitable, Callable, Optional, Union

from fastapi import Request, WebSocket
from flet_core.page import Page
from flet_core.types import WebRenderer

from flet.fastapi.flet_app import (
DEFAULT_FLET_OAUTH_STATE_TIMEOUT,
DEFAULT_FLET_SESSION_TIMEOUT,
Expand All @@ -12,8 +15,6 @@
from flet.fastapi.flet_oauth import FletOAuth
from flet.fastapi.flet_static_files import FletStaticFiles
from flet.fastapi.flet_upload import FletUpload
from flet_core.page import Page
from flet_core.types import WebRenderer


def app(
Expand All @@ -27,6 +28,7 @@ def app(
use_color_emoji: bool = False,
route_url_strategy: str = "path",
upload_dir: Optional[str] = None,
upload_endpoint_path: Optional[str] = None,
max_upload_size: Optional[int] = None,
secret_key: Optional[str] = None,
session_timeout_seconds: int = DEFAULT_FLET_SESSION_TIMEOUT,
Expand All @@ -46,6 +48,7 @@ def app(
* `use_color_emoji` (bool) - whether to load a font with color emoji. Default is `False`.
* `route_url_strategy` (str) - routing URL strategy: `path` (default) or `hash`.
* `upload_dir` (str) - an absolute path to a directory with uploaded files.
* `upload_endpoint_path` (str, optional) - absolute URL of upload endpoint, e.g. `/upload`.
* `max_upload_size` (str, int) - maximum size of a single upload, bytes. Unlimited if `None`.
* `secret_key` (str, optional) - secret key to sign and verify upload requests.
* `session_timeout_seconds` (int, optional)- session lifetime, in seconds, after user disconnected.
Expand Down Expand Up @@ -82,15 +85,16 @@ async def app_handler(websocket: WebSocket):
session_handler,
session_timeout_seconds=session_timeout_seconds,
oauth_state_timeout_seconds=oauth_state_timeout_seconds,
upload_endpoint_path=upload_endpoint_path,
secret_key=secret_key,
).handle(websocket)

if upload_dir:

@fastapi_app.put(f"/{upload_endpoint}")
@fastapi_app.put(
f"/{upload_endpoint_path if upload_endpoint_path else upload_endpoint}"
)
async def upload_handler(request: Request):
if not upload_dir:
return
await FletUpload(
upload_dir=upload_dir,
max_upload_size=max_upload_size,
Expand Down

0 comments on commit c4a239d

Please sign in to comment.