Assets

Coaster provides a simple asset management system for semantically versioned assets using the semantic_version and webassets libraries. Many popular libraries such as jQuery are not semantically versioned, so you will have to be careful about assumptions you make around them.

class coaster.assets.SimpleSpec(expression)[source]
class coaster.assets.VersionedAssets[source]

Semantic-versioned assets. To use, initialize a container for your assets:

from coaster.assets import VersionedAssets, Version
assets = VersionedAssets()

And then populate it with your assets. The simplest way is by specifying the asset name, version number, and path to the file (within your static folder):

assets['jquery.js'][Version('1.8.3')] = 'js/jquery-1.8.3.js'

You can also specify one or more requirements for an asset by supplying a list or tuple of requirements followed by the actual asset:

assets['jquery.form.js'][Version('2.96.0')] = (
    'jquery.js', 'js/jquery.form-2.96.js')

You may have an asset that provides replacement functionality for another asset:

assets['zepto.js'][Version('1.0.0-rc1')] = {
    'provides': 'jquery.js',
    'bundle': 'js/zepto-1.0rc1.js',
    }

Assets specified as a dictionary can have three keys:

Parameters:
  • provides (string or list) – Assets provided by this asset
  • requires (string or list) – Assets required by this asset (with optional version specifications)
  • bundle (string or Bundle) – The asset itself

To request an asset:

assets.require('jquery.js', 'jquery.form.js==2.96.0', ...)

This returns a webassets Bundle of the requested assets and their dependencies.

You can also ask for certain assets to not be included even if required if, for example, you are loading them from elsewhere such as a CDN. Prefix the asset name with ‘!’:

assets.require('!jquery.js', 'jquery.form.js', ...)

To use these assets in a Flask app, register the assets with an environment:

from flask_assets import Environment
appassets = Environment(app)
appassets.register('js_all', assets.require('jquery.js', ...))

And include them in your master template:

{% assets "js_all" -%}
  <script type="text/javascript" src="{{ ASSET_URL }}"></script>
{%- endassets -%}
require(*namespecs)[source]

Return a bundle of the requested assets and their dependencies.

exception coaster.assets.AssetNotFound[source]

No asset with this name