1

I am having a problem getting libsass to compile SASS files. I can get it to compile SCSS files just fine.

I have a Flask application and I've installed libsass and I am using the libsass binding sass. It works well but is missing some functionality - it can't compile SASS but it can compile SCSS. Here's what I did:

In my applicaiton.py file I have

from flask import Flask
import sass

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    sass.compile(dirname=('client/sass', 'client/css'), source_comments=True)
    app.run()

My two test files are test_scss.scss, which works:

$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}

And test_sass.sass:

$font-stack:    Helvetica, sans-serif
$primary-color: #333

body
  font: 100% $font-stack
  color: $primary-color

The documentation says sass.complie can take in SASS code but I can only get it to take SCSS code. That is, when I run my application, only the .scss file gets compile to CSS. The directory structure of my applicaiton after sass.compile runs is:

  • client
    • css
      • app.css
      • test_scss.css
    • lib
    • sass
      • test_sass.sass
      • test_scss.scss
  • server
    • templates
    • application.py

Someone suggested that I just rename the SASS file to something with a .scss extension. Like test_sass.scss:'

$font-stack:    Helvetica, sans-serif
$primary-color: #333

body
  font: 100% $font-stack
  color: $primary-color

However, when I do this, I just get the following error:

src/web-internal/server/applicaiton.py", line 8, in <module>
    sass.compile(dirname=('client/sass', 'client/css'), source_comments=True)
  File "/usr/local/lib/python3.4/site-packages/sass.py", line 516, in compile
    raise CompileError(v)
sass.CompileError: b"Error: top-level variable binding must be terminated by ';'\n        on line 2 of src/web-internal/server/../client/sass/test_sass.scss\n>> $primary-color: #333\n   ^\n"

I'd like to use SASS, and would perfer not to settle for SCSS. Does anyone have any suggestions? I feel like there is something in the sass documentation that I am missunderstanding.

Mike
  • 2,514
  • 2
  • 23
  • 37
  • Can you show the folder structure of your app? – doru Jul 28 '15 at 14:55
  • Your `sass`, `css` folders should be in the `static` named folder. See [here](http://hongminhee.org/libsass-python/frameworks/flask.html#id3) some info. – doru Jul 28 '15 at 15:06
  • Thanks @doru but, do you think this is the problem even though my Flask app is started using `app = Flask(__name__,static_url_path="",static_folder=CLIENT_FOLDER)` where `CLIENT_FOLDER` is the `client` folder (`CLIENT_FOLDER=os.path.join(os.path.realpath(os.path.dirname(__file__)),"..","client")`) – Mike Jul 28 '15 at 15:10
  • People are mostly going to be guessing unless you share a [Minimum, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – dirn Jul 28 '15 at 15:14
  • Thanks @dirn, any suggestions what to remove, add, or verify in this example? – Mike Jul 28 '15 at 15:17
  • 1
    You provide an exception but no traceback. You reference a file containing a line of code but don't share the file. These are good places to start. – dirn Jul 28 '15 at 21:52
  • Thanks @dirn, I adjusted the question a little bit in response to your comments. – Mike Jul 29 '15 at 12:34

3 Answers3

0

I never got this working with sass.compile but I found a work around.

...

if __name__ == '__main__':
    # Start sassc with --watch
    sass_cmd = subprocess.Popen(['sass', '--watch', 'client/sass' + ':' + 'client/css'])
    ... 
Mike
  • 2,514
  • 2
  • 23
  • 37
0

The Python libsass package's compile() method supports the indented kwarg to tell the compiler if the given input is indented .sass or regular bracketed .scss.

If you are using indented .sass as your input, just use it like this:

indented_sass_string = """

$font-stack:    Helvetica, sans-serif
$primary-color: #333

body
  font: 100% $font-stack
  color: $primary-color
"""

css = sass.compile(string=indented_sass_string, indented=True)

I assume that this will work if you are passing entire directories. I am not sure what happens if you try to compile an entire directory which has a mix of .sass and .scss files.

See the docs: https://sass.github.io/libsass-python/sass.html#sass.compile

kbuilds
  • 991
  • 11
  • 19
0

If you want to compile any file, then you can do it like this

import sass
sass.compile(dirname=("client/sass", "client/css"))

libsass-python documentation.

Al Mahdi
  • 635
  • 1
  • 9
  • 16