Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ?_trace=1 working with CSV and streaming CSVs #1351

Closed
simonw opened this issue May 31, 2021 · 1 comment
Closed

Get ?_trace=1 working with CSV and streaming CSVs #1351

simonw opened this issue May 31, 2021 · 1 comment

Comments

@simonw
Copy link
Owner

simonw commented May 31, 2021

I think it's worth getting ?_trace=1 to work with streaming CSV - this would have helped me spot this issue a long time ago.

Originally posted by @simonw in #1349 (comment)

@simonw
Copy link
Owner Author

simonw commented May 31, 2021

Since traces only work with text/html and JSON at the moment, the easiest way to do this will be to wrap generated CSV in a HTML page in a textarea if the user specified ?_trace=1:

if "text/html" in content_type and b"</body>" in accumulated_body:
extra = json.dumps(trace_info, indent=2)
extra_html = f"<pre>{extra}</pre></body>".encode("utf8")
accumulated_body = accumulated_body.replace(b"</body>", extra_html)
elif "json" in content_type and accumulated_body.startswith(b"{"):
data = json.loads(accumulated_body.decode("utf8"))
if "_trace" not in data:
data["_trace"] = trace_info
accumulated_body = json.dumps(data).encode("utf8")
await send({"type": "http.response.body", "body": accumulated_body})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant