FletStaticFiles
Serve Flet app static files.
Text and WebAssembly files, like the web client's main.dart.wasm and
main.dart.js, are sent gzip-compressed to browsers that accept it, which
makes them about 3x smaller to download. Each file is compressed once, in a
worker thread, on its first request, and the compressed copy is kept in a
temporary directory until the server exits. Byte range requests get the
uncompressed file.
Parameters:
- assets_dir (str | None, default:
None) - Path to app's assets directory. A relative path is resolved against the current working directory. - app_name (str | None, default:
None) - PWA application name. - app_short_name (str | None, default:
None) - PWA application short name. - app_description (str | None, default:
None) - PWA application description. - web_renderer (WebRenderer, default:
WebRenderer.AUTO) - Type of web renderer, as aWebRendereror its value, e.g."canvaskit". - route_url_strategy (RouteUrlStrategy, default:
RouteUrlStrategy.PATH) - Routing URL strategy, as aRouteUrlStrategyor its value, e.g."hash". - no_cdn (bool, default:
False) - Whether not load CanvasKit, Pyodide, and fonts from CDN. - websocket_endpoint_path (str | None, default:
None) - Path of Flet app WebSocket handler. A path without a leading/is resolved against the app mount path. Defaults tows.
Inherits: fastapi.staticfiles.StaticFiles
Properties
font_manifest_jsonindexmanifest_jsonroute_url_strategy- The route URL strategy written into the servedindex.html: theroute_url_strategyargument, orFLET_WEB_ROUTE_URL_STRATEGYif set.
Methods
file_response- Respond with a static file, addingVary: Accept-Encodingto compressible ones.get_response- Respond with a static file, gzip-compressed if the browser accepts it.is_not_modified- Whether the browser's cached copy of a file is still current.lookup_path- Resolve a static file path, with SPA fallback for client-side routes.
Properties
font_manifest_jsoninstance-attribute
indexinstance-attribute
manifest_jsoninstance-attribute
route_url_strategyproperty
route_url_strategy: RouteUrlStrategyThe route URL strategy written into the served index.html: the
route_url_strategy argument, or FLET_WEB_ROUTE_URL_STRATEGY if set.
Methods
file_response
file_response(
full_path: str | os.PathLike[str],
stat_result: os.stat_result,
scope: Scope,
status_code: int = 200,
) -> ResponseRespond with a static file, adding Vary: Accept-Encoding to
compressible ones.
Unlike StaticFiles, this never responds with 304 Not Modified:
get_response() checks the browser's cached copy once it has picked
the plain or compressed file.
get_responseasync
get_response(path: str, scope: Scope) -> ResponseRespond with a static file, gzip-compressed if the browser accepts it.
Parameters:
- path (str) - Requested path, relative to the mounted static root.
- scope (Scope) - ASGI request scope.
Returns:
- Response - The compressed file for a
200response of a compressible file to a request that accepts gzip and has noRangeheader; otherwise the response ofStaticFiles. Either is304 Not Modifiedif the browser has that same file cached.
is_not_modified
is_not_modified(
response_headers: Headers, request_headers: Headers
) -> boolWhether the browser's cached copy of a file is still current.
If-None-Match, when sent, decides alone, so a cached copy in the
other encoding, which has the same Last-Modified, is never taken as
current.
Parameters:
- response_headers (Headers) - Headers of the response that would be sent.
- request_headers (Headers) - Headers of the request.
lookup_path
lookup_path(
path: str,
) -> tuple[str, os.stat_result | None]Resolve a static file path, with SPA fallback for client-side routes.
With the path route URL strategy, route-like paths (no extension, or
.html) that don't match a file fall back to index.html so the
Flutter client can handle routing. Asset-like paths (any other
extension) resolve to a real 404.
With the hash strategy, routes live in the URL fragment and never
reach the server, so there is no fallback: any path that doesn't match
a file resolves to a real 404 (with a 404.html from the assets
directory as its body, if there is one), while the mount root still
serves index.html.
Parameters:
- path (str) - Requested path, relative to the mounted static root (e.g.
"about","sample.json").
Returns:
- tuple[str, os.stat_result | None] - A
(full_path, stat_result)tuple. Whenstat_resultisNone, Starlette responds with404.