Flask Series: Optimizations

Wednesday, Dec 16, 2015 22:21 · 403 words · 2 minutes read python flask flask series

Flask Series

  1. Prepare the Environment
  2. Structure the Application
  3. Application Configuration
  4. Templating
  5. Model
  6. Testing
  7. Views and Web Forms
  8. Error Management
  9. Security
  10. Optimizations
  11. Healthcheck and Monitoring
  12. Internationalization
  13. Deployment

In this blog post I will describe how to optimize your Flask application.

The website speed is very important. It could break the success of your site, no matter you provide your users great content and functionality. If your website pages load slower than your competitors’ ones, it is possible they get the attention and not your site. Especially when all of the major search engines like fast pages.

There are lots of performance measurement services you could use:

  • Google PageSpeed Insights
  • YSlow
  • WebPageTest

The services above will provide you with tips and tricks what and how can be improved in your site. In this post I will show how Flask allows you to resolve some of the major performance issues in your website.


Most of browsers nowadays support gzip compression and negotiate for all HTTP requests. Gzip compression allows to reduce the size of the response by 70-90%. It is very easy to accomplish this in a Flask application. The Flask-Compress extension compresses the application’s response with gzip.


You could find tests that verifies the app compress configuration and if it works correctly under the tests/compress_tests.py module.


Caching data in your application allows you to reduce calls to database, additional computation, etc. The Flask-Cache extension will help you solve this problem.

There are several different cache types you could use: simple, memcached, redis, filesystem, etc.. The complete list and its specific configurations can be found under the Configuring Flask-Cache section.

Now that you have the extension installed and configured you could use it in the following way:




Content Delivery Networks (CDN) addresses the problem serving content to users all around the world. They are large distributed system deployed in multiple data centers. The Flask-CDN extension provides developers with means to serve static content from CDNs.

The extension replaces the Flask url_for function. When it is invoked from your templates the flask_cdn.url_for function is called instead.

Combine Files

Combining asset files reduces calls to the server, which leads to significant performance boost of your website. The Flask-Assets extension allows you to bundle files in your Flask application.

In the next blog post I will describe how to monitor your Flask application.

The complete demo application, described in this blog post, can be found here.
comments powered by Disqus