Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  143] [ 3]  / answers: 1 / hits: 26049  / 9 Years ago, mon, may 11, 2015, 12:00:00

I'm sending a huge JSON string (with jsonify in Flask) to my webpage pretty often, so I would like to reduce the data. The easiest option is probably to remove all line breaks and space characters, but just to give you an example of this:



Normal jsonify: 361KB

Removing all line breaks and space characters: 118KB (wow).

Zip the original file: 35KB (double wow).



So I basically wonder if there is an easy way to come close to the 35KB. I couldn't find a solution so far which I could easily implement in python and javascript (to decompress).



Right now, I send around 4-5MB of data every second, which is - you guessed right - a little bit too much.


More From » python

 Answers
22

Web requests do support GZip and you could implement it in python.



Here is someone who asked that exact question.
How to use Content-Encoding: gzip with Python SimpleHTTPServer



According to the flask-compress repo




The preferred solution is to have a server (like Nginx) automatically compress the static files for you.




But you can do it in flask: https://github.com/colour-science/flask-compress.



If you go the gzip route you will not need to remove line breaks and white space, but if you still want to then according to the flask documentation you can disable pretty print by setting JSONIFY_PRETTYPRINT_REGULAR to false.


[#66648] Friday, May 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mahogany

Total Points: 645
Total Questions: 107
Total Answers: 98

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
mahogany questions
;