Fast CSS inlining written in Rust
Project description
Blazing-fast CSS inlining for Python implemented with Mozilla’s Servo project components.
Features:
Removing style tags after inlining;
Resolving external stylesheets (including local files);
Control if style tags should be processed;
Additional CSS to inline;
Inlining multiple documents in parallel (via Rust-level threads)
The project supports CSS Syntax Level 3.
Installation
To install css_inline via pip run the following command:
pip install css_inline
Usage
To inline CSS in a HTML document:
import css_inline
HTML = """<html>
<head>
<title>Test</title>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>"""
inlined = css_inline.inline(HTML)
# HTML becomes this:
#
# <html>
# <head>
# <title>Test</title>
# <style>h1 { color:blue; }</style>
# </head>
# <body>
# <h1 style="color:blue;">Big Text</h1>
# </body>
# </html>
If you want to inline many HTML documents, you can utilize inline_many that processes the input in parallel.
import css_inline
css_inline.inline_many(["<...>", "<...>"])
inline_many will use Rust-level threads; thus, you can expect it’s running faster than css_inline.inline via Python’s multiprocessing or threading modules.
For customization options use the CSSInliner class:
import css_inline
inliner = css_inline.CSSInliner(remove_style_tags=True)
inliner.inline("...")
Performance
Due to the usage of efficient tooling from Mozilla’s Servo project (html5ever, rust-cssparser and others) this library has excellent performance characteristics. In comparison with other Python projects, it is ~6-15x faster than the nearest alternative.
For inlining CSS in the html document from the Usage section above we have the following breakdown in our benchmarks:
css_inline 0.7.0 - 25.21 us
premailer 3.7.0 - 340.89 us (x13.52)
inlinestyler 0.2.4 - 2.44 ms (x96.78)
pynliner 0.8.0 - 2.78 ms (x110.27)
And for a more realistic email:
css_inline 0.6.0 - 529.1 us
premailer 3.7.0 - 3.38 ms (x6.38)
inlinestyler 0.2.4 - 64.41 ms (x121.73)
pynliner 0.8.0 - 93.11 ms (x175.97)
You can take a look at the benchmarks’ code at benches/bench.py file. The results above were measured with stable rustc 1.47.0, Python 3.8.6 on i8700K, and 32GB RAM.
Python support
css_inline supports Python 3.6, 3.7, 3.8, and 3.9.
License
The code in this project is licensed under MIT license. By contributing to css_inline, you agree that your contributions will be licensed under its MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for css_inline-0.7.6-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1d9fb92e94da6fcf2523a6c789682a84b0dfabe38992b66f0ef15d1be46d70a |
|
MD5 | 7b7266434a81c328cf7fd8d4bf6dc712 |
|
BLAKE2b-256 | 81394774af9ec1829918156949435a914bd90d6e66b591ca5e834398b780b0de |
Hashes for css_inline-0.7.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 878c3b03e65a414669f75faf50d7aeabf1ab0e9eaaced20a4df164720a475a38 |
|
MD5 | cfe6af9f67eda9da73d44133b05e36b7 |
|
BLAKE2b-256 | 3e81450ba6d9353f32a343edd63b6678002a2a63cca3452b04d941f6cc4e115c |
Hashes for css_inline-0.7.6-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c70a2155e4e48f7ac9d61fe71e5d01db10b001908ec47dcdb62e2baaeedf2916 |
|
MD5 | 80d19150166db0ab8aa772d358319fc4 |
|
BLAKE2b-256 | 216268f724f5a103f18acf0fe7d6d0814cd41c7edad66ac65c32d914bdd7d45f |
Hashes for css_inline-0.7.6-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5e02b274c446d2833d1b6e9a2d41dcab395f923c2ea5c3cb12434ee2b63df31 |
|
MD5 | 97bee986d4747479bc2ed54fab1f646b |
|
BLAKE2b-256 | 40ba6f051af200e7648f093f5f98b2673f91a455c36ac55c38a07a864a966494 |
Hashes for css_inline-0.7.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 130ca4c52b4733adf813c9c32cd2580d1e9d96bcbbfb218b28f51c8ab7456eae |
|
MD5 | 7c6b34a6659944771728ac94f863cec8 |
|
BLAKE2b-256 | 54b77d22235d1ddc949b9a454a75bb9bb48ef654494c7c27aa41fe415f44b62d |
Hashes for css_inline-0.7.6-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cab3bb9096428e48caca336162537fa0abf90cfd6c69dcef43ea0dd11053e4b |
|
MD5 | 1c7e7e8d86b84d5ef7f23ab03169a844 |
|
BLAKE2b-256 | ae9c58f36f92900bed3d0b91f1f2d268cf071210e9a801759faa97a58dee94f3 |
Hashes for css_inline-0.7.6-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dde498993e384072f0a239fd34bafe47c5d7b7bba0f0df8a392b2ffe7d27b15c |
|
MD5 | 427c8da555cf3d477589623ac7e150f7 |
|
BLAKE2b-256 | 9e702b85e07e6338a825a850003c0562c10d62c77d8ba07356797d787a4cfaf6 |
Hashes for css_inline-0.7.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9523bb553fdc15684a8021989ac1fb350601ff358c18a5a49d3e22bab0e3ac7e |
|
MD5 | ef76fc31758990c113ee0327768b9c16 |
|
BLAKE2b-256 | 8949b6aced32bb366985189d4a3c3c962325222aed657f4913d1c90395f7a6b0 |
Hashes for css_inline-0.7.6-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4260e2933b3791035d759c94cf9648aefd1710cc002ad2b4bd523cfaa1f9c71 |
|
MD5 | 6b8eb29276b15cc058ca1f69ca24d750 |
|
BLAKE2b-256 | 65a26b9d7017063510859446ce8b69aa1f5c82fd6ae212c1f13a6cfe542fcb1c |
Hashes for css_inline-0.7.6-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 051851ff8b740c8e5389a58e5aab854816c718fb3b90b0d8055bcc59177d0f33 |
|
MD5 | c5794d9effc85d8d00ef07020314f503 |
|
BLAKE2b-256 | 6370673f3272e7e3ce71fbb065895bfb5533c7544b1686c40ca04028af709a49 |
Hashes for css_inline-0.7.6-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9eed56cd3be166fd3e453791a88aeb6019a6e3f591eede4f704c29d91cedabb8 |
|
MD5 | fcdeb4419fe0af3412a1829eaab7d78e |
|
BLAKE2b-256 | 6162c3c79226b1175214f85f13063c4dd9ce7f144338a4bf6389b93d01a533cd |
Hashes for css_inline-0.7.6-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2af1aafb248eb286f018d28eb3cc92390320fd7cb48000e7a15bf0167c0293f |
|
MD5 | 4b85d459efc342e960e4e270ac081968 |
|
BLAKE2b-256 | 2e2e448d7bc3a8fe4ffd169776d6c717548424046d758041d15df1c35eb99950 |