1
app=FastAPI()

@app.get('/zip_format')
def format_check():
    try:
        with zipfile.ZipFile('files.zip') as file:
            return {"Status:Ok"}
    except zipfile.BadZipFile:
        return {'Error: Zip file is corrupted'}

@app.get('/zip_display')
def convert_df():
    with zipfile.ZipFile("files.zip", mode="r") as archive:
        df = pd.DataFrame([(zinfo.filename,
                                datetime.datetime(*zinfo.date_time),
                                zinfo.file_size) for zinfo in archive.filelist],
                              columns=["filename",
                                       "date_time",
                                       "file_size"])
        json_compatible_item_data = jsonable_encoder(df)
        return JSONResponse(content=json_compatible_item_data)

I want to check my input zip file format and return status which I am able to do in my first method named as format_check.

How can I run my second method as a get method in FastApi to convert the data into dataframe and display the contents of zip file as json on UI?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
SARTHAK
  • 51
  • 1
  • 7
  • 1
    Does this answer your question? [How to return a csv file/Pandas DataFrame in JSON format using FastAPI?](https://stackoverflow.com/questions/71203579/how-to-return-a-csv-file-pandas-dataframe-in-json-format-using-fastapi) – Chris Jan 16 '23 at 14:25
  • 1
    Related answers can also be found [here](https://stackoverflow.com/a/73694164/17865804), as well as [here](https://stackoverflow.com/a/73580096/17865804), [here](https://stackoverflow.com/a/71478057/17865804) and [here](https://stackoverflow.com/a/70655118/17865804). – Chris Jan 16 '23 at 14:26
  • @Chris No...Problem with this approach is that i don't have any global df defined – SARTHAK Jan 16 '23 at 14:46

0 Answers0