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.5-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 243129ff8e096ae6c54c60634ee01372bdcd30fcba459b2030caa0ca979ba9d9 |
|
MD5 | e4b1daa8bdf7185fc7d8f332d8d7c835 |
|
BLAKE2b-256 | 3bd255da47790af5d3ff6516622dc469085d75d405a2fc590bca219f1fc2b583 |
Hashes for css_inline-0.7.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39be9a95932712a6d798a93e17eda71d5aa84e71fdc9e7de8ae0de86d96eec56 |
|
MD5 | 74cd14cd0346f853ffbf4251da6afa99 |
|
BLAKE2b-256 | fb6266750e133f088b5df976f23636509e65cea22ff7d00003ce8821ca8d8c89 |
Hashes for css_inline-0.7.5-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abd8506784b2c58e78b57507bd73bc61ac01298108575a6dbcc50c03c997f7d0 |
|
MD5 | a174a7ff5dd03e03c8a54318de9d3c81 |
|
BLAKE2b-256 | eb6e4c6fafb7da44c7b771ed831df11f69073f76fb6f1ef659f708255ed8942c |
Hashes for css_inline-0.7.5-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b2f57483065dec4d7fc95844c3f0256f39eec290e7d28bfff5a38b0c648706f |
|
MD5 | 308bbf2b6f0862ade40e0e6a61203cd2 |
|
BLAKE2b-256 | 980990fb8401a06386592cb17a3908244b8f5c72a831e2f4f7ddee30467fa46b |
Hashes for css_inline-0.7.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9426df599410eded4e2caca41f2700c18801083fff6da75e60df832f02027aa9 |
|
MD5 | 9ab4b4ab660d317a3714cc3e7cbc39b7 |
|
BLAKE2b-256 | 645bdae4784c652f462da2cb6141abe4027a8c05412667c7f4686f62a7ca998a |
Hashes for css_inline-0.7.5-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cc1bdb746798f5aef21933fd534daed133a07e7902156bfbe4e62c882e79303 |
|
MD5 | b52b2d91d27d0a0f5c68c166bbda84ec |
|
BLAKE2b-256 | b3e0ce0b5f68c16d03c31b97c3f2db7b29918da7d28d4a83727804ca8454a4ca |
Hashes for css_inline-0.7.5-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65cffab8bcc04c44f53f4b878becc0714c43f75d8ed0a70b6ffdb89031b7df33 |
|
MD5 | 1f01c702a795a472f5194a611702e6e7 |
|
BLAKE2b-256 | e160a68b7458ed3ab88821bfa8df97f779d65e4f52c33b06ce930c19eb79b0a7 |
Hashes for css_inline-0.7.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb91efb45367070194d545b676141dbe3bae90972956b3c95088ebbe67f96a0a |
|
MD5 | 3c192eefeb44db2486307aa6a4c3fa84 |
|
BLAKE2b-256 | 2bb9757542c2497fa8273fb106cba2ab7ce95e175167cd87e60fb4e601535a48 |
Hashes for css_inline-0.7.5-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1f162904db3578c36dce846134005df4c6f092253b897c935832f3e7cdd01ab |
|
MD5 | 885982c1cbdc57aa8dc5c486c560d725 |
|
BLAKE2b-256 | cd23b25f27cd08832ac71c115b241bd06da22bfe7a0538d873b78604140fd8aa |
Hashes for css_inline-0.7.5-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3722a511d8363b167d00d11d80e59b31aac2b0f414845390b2d772b10bb11965 |
|
MD5 | 3b2040f6732822a75ee4c7e25011c008 |
|
BLAKE2b-256 | 9c45c34fcdca3aa7c68e9c19277b8dc43d035c847a4fa875d42289f93cdddfae |
Hashes for css_inline-0.7.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d7614eb5273fbdacf74503c1eebfd889de172801056e793507ecc4a3298ab0b |
|
MD5 | edf821d4020d6c7c936240e7df10cf8e |
|
BLAKE2b-256 | cdc3050845ac4809fb2e59a58caab686a59d78fa40fa54823ecf6509463ccc16 |
Hashes for css_inline-0.7.5-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f3dbcebc899a379b6bd7142fbc0384d1f28a1c08dbd06d6a029d964dc5bab54 |
|
MD5 | 77d1bd20121eb45e34e5424d96cff5a4 |
|
BLAKE2b-256 | 055fd88186e42cc376408a3769b673f01749e9c7bb3c032ffff246ec23ce7f1c |