Git Product home page Git Product logo

django-pyscss's Introduction

django-pyscss

A collection of tools for making it easier to use pyScss within Django.

Build Status Coverage Status

Note

This version only supports pyScss 1.3.4 and greater. For pyScss 1.2 support, you can use the 1.x series of django-pyscss.

Installation

django-pyscss supports Django 1.4+, and Pythons 2 and 3.

You may install django-pyscss off of PyPI:

pip install django-pyscss

Why do we need this?

This app smooths over a lot of things when dealing with pyScss in Django. It

  • Overwrites the import system to use Django's staticfiles app. This way you can import SCSS files from any app (or any file that's findable by the STATICFILES_FINDERS) with no hassle.
  • Configures pyScss to work with the staticfiles app for its image functions (e.g. inline-image and sprite-map).
  • It provides a django-compressor precompile filter class so that you can easily use pyScss with django-compressor without having to bust out to the shell. This has the added benefit of removing the need to configure pyScss through its command-line arguments AND makes it possible for the exceptions and warnings that pyScss emits to bubble up to your process so that you can actually know what's going on.

Rendering SCSS manually

You can render SCSS manually from a string like this:

from django_pyscss import DjangoScssCompiler

compiler = DjangoScssCompiler()
compiler.compile_string(".foo { color: green; }")

You can render SCSS from a file like this:

from django_pyscss import DjangoScssCompiler

compiler = DjangoScssCompiler()
compiler.compile('css/styles.scss')

The file needs to be able to be located by staticfiles finders in order to be used.

The DjangoScssCompiler class is a subclass of scss.Compiler that injects the DjangoExtension. DjangoExtension is what overrides the import mechanism.

DjangoScssCompiler also turns on the CompassExtension by default, if you wish to turn this off you do so:

from django_pyscss import DjangoScssCompiler
from django_pyscss.extensions.django import DjangoExtension

compiler = DjangoScssCompiler(extensions=[DjangoExtension])

For a list of options that DjangoScssCompiler accepts, please see the pyScss API documentation.

Using in conjunction with django-compressor

django-pyscss comes with support for django-compressor. All you have to do is add it to your COMPRESS_PRECOMPILERS setting. :

COMPRESS_PRECOMPILERS = (
    # ...
    ('text/x-scss', 'django_pyscss.compressor.DjangoScssFilter'),
    # ...
)

Then you can just use SCSS like you would use CSS normally. :

{% compress css %}
<link rel="stylesheet" type="text/x-scss" href="{% static 'css/styles.css' %}">
{% endcompress %}

If you wish to provide your own compiler instance (for example if you wanted to change some settings on the DjangoScssCompiler), you can subclass DjangoScssFilter. :

# myproject/scss_filter.py
from django_pyscss import DjangoScssCompiler
from django_pyscss.compressor import DjangoScssFilter

class MyDjangoScssFilter(DjangoScssFilter):
    compiler = DjangoScssCompiler(
        # Example configuration
        output_style='compressed',
    )

# settings.py
COMPRESS_PRECOMPILERS = (
    # ...
    ('text/x-scss', 'myproject.scss_filter.MyDjangoScssFilter'),
    # ...
)

Running the tests

You can run the tests by running.

$ python setup.py test

Please note that this will collecstatic into tmp/static/ automatically as some of the tests require the staticfiles to have been collected.

django-pyscss's People

Contributors

acatton avatar deshipu avatar gavinwahl avatar mrunge avatar philipgarnero avatar r1chardj0n3s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-pyscss's Issues

@import doesn't find files from STATICFILES_DIRS

I'm probably doing something wrong, because this is what django-pyscss is supposed to do, but I can't get it to work with STATICFILES_DIRS. Long story short, we have removed our bootstrap scss files from our repository, and packaged them in a separate packaged, which we then import in settings.py and add the directory with all the files to STATICFILES_DIRS with correct prefix. Django-pyscss fails to find those files. It also fails to report the failure and just behaves as if everything went fine -- until you try to use any of the variables defined in those files.

Here is a link to the patch with the issue:
https://review.openstack.org/#/c/116583/

I will prepare a minimal Django app that demonstrates the problem.

@import doesn't work

Hi, this is really great project, first of all, I'd like to say thank you for your effort for this project.
I tried this module for my Django project, basically it works, but only @import function doesn't work. Do you have any idea for this issue?

tests are broken

I think it's because of a new version of django-compressor.

