Debian Codegraph @

README.md

raw symbols SCIP
1 django-sass
2 ===========
3
4 The absolute simplest way to use [Sass](https://sass-lang.com/) with Django.
5 Pure Python, minimal dependencies, and no special configuration required.
6
7 [Source code on GitHub](https://github.com/coderedcorp/django-sass)
8
9
10 Status
11 ------
12
13 | | |
14 |------------------------|----------------------|
15 | Python Package | [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-sass)](https://pypi.org/project/django-sass/) [![PyPI - Django Version](https://img.shields.io/pypi/djversions/django-sass)](https://pypi.org/project/django-sass/) [![PyPI - Wheel](https://img.shields.io/pypi/wheel/django-sass)](https://pypi.org/project/django-sass/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/django-sass)](https://pypi.org/project/django-sass/) [![PyPI](https://img.shields.io/pypi/v/django-sass)](https://pypi.org/project/django-sass/) |
16 | Build | [![Build Status](https://dev.azure.com/coderedcorp/cr-github/_apis/build/status/django-sass?branchName=main)](https://dev.azure.com/coderedcorp/cr-github/_build/latest?definitionId=10&branchName=main) [![Azure DevOps tests (branch)](https://img.shields.io/azure-devops/tests/coderedcorp/cr-github/10/main)](https://dev.azure.com/coderedcorp/cr-github/_build/latest?definitionId=10&branchName=main) [![Azure DevOps coverage (branch)](https://img.shields.io/azure-devops/coverage/coderedcorp/cr-github/10/main)](https://dev.azure.com/coderedcorp/cr-github/_build/latest?definitionId=10&branchName=main) |
17
18
19 Installation
20 ------------
21
22 1. Install from pip.
23
24 ```
25 pip install django-sass
26 ```
27
28 2. Add to your `INSTALLED_APPS` (you only need to do this in a dev environment,
29 you would not want this in your production settings file, although it adds zero
30 overhead):
31
32 ```python
33 INSTALLED_APPS = [
34 ...,
35 'django_sass',
36 ]
37 ```
38
39 3. Congratulations, you're done 😀
40
41
42 Usage
43 -----
44
45 In your app's static files, use Sass as normal. The only difference is that
46 you can **not** traverse upwards using `../` in `@import` statements. For example:
47
48 ```
49 app1/
50 |- static/
51 |- app1/
52 |- scss/
53 |- _colors.scss
54 |- app1.scss
55 app2/
56 |- static/
57 |- app2/
58 |- scss/
59 |- _colors.scss
60 |- app2.scss
61 ```
62
63 In `app2.scss` you could reference app1's and app2's `_colors.scss` import as so:
64
65 ```scss
66 @import 'app1/scss/colors';
67 @import 'app2/scss/colors';
68 // Or since you are in app2, you can reference its colors with a relative path.
69 @import 'colors';
70 ```
71
72 Then to compile `app2.scss` and put it in the `css` directory,
73 run the following management command (the `-g` will build a source map, which
74 is helpful for debugging CSS):
75
76 ```
77 python manage.py sass app2/static/app2/scss/app2.scss app2/static/app2/css/app2.css -g
78 ```
79
80 Or, you can compile the entire `scss` directory into
81 a corresponding `css` directory. This will traverse all subdirectories as well:
82
83 ```
84 python manage.py sass app2/static/app2/scss/ app2/static/app2/css/
85 ```
86
87 In your Django HTML template, reference the CSS file as normal:
88
89 ```html
90 {% load static %}
91 <link href="{% static 'app2/css/app2.css' %}" rel="stylesheet">
92 ```
93
94 ✨✨ **Congratulations, you are now a Django + Sass developer!** ✨✨
95
96 Now you can commit those CSS files to version control, or run `collectstatic`
97 and deploy them as normal.
98
99 For an example project layout, see `testproject/` in this repository.
100
101
102 Watch Mode
103 ----------
104
105 To have `django-sass` watch files and recompile them as they change (useful in
106 development), add the ``--watch`` flag.
107
108 ```
109 python manage.py sass app2/static/app2/scss/ app2/static/app2/css/ --watch
110 ```
111
112
113 Example: deploying compressed CSS to production
114 -----------------------------------------------
115
116 To compile minified CSS, use the `-t` flag to specify compression level (one of:
117 "expanded", "nested", "compact", "compressed"). The default is "expanded" which
118 is human-readable.
119
120 ```
121 python manage.py sass app2/static/app2/scss/ app2/static/app2/css/ -t compressed
122 ```
123
124 You may now optionally commit the CSS files to version control if so desired,
125 or omit them, whatever fits your needs better. Then run `collectsatic` as normal.
126
127 ```
128 python manage.py collectstatic
129 ```
130
131 And now proceed with deploying your files as normal.
132
133
134 Limitations
135 -----------
136
137 * `@import` statements must reference a path relative to a path in
138 `STATICFILES_FINDERS` (which will usually be an app's `static/` directory or
139 some other directory specified in `STATICFILES_DIRS`). Or they can reference a
140 relative path equal to or below the current file. It does not support
141 traversing up the filesystem (i.e. `../`).
142
143 Legal imports:
144 ```scss
145 @import 'file-from-currdir';
146 @import 'subdir/file';
147 @import 'another-app/file';
148 ```
149 Illegal imports:
150 ```scss
151 @import '../file';
152 ```
153
154 * Only supports `-g`, `-p`, and `-t` options similar to `pysassc`. Ideally
155 `django-sass` will be as similar as possible to the `pysassc` command line
156 interface.
157
158 Feel free to file an issue or make a pull request to improve any of these
159 limitations. 🐱‍💻
160
161
162 Why django-sass?
163 ----------------
164
165 Other packages such as
166 [django-libsass](https://github.com/torchbox/django-libsass) and
167 [django-sass-processor](https://github.com/jrief/django-sass-processor), while
168 nice packages, require `django-compressor` which itself depends on several other
169 packages that require compilation to install.
170
171 Installing `django-compressor` in your production web server requires a LOT of
172 extra bloat including a C compiler. It then will compile the Sass on-the-fly
173 while rendering the HTML templates. This is a wasteful use of CPU on your web
174 server.
175
176 Instead, `django-sass` lets you compile the Sass locally on your machine
177 *before* deploying, to reduce dependencies and CPU time on your production web
178 server. This helps keep things fast and simple.
179
180 * If you simply want to use Sass in development without installing a web of
181 unwanted dependencies, then `django-sass` is for you.
182 * If you don't want to deploy any processors or compressors to your production
183 server, then `django-sass` is for you.
184 * If you don't want to change the way you reference and serve static files,
185 then `django-sass` is for you.
186 * And if you want the absolute simplest installation and setup possible for
187 doing Sass, `django-sass` is for you too.
188
189 django-sass only depends on libsass (which provides pre-built wheels for
190 Windows, Mac, and Linux), and of course Django (any version).
191
192
193 Programmatically Compiling Sass
194 -------------------------------
195
196 You can also use `django-sass` in Python to programmatically compile the sass.
197 This is useful for build scripts and static site generators.
198
199 ```python
200 from django_sass import compile_sass
201
202 # Compile scss and write to output file.
203 compile_sass(
204 inpath="/path/to/file.scss",
205 outpath="/path/to/output.css",
206 output_style="compressed",
207 precision=8,
208 source_map=True
209 )
210 ```
211
212 For more advanced usage, you can specify additional sass search paths outside
213 of your Django project by using the `include_paths` argument.
214
215 ```python
216 from django_sass import compile_sass, find_static_paths
217
218 # Get Django's static paths.
219 dirs = find_static_paths()
220
221 # Add external paths.
222 dirs.append("/external/path/")
223
224 # Compile scss and write to output file.
225 compile_sass(
226 inpath="/path/to/file.scss",
227 outpath="/path/to/output.css",
228 output_style="compressed",
229 precision=8,
230 source_map=True,
231 include_paths=dirs,
232 )
233 ```
234
235 Contributing
236 ------------
237
238 To set up a development environment, first check out this repository, create a
239 venv, then:
240
241 ```
242 (myvenv)$ pip install -r requirements-dev.txt
243 ```
244
245 Before committing, run static analysis tools:
246
247 ```
248 (myvenv)$ black .
249 (myvenv)$ flake8
250 (myvenv)$ mypy
251 ```
252
253 Then run the unit tests:
254
255 ```
256 (myvenv)$ pytest
257 ```
258
259
260 Changelog
261 ---------
262
263 #### 1.1.0
264 * New: Now compiles `.sass` files as well as `.scss` files.
265 * Fix bug when input path is a file and output path does not exist.
266
267 #### 1.0.1
268 * Maintanence release, no functional changes.
269 * Add additional type hints within the codebase.
270 * Tested against Django 3.1
271 * Formatted code with `black`.
272
273 #### 1.0.0
274 * New: You can now use `django_sass` APIs directly in Python.
275 * Added unit tests.
276 * Code quality improvements.
277
278 #### 0.2.0
279 * New feature: `-g` option to build a source map (when input is a file, not a
280 directory).
281
282 #### 0.1.2
283 * Fix: Write compiled CSS files as UTF-8.
284 * Change: Default `-p` precision from 5 to 8 for better support building
285 Bootstrap CSS.
286
287 #### 0.1.1
288 * Fix: Create full file path if not exists when specifying a file output.
289
290 #### 0.1.0
291 * Initial release