Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sass with Flask and jinja2

I would like to include a sass compiler in my Flask application. Is there a generally accepted way of doing this?

like image 322
kasperhj Avatar asked Jan 19 '13 08:01

kasperhj


3 Answers

Flask-Assets extension (which uses webassets library) can be used for that. Here's how to configure it to use pyScss compiler (implemented in Python) for SCSS:

from flask import Flask, render_template
from flask.ext.assets import Environment, Bundle

app = Flask(__name__)

assets = Environment(app)
assets.url = app.static_url_path
scss = Bundle('foo.scss', 'bar.scss', filters='pyscss', output='all.css')
assets.register('scss_all', scss)

And in the template include this:

{% assets "scss_all" %}
<link rel=stylesheet type=text/css href="{{ ASSET_URL }}">
{% endassets %}

SCSS files will be compiled in debug mode as well.

pyScss only supports SCSS syntax, but there are other filters (sass, scss and compass) which use the original Ruby implementation.

like image 76
Audrius Kažukauskas Avatar answered Nov 07 '22 05:11

Audrius Kažukauskas


Some things have changed since the question was answered in 2013.

You can't have scss installed at the same time as pyscss and expect the pyscss filter to work like in the accepted answer.

scss = Bundle('foo.scss', 'bar.scss', filters='pyscss', output='all.css')

I was getting an error that ended in:

File "/home/sri/crap/example/flask/lib/python2.7/site-packages/webassets/filter/pyscss.py", line 110, in setup
scss.config.STATIC_ROOT = self.static_root or self.ctx.directory

You have to remove scss (i.e. pip uninstall scss) and be sure that pyscss is installed (i.e. pip install pyscss).

Also note that you will have to set some environment variables in order to get pyscss to work as well:

app = Flask(__name__)

assets     = Environment(app)
assets.url = app.static_url_path
scss       = Bundle('index.scss', filters='pyscss', output='all.css')


assets.config['SECRET_KEY'] = 'secret!'
assets.config['PYSCSS_LOAD_PATHS'] = assets.load_path
assets.config['PYSCSS_STATIC_URL'] = assets.url
assets.config['PYSCSS_STATIC_ROOT'] = assets.directory
assets.config['PYSCSS_ASSETS_URL'] = assets.url
assets.config['PYSCSS_ASSETS_ROOT'] = assets.directory

assets.register('scss_all', scss)

see the documentation on the pyscss filter for more info: http://webassets.readthedocs.io/en/latest/builtin_filters.html#pyscss

I hope that this save someone else a lot of time because I wasted a whole day on it.

like image 20
shrewmouse Avatar answered Nov 07 '22 05:11

shrewmouse


I think the most pythonic approach is this one line solution using libsass. After you import sass simply use the compile method with the dirname keyword argument, like this:

sass.compile(dirname=('path/to/sass', 'path/to/css'))

You also have the option to set the output style, for example:

sass.compile(dirname=('path/to/sass', 'path/to/css'), output_style='compressed')

If you want to watch a file or directory for automatic compilation on every edit use boussole.

like image 10
Jacob Philpott Avatar answered Nov 07 '22 05:11

Jacob Philpott