question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Better way to send an in-memory file?

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Reactions:16
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
rehanhaidercommented, Jun 12, 2021

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

  response = StreamingResponse(buffer, media_type="application/zip")
  response.headers["Content-Disposition"] = "attachment; filename=images.zip"

  return response
6reactions
scriptatorcommented, May 7, 2021

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 the StreamingResponse with a normal iterator.

return PlainTextResponse(csv_str, media_type="text/csv")
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found