librelist archives

« back to archive

Flask-Assets and Heroku

Flask-Assets and Heroku

From:
Freedom Dumlao
Date:
2013-05-29 @ 17:33
Does anyone out there have a good process for using Flask-Assets (or even
just webassets) with Heroku? I'm trying to find a good way to pre-build the
assets before pushing to Heroku and then preventing it from trying to build
them again once it's been deployed.

Thanks,

- Freedom

Re: [flask] Flask-Assets and Heroku

From:
Audrius Kažukauskas
Date:
2013-05-29 @ 20:02
On Wed, 2013-05-29 at 13:33:11 -0400, Freedom Dumlao wrote:
> Does anyone out there have a good process for using Flask-Assets (or even
> just webassets) with Heroku? I'm trying to find a good way to pre-build the
> assets before pushing to Heroku and then preventing it from trying to build
> them again once it's been deployed.

I'm doing this on my Linode VPS for personal projects, but don't see any
reason why it shouldn't work for Heroku, although I may be wrong here,
have tried Heroku only once and it was long ago.

In my webapp's main .py file:

  # ...
  assets = Environment(app)
  # Disable asset rebuilding in production mode.
  if not app.debug:
      assets.auto_build = False
      assets.manifest = 'file'
  # ... Further assets configuration follows ...

I don't recall exact reason why manifest is set to 'file', but I think
it's because otherwise whole .webassets-cache/ directory would be
required.

The webapp is a proper Python package[*] and is configured to use
Flask-Script (has manage.py).  MANIFEST.in contains the following lines
(all.css and all.js is minified and concatenated CSS and JS code):

  include foobar/static/.webassets-manifest
  include foobar/static/css/all.css
  include foobar/static/js/all.js

Inside manage.py:

  # ...
  from flask.ext.assets import ManageAssets
  # ...
  manager.add_command('assets', ManageAssets())
  # ...

For deployment I am using good old shell script (again, this is for
personal projects on a single VPS), but you can use whatever suits you
best (or what Heroku provides).  The relevant part from the shell
script:

  PYTHON=venv/bin/python
  APP_NAME=foobar
  ASSETS_DIR=$APP_NAME/static

  rm -f $ASSETS_DIR/.webassets-manifest
  $PYTHON manage.py assets clean
  $PYTHON manage.py assets build --manifest $ASSETS_DIR/.webassets-manifest
  $PYTHON setup.py sdist --format=gztar

After executing this inside dist/ directory there will be a tarball of
the webapp, ready to be transfered to a server and pip-installed into a
virtualenv.

[*] http://flask.pocoo.org/docs/patterns/distribute/

HTH,
-- 
Audrius Kažukauskas
http://neutrino.lt/