from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import io
import pandas as pd
app = FastAPI()
@app.get("/get_csv")
async def get_csv():
df = pd.DataFrame(dict(col1 = 1, col2 = 2), index=[0])
stream = io.StringIO()
df.to_csv(stream, index = False)
response = StreamingResponse(iter([stream.getvalue()]),
media_type="text/csv"
)
response.headers["Content-Disposition"] = "attachment; filename=export.csv"
return response
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter