Python/Tornado - compressing static files Python/Tornado - compressing static files django django

Python/Tornado - compressing static files


Take a look at tornado_utils, it should do what you want. Especially take look at tornado_static.py

tornado_static is a module for displaying static resources in a Tornado web application.

It can take care of merging, compressing and giving URLs ideal renamings suitable for aggressive HTTP caching.


The best option I've seen so far is WebAssets.

From the documentation: webassets is a general, dependency-independent library for managing the assets of your web application. It can merge and compress your CSS and JavaScript files, supporting a wide variety of different filters, and supports working with compilers like CoffeeScript or Sass.

You can use it in standalone mode with tornado (see the specific documentation).

Set up is easy and pretty straightforward:

from webassets import Environmentstatic_directory = "../static"output_directory = "/static"my_env = Environment(static_directory, output_directory)

Of course you can customise it far better. The rest is pretty well explained in the documentation.

Main features:

  • Easy integration
  • Possible to compress static files in advance (command-line tool)
  • Possible to compress static files on the fly
  • Supports most minifying/compression libraries (JS, CSS)
  • Supports LESS/SASS compiling inside the browser
  • Supports compression of JS Templates inside the browser (Handlebars...)
  • Supports CSS sprite mapper

Example of what a template (here, Jinja2) looks like after proper configuration:

# css{% assets filters="cssmin", output="css/compiled-layout.css",    "css/custom.css",    "css/bootstrap-datepicker.css",    "css/typeahead.css" %}    <link rel="stylesheet" type="text/css" href="{{ ASSET_URL }}">{% endassets %}# js{% assets filters="jsmin", output="js/lib/compiled-libs.js",    "js/lib/jquery-2.1.1.min.js",    "js/lib/jquery-ui.min.js",    "js/lib/bootstrap.min.js",    "js/lib/bootstrap-datepicker.js",    "js/lib/d3.min.js",    "js/lib/typeahead.bundle.min.js",    "js/lib/moment.min.js",    "js/lib/handlebars-v2.0.0.js",    "js/global.js" %}    <script type="text/javascript" src="{{ ASSET_URL }}"></script>{% endassets %}

I've been using WebAssets tied to Flask for a year with no hassle and it's perfectly reliable, plus well-maintained: it's been there for several years, and last commit to date was yesterday.


As far as I understand from looking into the open-source tornado projects there is no standard and canonical way of doing static-files minification and compression in the "tornado world".

The different options I've seen, are:

Only the first two options are tornado-specific. Other tools need to be tied up with Tornado template rendering and static files serving mechanisms manually.