[PYTHON] Send CSS compressed to Gzip with Flask

I wanted to improve the problem that bootstrap.min.css was slow to load, so I wondered how I could send css with gzip ... and I found out that it was successful, so I'll take note of it.

Make a View

For the time being, let's create a new View. This time we will send a Gzip compressed version of bootstrap.min.css, so create a URI with the name /bootstrap.gz.css.

@app.route("/bootstrap.gz.css")

Gzip compression

Then load the Gzip-compressed CSS file. (Probably it's okay to read a regular CSS file and compress it with the gzip module)

fp = open("static/css/bootstrap.min.css.gz")
content = fp.read()
fp.close()

Add a bit to the header

This is no good, so let's add some effort to the header. This time it is compressed with GZIP, so add the information to the Content-Encoding header.

res = make_response(content)
res.headers["Content-Type"] = "text/css"
res.headers["Content-Encoding"] = "gzip"

Putting these together gives the following code. This time I sent a compressed version of CSS, but I think you can do the same with Javascript and images ...!

@view.route("/bootstrap.gz.css")
def Bootstrap():
    fp = open("static/css/bootstrap.min.css.gz")
    content = fp.read()
    fp.close()

    res = make_response(content)
    res.headers["Content-Type"] = "text/css"
    res.headers["Content-Encoding"] = "gzip"

    return res

Please refer to it!

Recommended Posts

Send CSS compressed to Gzip with Flask
Send msgpack with ajax to flask (werkzeug) environment
When you want to send an object with requests using flask
Send data to DRF API with Vue.js
How to upload with Heroku, Flask, Python, Git (4)
Send a message to LINE with Python (LINE Notify)
How to send a message to LINE with curl
Sample to send slack notification with python lambda
Flow to complete Slack authentication with Flask (Python)
I want to transition with a button in flask
How to upload with Heroku, Flask, Python, Git (Part 3)
Output log to console with Flask + Nginx on Docker
Try to make RESTful API with MVC using Flask 1.0.2
How to upload with Heroku, Flask, Python, Git (Part 1)
termux × AWS Send smartphone location information to AWS with IoT
How to upload with Heroku, Flask, Python, Git (Part 2)
Convert 202003 to 2020-03 with pandas
Serialization with cPickle, gzip
Easy to use Flask
Send email with Django
IP restrictions with Flask
Send email with Python
Hello world with flask
Programming with Python Flask
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 2]
How to deploy a web app made with Flask to Heroku
How to make a Cisco Webex Teams BOT with Flask
How to install NPI + send a message to line with python
I tried various methods to send Japanese mail with Python
Send experiment results (text and images) to slack with Python
Understanding how to use Jinja2 makes development with Flask smarter
From environment construction to deployment for flask + Heroku with Docker
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 1]
Send push notifications to iOS apps with Python2 (with sample code)
Until API made with Flask + MySQL is converted to Docker