Skip to main content

filtercss

filtercss lets you filter unused rules from CSS files, like https://purgecss.com/ but in Python.

Installation

pip install filtercss

Example

Filtering is simple, just use filter_css.

from filtercss import filter_css

css = """
.test1, .test3 {
    color: red;
}
.test2 {
    color: blue;
}
"""

html = """
<a class="test1 some-other-cls">test</a>
"""

res_css = filter_css(css, html)
assert "test1" in res_css
assert "test2" not in res_css

Limitations

filtercss uses tinycss2. Also filtercss will not be able to anticipate what DOM elements might be inserted by any JavaScript and thus will happily filter out the unused CSS rules they might want to use. filtercss uses BeautifulSoup4 to parse HTML and to detect whether a CSS selector in a rule matches against a given file. However, BeautifulSoup4 might not support all of the CSS selectors, in which case filtercss is conservative and includes the given CSS rule. filtercss is also not optimal in that it includes a full CSS rule if any of its selectors matches and it does not filter out unmatched selectors from the rule. Note that this may change in the future.

License

MIT License

Release files for filtercss 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for filtercss 0.0.2
File Size Uploaded
filtercss-0.0.2.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for filtercss 0.0.2
File Interpreter ABI Platform
filtercss-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.4 kB

Release files / filtercss-0.0.2.tar.gz

Download URL filtercss-0.0.2.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7e71fc38f64b9707cb746fc7d9305bc9e167f10ecd9340374ba4347ea09ee41f
BLAKE2b-256 checksum
How to use checksums
a83fe169a935ba32e41842f779accb2c8c70576b5dbbe1d46ec836e3f2ce5384
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / filtercss-0.0.2-py3-none-any.whl

Download URL filtercss-0.0.2-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bd72e5a53a81bcb720d1b6d22d34896f2f2c637457bf746b5bbd7c4bece0cf1a
BLAKE2b-256 checksum
How to use checksums
8c9f102a8721818af3de3081ec6aa3216f807b120602ebbd321f75ddbf374850
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page