Trigger file download with FastAPI #3289
-
When web app runs on Windows 11 local browser (Brave and Edge tested), files are successfully downloaded into the app directory with no file save dialog appearing in the web browser. When web app runs on Fly.io virtual machine, no files are downloaded. How to download from the remote virtual machine?
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
When running locally, "/download" appears in the web browser but not when running remotely on the virtual machine. |
Beta Was this translation helpful? Give feedback.
-
I fooled myself. The locally running app wrote a csv file to a "download" folder on my local machine, as it should, and I mistook that file as a download, which never really happened. I was able to get download to work on my local machine and on a virtual machine using import os
from pathlib import Path
from contextlib import asynccontextmanager
import flet as ft
import flet.fastapi as flet_fastapi
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
import analyzer as an # a local module
def set_environment(variable, default):
"""
reads then sets environment variable
:param variable: variable name (string, typically upper case)
:param default: variable default value
"""
value = os.getenv(variable, default=default)
os.environ[variable] = default if value is None else value
# %%%%%%%% Get or set secret key--REQUIRED for upload!!! %%%%%%%%%%%%%%
set_environment(variable="FLET_SECRET_KEY", default=os.urandom(12).hex())
# ######################## API FUNCTIONS #######################
@asynccontextmanager
async def lifespan(app: FastAPI):
""" Serves Flet App from FastAPI """
await flet_fastapi.app_manager.start() # executes before the application starts taking requests
yield
await flet_fastapi.app_manager.shutdown() # executes after the application finishes handling requests
# instantiate FastAPI
app = FastAPI(lifespan=lifespan)
@app.get(path='/download/{csv_file}')
def send_csv(csv_file: str):
""" downloads CSV file from an.RESULTS_DIR """
csv_path = str(Path.cwd().joinpath(an.RESULT_DIR).joinpath(csv_file))
if os.path.exists(csv_path):
return FileResponse(path=csv_path,
media_type='text/csv',
filename=Path(csv_path).name,
headers={"Content-Disposition": "attachment"})
else:
raise HTTPException(status_code=404, detail=f"{csv_path} not found")
def main(page: ft.Page):
analyzer = an.Analyzer.construct(page) # function from Analyzer class
page.add(analyzer)
app.mount(path='/', app=flet_fastapi.app(main, assets_dir=an.ASSETS_DIR, upload_dir=an.UPLOAD_DIR)) |
Beta Was this translation helpful? Give feedback.
-
since you also tried downloading I got a questions. For me downloading works, when calling page.launch_url(...) once. |
Beta Was this translation helpful? Give feedback.
I fooled myself. The locally running app wrote a csv file to a "download" folder on my local machine, as it should, and I mistook that file as a download, which never really happened.
I was able to get download to work on my local machine and on a virtual machine using
FastAPI @app.get()
as follows.