2

I'm trying to return a CSV response to a GET query (end library requirement..), and I am hoping to find a way to do this without writing the CSV to disk. A similar question has been answered here, but I have one additional bit of complexity where I'm returning data from another function.

import json
import time
from typing import Optional
import io

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.responses import StreamingResponse

import xarray as xr
import pandas as pd
import numpy as np
import gc

async def get_intensity_csv(x,y):
    #Use select on the xarray datasource, returning a data array and then convert to a pandas dataframe
    #Index
    da = ds["intensity"].sel(longitude=y, latitude=x, method="nearest")

    df = da.to_dataframe()
    df.drop(columns=['latitude', 'longitude'], inplace=True)

    #pandas to csv ready for streaming to the client
    stream = io.StringIO()
    resp_csv = df.to_csv(stream, index = False)

    return resp_csv

The above is being called from:

#create an app.get that returns intensity as a csv stream
@app.get("/intensity_csv/{x}/{y}", tags=["intensity_csv"])
async def get_intensity_csv(x: float, y: float):
    res = await int_process(x,y)
    return StreamingResponse(io.StringIO(res), media_type="text/csv")

The error I'm receiving is:

return StreamingResponse(io.StringIO(res), media_type="text/csv")
TypeError: initial_value must be str or None, not dict

I believe my issue is getting data from the first sample via these couple of lines back to the def async get_intensity_csv

#pandas to csv ready for streaming to the client
    stream = io.StringIO()
    resp_csv = df.to_csv(stream, index = False)

I'm not sure how to pass the content of the dataframe back so I can then use StreamingResponse to deliver the csv to the client.

Chris
  • 18,724
  • 6
  • 46
  • 80
anakaine
  • 1,188
  • 2
  • 14
  • 30
  • 1
    Does this answer your question? [How to stream DataFrame using FastAPI without saving the data to csv file?](https://stackoverflow.com/questions/73688641/how-to-stream-dataframe-using-fastapi-without-saving-the-data-to-csv-file) – Chris Dec 21 '22 at 04:48

0 Answers0