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 ~9-18x 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 - 21.94 us
premailer 3.7.0 - 403.48 us (x18.39)
inlinestyler 0.2.4 - 2.84 ms (x129.44)
pynliner 0.8.0 - 3.34 ms (x152.23)
And for a more realistic email:
css_inline 0.7.7 - 446.8 us
premailer 3.7.0 - 4.10 ms (x9.18)
inlinestyler 0.2.4 - 75.71 ms (x169.43)
pynliner 0.8.0 - 124.13 ms (x277.78)
You can take a look at the benchmarks’ code at benches/bench.py file. The results above were measured with stable rustc 1.57.0, Python 3.9.9 on i8700K, and 32GB RAM.
Python support
css_inline supports Python 3.6, 3.7, 3.8, 3.9, and 3.10.
Extra materials
If you want to know how this library was created & how it works internally, you could take a look at these articles:
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.8-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ac823207f0faf2f6d5eff98c193f8f461733b80620104fba98918140638b5bc |
|
MD5 | de3256e79e1997762a44e0308af4376f |
|
BLAKE2b-256 | 4e27e05fca61e95056d3b6e30caa847f14c40c7d2087e1880cecdb16013345cf |
Hashes for css_inline-0.7.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8f4e8e6d8b46fd8fd36bc858f95a44115e1eba7c155b5907f20b3ca7de8ce3d |
|
MD5 | 550d0ba178733ab09c12fec089b3f389 |
|
BLAKE2b-256 | 2bc93db2d0a1baaeab1aa0515b832c586b433ca652a3977602e5c489f028aab3 |
Hashes for css_inline-0.7.8-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73a550784259afe648ec1acd152106dce8f50bdaacde51c0d2d706dec10df05a |
|
MD5 | 0640a0e2c02774eca8d6674e5f4182b4 |
|
BLAKE2b-256 | 279745535902222e1806e1ca997f70d897f046e5bb4dbc04da97dfc46d71634d |
Hashes for css_inline-0.7.8-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 748b78011d2f6451e3e571c944cdc80710bb122567e8aba20535853904cf2dd0 |
|
MD5 | 3a6b189805755dbe3b344b2b7b554311 |
|
BLAKE2b-256 | 79cd8100a5c988b8beaa87b1eb9a5457815b5d424eff68aa2fe8382cdc1476dc |
Hashes for css_inline-0.7.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c14216481d06b3cfe08b0c7119776cf5c693d037c07e0cd47fd98f6c6bc2c3e |
|
MD5 | e84e9cb3c427c52b540b9c95b1b4947a |
|
BLAKE2b-256 | 4a005b0f52f45a45b02806e82ef0fa99cac37401dd27e038f1c5633d580f1633 |
Hashes for css_inline-0.7.8-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 538ee19c0d3ba0afb317ae05f0f25152e223ffb8f143b245ec9590dc2353e969 |
|
MD5 | 4b783ac48c6e834380fb99642479c8e6 |
|
BLAKE2b-256 | 5482909e1b97ae07ec03423f776eb6b615ab221d706dfec66da7d8ebbe7be5af |
Hashes for css_inline-0.7.8-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 423c47620447ae03afbbb324a2aa035714c49606631dce2dd4a98f4f98f824a6 |
|
MD5 | 6cd8c7f24d5d506abaeb202054bd4865 |
|
BLAKE2b-256 | 8c74c1d7a4d0218df6578a5b84b3d92ffa2e6ffd4a067b94451cd4fb67cd4693 |
Hashes for css_inline-0.7.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7133704c467e12ffea61e79feeac0351b083c54b742cbf813901d3126d7df301 |
|
MD5 | 9e6619552e4195e4f6ed4283e9d36353 |
|
BLAKE2b-256 | 41091b3b6f8e8171cbfed7d22749a0b31a79892ff7306d66ebe8424a7b5dfb27 |
Hashes for css_inline-0.7.8-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8791100f2beac327bff96547861d2bc9244eed0fae59fb08d954804b933d7989 |
|
MD5 | 7d9fd58ea2b195cd3dc5ec2298f744d9 |
|
BLAKE2b-256 | 5d98d2a2c3eaceffc83e298364dd071365aa7d7804ebdacbbae48cea31b73391 |
Hashes for css_inline-0.7.8-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55de3b27443f5d86e8f2fc6649f24867300f6e30ae0431f61767c00a95d5d414 |
|
MD5 | 7180e4fe3d5ef2684316b277d61dcb88 |
|
BLAKE2b-256 | 0bef8c584f43f4c28a22810ab299bc3f2fef5bc1da6d17523f0fce46fe4d8770 |
Hashes for css_inline-0.7.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df2d33cce16a42ed59d8500b879072e2672e1b81b524e2c246ef65c1f81bc170 |
|
MD5 | beeabc369d7e7740f323f077f2f28ec8 |
|
BLAKE2b-256 | 5d6fcd8b046d676b2208140d0f030a125e36d2937d8518efc252926b1e758696 |
Hashes for css_inline-0.7.8-cp37-cp37m-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d32ac89ad808ee1cb3adb5bd6ccfb8701ecb9422e50394de880efaeb69e9ee51 |
|
MD5 | a17e0a97618abcab4a01f2ab990692e2 |
|
BLAKE2b-256 | 059cae8f76852218b39b1765e642699259abc2aebbb5f4b75838cbbe472060f6 |
Hashes for css_inline-0.7.8-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3571af87ac39afaf44bef7c70d120dbf69e8481ff1b34afbc105f3f9ff6132b1 |
|
MD5 | e234a422efb347c15eae8af35726bdd3 |
|
BLAKE2b-256 | 54a8f1a7da3abce202889b33413b59a8d4973320e506852dc120bc939dee35c6 |
Hashes for css_inline-0.7.8-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5558ec642676d07a2e977e1b971bc151b2e26b54eb9ce8d5e3639dc6f04390dd |
|
MD5 | 71fe0d6b2cca76ad5528217495a0bec5 |
|
BLAKE2b-256 | e3e88446808442299465eb11fdbe696b1e7675c9f706611b7b292796398022fb |
Hashes for css_inline-0.7.8-cp36-cp36m-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d44fed5a770d060b3c06d6bd3f555d8f84cae8bf922243891aae5209100b42f |
|
MD5 | 4b771278de83a5a08182a5f3b724f5ae |
|
BLAKE2b-256 | bb7fab6a86f115354a9aa116db0cd119050e598d7f396f85d033e0770cfc31a1 |