I would like to include a sass compiler in my Flask application. Is there a generally accepted way of doing this?
5 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.

- 13,173
- 4
- 53
- 54
-
Interesting. Will this compile every time, or only in debug mode? – kasperhj Jan 19 '13 at 14:40
-
Yes, it will compile in production too. What I wanted to stress that webassets CSS compiler filters work in debug mode as well, otherwise the browser wouldn't be able to parse served files. Other filters like JS and CSS minifiers are enabled only in production mode. – Audrius Kažukauskas Jan 19 '13 at 16:12
-
Wouldn't it be a better idea to _only_ have the code compile in debug-mode, so that production-mode would not "suffer" from the compilation overhead? – kasperhj Jan 19 '13 at 18:49
-
9Ah, I misunderstood your previous question. webassets saves the compiled results and also caches them to prevent unneeded recompilation, unless the changes were made to the source files. In debug mode filters are usually disabled completely to not interfere with development (debugging minified JS code would be a pain). CSS compiler filters are exception to this rule, since usually you want their results (CSS files) all the time. – Audrius Kažukauskas Jan 19 '13 at 19:03
-
3For anyone finding this answer: this assumes that your scss files exist in static/ relative to your root path. If you want them somewhere else, like assets/, add these lines `assets.url = app.static_url_path; assets.directory = app.static_folder; assets.append_path('assets')`. The paths are relative to the root path; don't prepend `/`. See https://github.com/miracle2k/flask-assets/issues/35#issuecomment-11840908 – btown Jul 10 '15 at 18:10
-
1Unfortunately it seems it doesn’t like `@import` statements. It won’t recompile the SCSS if you modify a file that’s `@importe`d; only if it’s the main file (listed in the bundle). – bfontaine Oct 16 '15 at 12:40
-
2@bfontaine I found out the workaround, you can use the depends argument when creating a bundle to monitor some other files. See https://webassets.readthedocs.org/en/latest/bundles.html – tiagosilva Apr 19 '16 at 18:27
-
1I just tried using this method for my GAE project, and it seems that it is impossible to do because GAE disallows file creation at runtime. – ThePloki Oct 02 '16 at 16:17
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.

- 5,338
- 3
- 38
- 43
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.

- 538
- 1
- 8
- 24
Currently, exist a better approach for this issue, the extion Flask-Scss.
You just have to install it: pip install Flask-Scss
And instanciate a Scss object after configuring the application (probably in your manage.py
file):
from flask import Flask
from flask.ext.scss import Scss
app = Flask(__name__)
Scss(app)
By default, the extension will look for your .scss files in {app.root_dir}/assets/scss
or {app.root_dir}/assets
and will put the generate .css files in {default_static_dir}/css
or {default_static_dir}
.

- 1,983
- 3
- 18
- 29
-
11The `from flask.ext.scss` import does not anymore work. Instead, I needed to use `from flask_scss import Scss`. (In the [Flask-Scss docs](http://pythonhosted.org/Flask-Scss/), there's a deprecation warning but it's not clear that it refers to this new import method.) – Gino Mempin Apr 13 '18 at 03:10
-
1Does this still work? I am looking at using [https://github.com/material-components/material-components-web] but can't manage to get this to generate my css and js. – BashOverride Jun 07 '18 at 03:46
-
Libsass is a good solution for this.
# Your __init__.py file
from flask import Flask
from sassutils.wsgi import SassMiddleware
app = Flask(__name__)
app.wsgi_app = SassMiddleware(app.wsgi_app, {
'myapp': ('static/sass', 'static/css', '/static/css')
})
# Your HTML (Jinja) file
<link href="{{ url_for('static', filename='css/style.scss.css') }}"
rel="stylesheet" type="text/css">

- 729
- 10
- 12
-
i followed the site in your link, but aside from a "FutureWarning: `strip_extension` was not specified, defaulting to `False`." warning when i run the app, nothing happens. The "css/style.scss.css" is not found. Nothing compiles, too. – goleon Sep 27 '22 at 14:22
-
-
yes, i did. I just found my error. When setting the manifests, i had the name of the FLASK_APP script, it should have been just "app". So for the script to work its `'app': ('static/sass', 'static/css', '/static/css')` for me. Thanks! – goleon Sep 27 '22 at 14:53