14

For django projects there is an awesome tool called django-compressor. It combines all js or css files under compress template tag into single cached file, like this:

{% load compress %}

{% compress css %}
<link rel="stylesheet" href="/static/css/one.css" type="text/css" charset="utf-8">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css" charset="utf-8">
{% endcompress %}

I'm wondering if there is anything like this for tornado projects? Or maybe any workaround/alternative solution?

I've found this project on github, but it's no longer maintained.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

3 Answers3

7

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.

Aldarund
  • 17,312
  • 5
  • 73
  • 104
  • Thank you very much! It was a bit tricky to configure due to the lack of the documentation, but it works. – alecxe Apr 27 '13 at 13:02
3

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 Environment
static_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.

Jivan
  • 21,522
  • 15
  • 80
  • 131
  • @alecxe Precision: I know it's a complete asset management framework and not a simple library as you asked, but I think it's quality-made and is an alternative worth digging. Tell me what you think when you have the opportunity to look further into it. – Jivan Dec 28 '14 at 22:55
  • 1
    Thank you for an interesting option. I think it can be used in standalone mode with tornado. I hope I'll have time to evaluate it today - will get back to you. – alecxe Dec 29 '14 at 14:20
  • I've looked through several projects that use `tornado` + `webassets` (for example, [this one](https://github.com/bashwork/speleo/tree/master/speleo.service/service)). The integration is pretty easy and transparent. Good option. Thanks again! – alecxe Dec 30 '14 at 00:11
2

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195