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.4-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab505a5859c90c1f2e8883eb4fb5e2ac507d7741c571b49d0dcd8390961776f1 |
|
MD5 | 87456900915b2d16e06e5be6c0985413 |
|
BLAKE2b-256 | a1326a045474112014d46e3195169d0e22dd5c30e8439396a8702875d23b8195 |
Hashes for css_inline-0.7.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3a8611c6efc450dc3b2bff3c7c5b8a97afc89fb48288ee3ef850a9c9f3cda08 |
|
MD5 | 97ad42ce4d096c3e532721f312f0a4de |
|
BLAKE2b-256 | cc2a4c3afd70aa0546f8688eaffaefad768f6f51167e8ae1371fac8e1d1c0811 |
Hashes for css_inline-0.7.4-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd44493c538efaef4c430d79030b3b1d6f0b47249a972969eefc1728f7ecce16 |
|
MD5 | 3426eb8d2a396c7c1edfae65027b2e74 |
|
BLAKE2b-256 | b49a98b48520549d741068b324e80133e0ebb28fd4763d579712d43c4d00b78b |
Hashes for css_inline-0.7.4-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50ac4051bc985baa4520214d05ed42874529bd0b6ea9e0225c4e044bcf9f634f |
|
MD5 | adcdeb601d2ad5f42cb255dcdf3f4a0a |
|
BLAKE2b-256 | f452fae2fef60d329b98ad73e9528ae1a87f138ff0f3a4097828766652ccedde |
Hashes for css_inline-0.7.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eadfc538cf13b86ffd156fc6b0ce2012ac72ad9d56540c0f2c0e122cd4bf1c46 |
|
MD5 | 86730e8c664c7079c58a8c98ae37d1c8 |
|
BLAKE2b-256 | 680055cc8c446b3014756ed6137d35044623deec9f9acc2681aab15c05172f59 |
Hashes for css_inline-0.7.4-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb81c12783ce8838f13e5da4b896320a18dd5535bc0efcfd653cf4db39f17e45 |
|
MD5 | 630fc390f766ced826245a59d0700813 |
|
BLAKE2b-256 | 384a4de46bcf6308cc9f74d262dfc316ae0fb764c15d41216323f2bad663469a |
Hashes for css_inline-0.7.4-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0847993b4494bd7a31482d19168c98ff848507bc04b48e2179b76478b446fdae |
|
MD5 | b3af253993cd5cc0d678f21b6035c7fa |
|
BLAKE2b-256 | df663e9b244977856a3d3ae578485786a789ea5a916dbb870665ab0482a4f233 |
Hashes for css_inline-0.7.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fe7195e39aee7db51c9f9cad29a1083039651853a57fad48408b146e1e462b6 |
|
MD5 | 0c1f984aa8dfed3ca2701c1f1800827f |
|
BLAKE2b-256 | d8de5c8034ff735ee3156945d147e7b04b03ef71e39637869f0a95e31d369342 |
Hashes for css_inline-0.7.4-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f15866a3c960c4d06757560380b8358bf87bb3593cbe1e8256e9a92fe48cb65 |
|
MD5 | be3a0ed75fa22a4975af22d20609bd3a |
|
BLAKE2b-256 | 9fc439da250f8b46a1b91af67342e147a08a7b429d59d296e25db4645ca9cf77 |
Hashes for css_inline-0.7.4-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4715e22c254b61bc54dd0fdded3a6fe03c6690387f88cfaa9cb60bfd21975fe |
|
MD5 | 4a75b49e7d6061b7a0dadb0efbc23022 |
|
BLAKE2b-256 | af4455dcf158d300b5aa5ded10b62393649ca524752e93b5102cb44f55a6d5da |
Hashes for css_inline-0.7.4-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51fefda772c9607e45348cf9ddeb0c3d5d08f04ea58448fe4a38347983483dc4 |
|
MD5 | c48e04bc1a5a6afffa755aca84da4d56 |
|
BLAKE2b-256 | 025eeb6085b292c52d59cea0f43916322c0ed69f46c97cc07232f7b571b07821 |
Hashes for css_inline-0.7.4-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | afebbb6ef7e7f875a922894fcd70409fc5f33444ed36a2956a1271e7638f3556 |
|
MD5 | aff67d3632456a57e54017334b798f02 |
|
BLAKE2b-256 | ef2a45f8315d0ef3e7d1e2d53b403a1530cfde40046cfdb0bf01c5434dddc3c3 |