Old question but I was searching for this and it was the first result on Google. The link to the answer of Leon has a solution not for Flask and also it is old. With Python 3 now we can do all in few lines with the standard libraries (and Flask):
from flask import make_response, json
import gzip
@app.route('/data.json')
def compress():
very_long_content = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}]
content = gzip.compress(json.dumps(very_long_content).encode('utf8'), 5)
response = make_response(content)
response.headers['Content-length'] = len(content)
response.headers['Content-Encoding'] = 'gzip'
return response
With gzip.compress we have directly a byte string compressed and it is required as input a byte string. Then, as the link from Leon, we make a custom response saying that the content is a gzip so the browser will decompress by itself.
For decoding in Javascript using a JQuery ajax request there isn't any particular difference from a standard request:
$.ajax({
url: '/data.json',
dataType: 'json',
success: function(data) {
console.log(data);
}
})
Note that this snippet compress and then send the long content. You should consider the amount of time that it takes to compress the content (especially in this case that we have very long content), so be sure to set an appropriate level of compression that doesn't require more time to compress + send than send the long content as it is.
My use case was that I sent the big content from a slow connection, so I had all the benefits to compress the content before send it.