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.7-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32d8ef72445c06e6ce1a699ba1f459d7450104a77fee2a4c860b12e42b508c1e |
|
MD5 | fcf116ff7cc593190863e74a4ef33cde |
|
BLAKE2b-256 | 293d102c1315e48ec6de2eaed70ecee32dd960b0fbc44d2a138f8f92926041a3 |
Hashes for css_inline-0.7.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c84ac25b4531c74569a50508079b3b8c7299c820a233b360ef0d1c1b37cfbcd1 |
|
MD5 | 1e8fb28916681dfef8f118cd4f5ccb30 |
|
BLAKE2b-256 | 918217ee69f7e1203b1dfa11d0cd38d78103208adfaa74558a6d5ff2919cd3c6 |
Hashes for css_inline-0.7.7-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b1f2f54ddca7df4e9d3efc449e9118f76bd5162cefcaa0ac551ebd8e0ba7845 |
|
MD5 | 167d5094a222580c8f1bebf73b186351 |
|
BLAKE2b-256 | 88f094b88566147618476004bc40a799c7a89f226dc709bdb7c608bd2d17653e |
Hashes for css_inline-0.7.7-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65bb9ff96691ac073ac176c1c41cf1443dda93dce881fb69a94215c9ea1b0223 |
|
MD5 | 7a6adb255606d402a8b08a094c6dff3f |
|
BLAKE2b-256 | 513b38d92b7dea0a0717172c111fdc4091080c1ebc3c952fa12eee4108046b72 |
Hashes for css_inline-0.7.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7607fbdb5042e3ee166d97497ed80d146a5a8026f56afbd622845dc3d2937a4 |
|
MD5 | aea6356da065240dd3ee637e83c34562 |
|
BLAKE2b-256 | b9641cd7a661a768053b3c431e9155ac98699962e716b61e71f92182380fa2df |
Hashes for css_inline-0.7.7-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5282c21c5f81bd1af0fbd09fbdeaa9c2796cab48cd810997ced12036cf159a6e |
|
MD5 | 379721be87d40c5b3d6fd681326ebd8b |
|
BLAKE2b-256 | e9738c6afb6ab890872b875cd76e41e2efbe83f60da9de3e291a8850b1a97c7e |
Hashes for css_inline-0.7.7-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f09853736618e73505451308b33b9323b3479ccecc5db3aec3431ae5793374b7 |
|
MD5 | 939e6572d7214eb9501b8eff4b020c23 |
|
BLAKE2b-256 | 26167af5f9102ac566292bb2f21a21edeb23e728ad57ee9901e7fb8ef0e3a486 |
Hashes for css_inline-0.7.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1d17f74775e0271e52abc982f809d4c83bb3e40580179298b005b6999a0714d |
|
MD5 | d067ff2d7aa412de1bf1e01b925691d5 |
|
BLAKE2b-256 | 6698abaaa5e9ceeaf52c5a63c58c52c535ad0c311d9d87af30aff4b208891139 |
Hashes for css_inline-0.7.7-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab4de3110e3f54ce52389a3549c06604458df0d31953bc38e3cc17bc4c8e4b68 |
|
MD5 | 82b020e96d686610856da181fcf2bd03 |
|
BLAKE2b-256 | fe5d3bf295e71fa81d8e43a747444b5716e3dda05fd22b6094d373267ec58d3c |
Hashes for css_inline-0.7.7-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad0f76a62e30e54201dc78340ccf7ac07ebd2537e4cbfeb1c79f56398d38957e |
|
MD5 | f5364ac909b92de4a390f355f48cf2d5 |
|
BLAKE2b-256 | 2bc8b7065777b8053a0ca78aa8c34c5616274f1788ae64b7d045df8f6db1d485 |
Hashes for css_inline-0.7.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8a1955e9cb3aeb6e0a7626cef15674c60f7e993ffaa77e1803afbc9edad2b90 |
|
MD5 | 65a20e8a9ec7083265d4f30dad33d9e3 |
|
BLAKE2b-256 | ace5a309e5245a5d152f4a7ac42364e9de9d924b14c579ccbcef2c5ce43ec0d6 |
Hashes for css_inline-0.7.7-cp37-cp37m-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11093146bd89ca7148e3d6b49c12e12ec0528f311800200f9e0806ea37a1c9cd |
|
MD5 | 6100d996f711820c4e25c274888d8bfd |
|
BLAKE2b-256 | d57bc6820bc2ba5c57cdfe83278e920efeb761d585f2a7a140f77cab164e8c3c |
Hashes for css_inline-0.7.7-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5ae053e08ed87f3eeba077ebafb71482cf81a0e836bd67aaf006751b74ffa61 |
|
MD5 | 7ca17404b7b35dc53faaa595e4585c37 |
|
BLAKE2b-256 | 3c6c0a12f4759fd234e2297fe01b6e0f7e9b23e0fd4e27a738d29a952fd6a82e |
Hashes for css_inline-0.7.7-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1bd56bb980d29d502d7df86a49e0d69abbe01b886d3346d07343124e5159fdc |
|
MD5 | 5914d5355259ace1d6eb3a62d29f346b |
|
BLAKE2b-256 | a62db7b145ecd0e270d5299f68a05918cf4664cb9498d2766622ab70e1b0a2ce |
Hashes for css_inline-0.7.7-cp36-cp36m-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 680269e9567db4bcca2e02abaa5b2121628fe4c9b1872bd9796705f41247fc24 |
|
MD5 | 289b33b6270f0a9a77bba55b3d9e0127 |
|
BLAKE2b-256 | 178e4f31257b13ab1bc49ebaf966e34c3f9011421aede716b23f89bcc9446e79 |