How to Stream a Large Python Dictionary as a JSON Download

23 views
Skip to first unread message

Ali

unread,
May 28, 2025, 11:30:01 PM5/28/25
to py4web
Hi,

I need to make a Python dictionary available as a downloadable JSON file. Since the dictionary can be quite large, I’d like to stream the response so it downloads in chunks rather than all at once. How can I achieve that?

P.S. The following code works, but it appears to send the entire file in one go rather than streaming it:

    json_str = json.dumps(data_dict, indent=2)

    response.headers['Content-Type'] = 'application/json'  # Removed to force file download
    response.headers['Content-Disposition'] = f'attachment; filename=topic_data.json'
    return json_str

Thanks!
Reply all
Reply to author
Forward
0 new messages