|
| 1 | +Using with Flask |
| 2 | +================ |
| 3 | + |
| 4 | +This guide explains how to use libsass with Flask_ web framework. |
| 5 | +:mod:`sassutils` package provides several tools that can be integrated |
| 6 | +to web applications written in Flask. |
| 7 | + |
| 8 | +.. _Flask: http://flask.pocoo.org/ |
| 9 | + |
| 10 | +.. contents:: |
| 11 | + |
| 12 | + |
| 13 | +Directory layout |
| 14 | +---------------- |
| 15 | + |
| 16 | +Imagine the project contained in such directory layout: |
| 17 | + |
| 18 | +- :file:`setup.py` |
| 19 | +- :file:`myapp/` |
| 20 | + |
| 21 | + - :file:`__init__.py` |
| 22 | + - :file:`static/` |
| 23 | + |
| 24 | + - :file:`sass/` |
| 25 | + - :file:`css/` |
| 26 | + - :file:`templates/` |
| 27 | + |
| 28 | +SASS/SCSS files will go inside :file:`myapp/static/sass/` directory. |
| 29 | +Compiled CSS files will go inside :file:`myapp/static/css/` directory. |
| 30 | +CSS files can be regenerated, so add :file:`myapp/static/css/` into your |
| 31 | +ignore list like :file:`.gitignore` or :file:`.hgignore`. |
| 32 | + |
| 33 | + |
| 34 | +Defining manifest |
| 35 | +----------------- |
| 36 | + |
| 37 | +The :mod:`sassutils` defines a concept named :dfn:`manifest`. |
| 38 | +Manifest is building settings of SASS/SCSS. It specifies some paths |
| 39 | +related to building SASS/SCSS: |
| 40 | + |
| 41 | +- The path of the directory which contains SASS/SCSS source files. |
| 42 | +- The path of the directory compiled CSS files will go. |
| 43 | +- The path, is exposed to HTTP (through WSGI), of the directory that |
| 44 | + will contain compiled CSS files. |
| 45 | + |
| 46 | +Every package may have their own manifest. Paths have to be relative |
| 47 | +to the path of the package. |
| 48 | + |
| 49 | +For example, in the project the package name is :mod:`myapp`. |
| 50 | +The path of the package is :file:`myapp/`. The path of SASS/SCSS directory |
| 51 | +is :file:`static/sass/` (relative to the package directory). |
| 52 | +The path of CSS directory is :file:`static/css/`. |
| 53 | +The exposed path is :file:`/static/css`. |
| 54 | + |
| 55 | +This settings can be represented as the following manifests:: |
| 56 | + |
| 57 | + { |
| 58 | + 'myapp': ('static/sass', 'static/css', '/static/css') |
| 59 | + } |
| 60 | + |
| 61 | +As you can see the above, the set of manifests are represented in dictionary. |
| 62 | +Keys are packages names. Values are tuples of paths. |
| 63 | + |
| 64 | + |
| 65 | +Building SASS/SCSS for each request |
| 66 | +----------------------------------- |
| 67 | + |
| 68 | +.. seealso:: |
| 69 | + |
| 70 | + Flask --- `Hooking in WSGI Middlewares`__ |
| 71 | + The section which explains how to integrate WSGI middlewares to |
| 72 | + Flask. |
| 73 | + |
| 74 | + Flask --- :ref:`flask:app-dispatch` |
| 75 | + The documentation which explains how Flask dispatch each |
| 76 | + request internally. |
| 77 | + |
| 78 | + __ http://flask.pocoo.org/docs/quickstart/#hooking-in-wsgi-middlewares |
| 79 | + |
| 80 | +In development, to manually build SASS/SCSS files for each change is |
| 81 | +so tiring. :class:`~sassutils.wsgi.SassMiddleware` makes the web |
| 82 | +application to automatically build SASS/SCSS files for each request. |
| 83 | +It's a WSGI middleware, so it can be plugged into the web app written in |
| 84 | +Flask. |
| 85 | + |
| 86 | +:class:`~sassutils.wsgi.SassMiddleware` takes two required parameters: |
| 87 | + |
| 88 | +- The WSGI-compliant callable object. |
| 89 | +- The set of manifests represented as dictionary. |
| 90 | + |
| 91 | +So:: |
| 92 | + |
| 93 | + from flask import Flask |
| 94 | + from sassutils.wsgi import SassMiddleware |
| 95 | + |
| 96 | + app = Flask(__name__) |
| 97 | + |
| 98 | + app.wsgi_app = SassMiddleware(app.wsgi_app, { |
| 99 | + 'myapp': ('static/sass', 'static/css', '/static/css') |
| 100 | + }) |
| 101 | + |
| 102 | +And then, if you want to link a compiled CSS file, use :func:`~flask.url_for()` |
| 103 | +function: |
| 104 | + |
| 105 | +.. sourcecode:: html+jinja |
| 106 | + |
| 107 | + <link href="{{ url_for('static', filename='css/style.scss.css') }}" |
| 108 | + rel="stylesheet" type="text/css"> |
| 109 | + |
| 110 | +.. note:: |
| 111 | + |
| 112 | + The linked filename is :file:`style.scss.css`, not just :file:`style.scss`. |
| 113 | + All compiled filenames have trailing ``.css`` suffix. |
| 114 | + |
| 115 | + |
| 116 | +Building SASS/SCSS for each deployment |
| 117 | +-------------------------------------- |
| 118 | + |
| 119 | +.. note:: |
| 120 | + |
| 121 | + This section assumes that you use distribute_ (:mod:`setuptools`) |
| 122 | + for deployment. |
| 123 | + |
| 124 | +.. seealso:: |
| 125 | + |
| 126 | + Flask --- :ref:`flask:distribute-deployment` |
| 127 | + How to deploy Flask application using distribute_. |
| 128 | + |
| 129 | +If libsass has been installed in the :file:`site-packages` (for example, |
| 130 | +your virtualenv), :file:`setup.py` script also gets had new command |
| 131 | +provided by libsass: :class:`~sassutils.distutils.build_sass`. |
| 132 | +The command is aware of ``sass_manifests`` option of :file:`setup.py` and |
| 133 | +builds all SASS/SCSS sources according to the manifests. |
| 134 | + |
| 135 | +Add these arguments to :file:`setup.py` script:: |
| 136 | + |
| 137 | + setup( |
| 138 | + # ..., |
| 139 | + setup_requires=['libsass >= 0.2.0'], |
| 140 | + sass_manifests={ |
| 141 | + 'myapp': ('static/sass', 'static/css', '/static/css') |
| 142 | + } |
| 143 | + ) |
| 144 | + |
| 145 | +The ``setup_requires`` option makes sure that the libsass is installed |
| 146 | +in :file:`site-packages` (for example, your virtualenv) before |
| 147 | +:file:`setup.py` script. That means: if you run :file:`setup.py` script |
| 148 | +and libsass isn't installed yet at the moment, it will automatically |
| 149 | +install libsass first. |
| 150 | + |
| 151 | +The ``sass_manifests`` specifies the manifests for libsass. |
| 152 | + |
| 153 | +Now :program:`setup.py build_sass` will compile all SASS/SCSS files |
| 154 | +in the specified path and generates compiled CSS files into the specified |
| 155 | +path (according to the manifests). |
| 156 | + |
| 157 | +If you use it with ``sdist`` or ``bdist`` command, a packed archive also |
| 158 | +will contain compiled CSS files! |
| 159 | + |
| 160 | +.. sourcecode:: console |
| 161 | + |
| 162 | + $ python setup.py build_sass sdist |
| 163 | + |
| 164 | +You can add aliases to make these commands to always run ``build_sass`` |
| 165 | +command before. Make :file:`setup.cfg` config: |
| 166 | + |
| 167 | +.. sourcecode:: ini |
| 168 | + |
| 169 | + [aliases] |
| 170 | + sdist = build_sass sdist |
| 171 | + bdist = build_sass bdist |
| 172 | + |
| 173 | +Now it automatically builds SASS/SCSS sources and include compiled CSS files |
| 174 | +to the package archive when you run :program:`setup.py sdist`. |
| 175 | + |
| 176 | +.. _distribute: http://pypi.python.org/pypi/distribute |
0 commit comments