I'm looking for a simple Python script that can minify CSS as part of a web-site deployment process. (Python is the only scripting language supported on the server and full-blown parsers like CSS Utils are overkill for this project).
Basically I'd like jsmin.py for CSS. A single script with no dependencies.
Any ideas?
This seemed like a good task for me to get into python, which has been pending for a while. I hereby present my first ever python script:
I believe this to work, and output it tests fine on recent Safari, Opera, and Firefox. It will break CSS hacks other than the underscore & /**/ hacks! Do not use a minifier if you have a lot of hacks going on (or put them in a separate file).
Any tips on my python appreciated. Please be gentle though, it's my first time. :-)
There is a nice online tool cssminifier which has also an API which is pretty simple and easy to use. I made a small python script that posts the CSS file content to that tool's API, returns the minifed CSS and saves it into a file "style.min.css". I like it because it is a small code that may be nicely integrated in an automated deployment script:
In case someone landed on this question and is using Django, there is a commonly used package for this matter called Django Compressor:
In the webassets docs you can find links to multiple compressors and compilers. From that list I have chosen pyScss, which also minifies the resulting CSS.
If you need just a CSS compressor you can try csscompressor:
A more generic tool is css-html-prettify:
There is a port of YUI's CSS compressor available for python.
Here is its project page on PyPi: http://pypi.python.org/pypi/cssmin/0.1.1
I don't know of any ready made python css minifiers, but like you said css utils has the option. After checking and verifying that the license allows for it, you could go through the source code and snip out the portions that do the minifying yourself. Then stick this in a single script and voila! There you go.
As a head start, the csscombine function in .../trunk/src/cssutils/script.py seems to do the work of minifying somewhere around line 361 (I checked out revision 1499). Note the boolean function argument called "minify".