I have a generator function that generates CSVs and as per Flask's documentation I pass that function inside the Response object.
app.route("/stream")
...
def generate():
yield ",".join(result.keys()) #Header
for row in result:
yield ",".join(["" if v is None else str(v) for v in row])
return current_app.response_class(stream_with_context(generate()))
When I go to read the response inside my Request
I get response back as one giant string as opposed to getting line by line so it can be written to a csvwriter
s = requests.Session()
with s.get(
urllib.parse.urljoin(str(current_app.config["API_HOST_NAME"]), str("/stream")),
headers=None,
stream=True,
) as resp:
for line in resp.iter_lines():
if line:
print(line) #All the rows are concatenated in one big giant String
cw.writerow(str(line, "utf-8").split(","))
CodePudding user response:
As you can see in the docs you posted:
yield f"{','.join(row)}\n" # <=== Newline at the end!
the new line is provided in the yield statement. If you don't put a new line, you won't get one in the response. Just add it with '\n'
.