[QUESTION] Better way to send an in-memory file?
See original GitHub issueI have pandas dataframe I want to send as a csv
file to the client. I am currently doing this:
file = StringIO()
df_solution.to_csv(file)
return StreamingResponse(
iter([file.getvalue()]),
...
)
This works but I don’t know if its the bast way. Intuitively you might be able to send the buffer directly, or at least avoid creating a single value iterator from a list. FileResponse
asks for a path
so I guess its not what I want. Not a pressing issue but its not obvious what the best solution is in this case.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:7 (1 by maintainers)
Top Results From Across the Web
11 Best Ways to Transfer, Share or Send Large Files in 2022
Discover the 11 Best Ways to Transfer, Send or Share large files. Pros & Cons, Free & Paid tools. Services you can start...
Read more >recommendation, store file in memory and send it by email
I am generating a report in excel from a query to a database, I would like to generate that report, store it in...
Read more >Design In-Memory File System - Leetcode 588 - YouTube
In this video, we introduce how to solve the "Design In- Memory File System" question which is used by big tech companies like...
Read more >How to fix: Memory stick says "File too large" - YouTube
Do you get the error message " File too large" when you try to copy files to your USB memory stick, even though...
Read more >How To Ask Questions The Smart Way - Catb.org
This guide will teach you how to ask questions in a way more likely to get you ... source file or patch), and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have used the below snippet in situations where my files are in-memory (BytesIO) but want to send the file as an attachment that can be downlaoded
If you want to generate the csv string with pandas upfront and then send it in the response (i.e. non-async), you can use
PlainTextResponse
which should also perform better than theStreamingResponse
with a normal iterator.