Generating CSV and XLSX reports
The DataFrame
object has built-in to_csv()
and to_excel()
methods that save its data in CSV or XLSX files, respectively. But the main goal is to create an API service that will return these files as responses. The following implementation shows how a FastAPI service can return a CSV file containing a list of respondent profiles:
from fastapi.responses import StreamingResponse import pandas as pd from io import StringIO from survey.repository.respondent import RespondentRepository @router.get("/respondents/csv", response_description='csv') async def create_respondent_report_csv(): repo = RespondentRepository() result = await repo.get_all_respondent() ids = [ item["id"] for item in result ] fnames = [ f'{item["fname"]}' for item in result...