Traceback (most recent call last):
  File "setup.py", line 58, in <module>
    'Programming Language :: Python :: 3.4',
  File "/opt/python/2.6.9/lib/python2.6/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/opt/python/2.6.9/lib/python2.6/distutils/dist.py", line 975, in run_commands
    self.run_command(cmd)
  File "/opt/python/2.6.9/lib/python2.6/distutils/dist.py", line 995, in run_command
    cmd_obj.run()
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/setuptools/command/test.py", line 172, in run
    self.run_tests()
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/setuptools/command/test.py", line 193, in run_tests
    testRunner=self._resolve_as_ep(self.test_runner),
  File "/opt/python/2.6.9/lib/python2.6/unittest.py", line 816, in __init__
    self.parseArgs(argv)
  File "/opt/python/2.6.9/lib/python2.6/unittest.py", line 843, in parseArgs
    self.createTests()
  File "/opt/python/2.6.9/lib/python2.6/unittest.py", line 849, in createTests
    self.module)
  File "/opt/python/2.6.9/lib/python2.6/unittest.py", line 613, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/opt/python/2.6.9/lib/python2.6/unittest.py", line 598, in loadTestsFromName
    test = obj()
  File "/home/travis/build/fusionbox/django-pyscss/testproject/runtests.py", line 19, in runtests
    failures = test_runner.run_tests(['tests'])
  File "build/bdist.linux-x86_64/egg/discover_runner/runner.py", line 154, in run_tests
  File "build/bdist.linux-x86_64/egg/discover_runner/runner.py", line 116, in setup_databases
  File "build/bdist.linux-x86_64/egg/discover_runner/runner.py", line 289, in setup_databases
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/db/backends/creation.py", line 271, in create_test_db
    load_initial_data=False)
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/core/management/__init__.py", line 150, in call_command
    return klass.execute(*args, **defaults)
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/core/management/base.py", line 231, in execute
    self.validate()
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/core/management/validation.py", line 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/db/models/loading.py", line 158, in get_app_errors
    self._populate()
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/db/models/loading.py", line 67, in _populate
    self.load_app(app_name)
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/db/models/loading.py", line 88, in load_app
    models = import_module('.models', app_name)
  File "/home/travis/build/fusionbox/django-pyscss/.tox/py26-dj14/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/travis/build/fusionbox/django-pyscss/.eggs/django_compressor-2.0-py2.6.egg/compressor/models.py", line 1, in <module>
    from compressor.conf import CompressorConf  # noqa
  File "/home/travis/build/fusionbox/django-pyscss/.eggs/django_compressor-2.0-py2.6.egg/compressor/conf.py", line 5, in <module>
    from django.template.utils import InvalidTemplateEngineError
ImportError: No module named utils
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/opt/python/2.6.9/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/opt/python/2.6.9/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
    info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/opt/python/2.6.9/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/opt/python/2.6.9/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
    info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/opt/python/2.6.9/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/opt/python/2.6.9/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
    info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/opt/python/2.6.9/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/opt/python/2.6.9/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
    info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/opt/python/2.6.9/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/opt/python/2.6.9/lib/python2.6/multiprocessing/util.py", line 258, in _exit_function
    info('process shutting down')
TypeError: 'NoneType' object is not callable

STATIC_ROOT is a required setting

If STATIC_ROOT isn't set, pyscss will write static assets to the CWD, which is bad news bears. Can we enforce the existence of STATIC_ROOT somehow?

Creating unwanted assets dir

How can I stop widgy from creating the assets dir ?
It doesn't follow my directory structure at all.
Could you add a workaround to prevent this ?

ImportError: No module named pathlib

New in 2.0.0: import Py3K stdlib module pathlib even on Python 2.x where it does not exist

Traceback (most recent call last):
  File "/opt/stack/new/horizon/manage.py", line 23, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/dist-packages/django_pyscss/__init__.py", line 1, in <module>
    from .compiler import DjangoScssCompiler  # NOQA
  File "/usr/local/lib/python2.7/dist-packages/django_pyscss/compiler.py", line 4, in <module>
    from pathlib import PurePath
ImportError: No module named pathlib

import not finding inside app

Hi,

I am trying to import a scss file from one of my apps from my main .scss file.
If I try @import "/myapp/css/myfile.scss";works fine
but if I try @import "myfile.scss";it doesn't find it, showing me the error:

SassImportError at /myapp/base/
Couldn't find anything to import: myfile.scss
Extensions: <DjangoExtension>, <CompassExtension>
Search path:

on line 1 of css/main.scss

Any idea of what can be going on?
Thanks for your help :)

handle_import breaks sub-imports with relative filenames

For example, in bootstrap.scss there's a number of imports with a relative directory path. For example, in bootstrap/scss/bootstrap.scss there is "@import bootstrap/variables". This is currently broken in handle_import:

    if original_path.is_absolute():
        # Remove the beginning slash
        search_path = original_path.relative_to('/').parent
    elif rule.source_file.origin:
        search_path = rule.source_file.origin
    else:
        search_path = original_path.parent

    basename = original_path.stem

The removal of the original_path parent information in the second case means the file cannot be located.

Should throw an error if django-compressor version is < 1.3

django-compressor==1.3 introduced the filter feature that django_pyscss.compressor.DjangoScssFilter relies on. We can't just put it in install_requires in the setup.py because some people might want to use django-pyscss without compressor.

pyScss 1.2.0.post3 breaks django-pyscss

I don't have time to investigate it right now, but there is something weird unicode problems going on with final_cont and fcont inside the Compiler function.

Python3 support

Hypothetically the codebase already supports Python3, but I can't get the Travis tests to pass. You can see the error here. The problem is that while django-compressor portends to support Python3, I can't manage to install it from pip, which definitely causes problems for running the tests that depend on compressor existing.

Apparently, django-compressor has fixed this in django-compressor/django-compressor@0e3eeab. When that fix is released, then we can support Python3.

ImportError: cannot import name dequote

I just tried it with a pre-release version of pyScss 1.3.0.a1, and I'm getting this error:

>>> import django_pyscss.compressor.DjangoScssFilter
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/sheep/horizon/.venv/lib/python2.7/site-packages/django_pyscss/compressor.py", line 8, in <module>
     from django_pyscss.scss import DjangoScss
  File "/home/sheep/horizon/.venv/lib/python2.7/site-packages/django_pyscss/scss.py", line 9, in <module>
    from scss import (
ImportError: cannot import name dequote

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.