If you use LessCSS to stay sane while writing CSS, you probably know it can be a pain to run lessc --watch static/style.less every time you want to work on your styles. It gets even worse when you have more than one .less file.
flask-lesscss is a small Flask extension that will automatically re-render your .less files into CSS before each request if they’ve changed.
NOTE: You need to have the LessCSS gem installed for this to work.
Install flask-lesscss with pip:
pip install -e 'hg+http://email@example.com#egg=flask-lesscss'
pip install -e 'git+http://firstname.lastname@example.org#egg=flask-lesscss'
You can activate it by calling the lesscss function with your Flask app as a parameter:
from flaskext.lesscss import lesscss lesscss(app)
This will watch your app’s static media directory and automatically render .less files into .css files in the same (sub)directory.
When you deploy your app you might not want to accept the overhead of checking the modification time of your .less and .css files on each request. A simple way to avoid this is wrapping the lesscss call in an if statement:
if app.debug: from flaskext.lesscss import lesscss lesscss(app)
If you do this you’ll be responsible for rendering the .less files into CSS when you deploy in non-debug mode to your production server.