High-performance library for inlining CSS into HTML 'style' attributes
Project description
css_inline
css_inline
is a high-performance library for inlining CSS into HTML 'style' attributes.
This library is designed for scenarios such as preparing HTML emails or embedding HTML into third-party web pages.
For instance, the library transforms HTML like this:
<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>
into:
<html>
<head></head>
<body>
<h1 style="color:blue;">Big Text</h1>
</body>
</html>
- Uses reliable components from Mozilla's Servo project
- 10-400x faster than alternatives
- Inlines CSS from
style
andlink
tags - Removes
style
andlink
tags - Resolves external stylesheets (including local files)
- Can process multiple documents in parallel
- Works on Linux, Windows, and macOS
- Supports HTML5 & CSS3
Installation
Install with pip
:
pip install css_inline
Pre-compiled wheels are available for most popular platforms. If not available for your platform, a Rust compiler will be needed to build this package from source. Rust version 1.60 or higher is required.
Usage
import css_inline
HTML = """<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>"""
inlined = css_inline.inline(HTML)
# HTML becomes this:
#
# <html>
# <head>
# <style>h1 { color:blue; }</style>
# </head>
# <body>
# <h1 style="color:blue;">Big Text</h1>
# </body>
# </html>
When there is a need to inline multiple HTML documents simultaneously, css_inline
offers the inline_many
function.
This feature allows for concurrent processing of several inputs, significantly improving performance when dealing with a large number of documents.
import css_inline
css_inline.inline_many(["<...>", "<...>"])
Under the hood, inline_many
, spawns threads at the Rust layer to handle the parallel processing of inputs.
This results in faster execution times compared to employing parallel processing techniques at the Python level.
Note: To fully benefit from inline_many
, you should run your application on a multicore machine.
Configuration
For configuration options use the CSSInliner
class:
import css_inline
inliner = css_inline.CSSInliner(keep_style_tags=True)
inliner.inline("...")
keep_style_tags
. Specifies whether to keep "style" tags after inlining. Default:False
keep_link_tags
. Specifies whether to keep "link" tags after inlining. Default:False
base_url
. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use thefile://
scheme. Default:None
load_remote_stylesheets
. Specifies whether remote stylesheets should be loaded. Default:True
extra_css
. Extra CSS to be inlined. Default:None
preallocate_node_capacity
. Advanced. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default:32
You can also skip CSS inlining for an HTML tag by adding the data-css-inline="ignore"
attribute to it:
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<!-- The tag below won't receive additional styles -->
<h1 data-css-inline="ignore">Big Text</h1>
</body>
The data-css-inline="ignore"
attribute also allows you to skip link
and style
tags:
<head>
<!-- Styles below are ignored -->
<style data-css-inline="ignore">h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
If you'd like to load stylesheets from your filesystem, use the file://
scheme:
import css_inline
# styles/email is relative to the current directory
inliner = css_inline.CSSInliner(base_url="file://styles/email/")
inliner.inline("...")
XHTML compatibility
If you'd like to work around some XHTML compatibility issues like closing empty tags (<hr>
vs. <hr/>
), you can use the following snippet that involves lxml
:
import css_inline
from lxml import html, etree
document = "..." # Your HTML document
inlined = css_inline.inline(document)
tree = html.fromstring(inlined)
inlined = etree.tostring(tree).decode(encoding="utf-8")
Performance
css-inline
is powered by efficient tooling from Mozilla's Servo project and significantly outperforms other Python alternatives in terms of speed.
It achieves over a 10x speed advantage compared to the next fastest alternative.
Here is the performance comparison:
css_inline 0.10.3 |
premailer 3.10.0 |
toronado 0.1.0 |
inlinestyler 0.2.5 |
pynliner 0.8.0 |
|
---|---|---|---|---|---|
Basic | 7.58 µs | 192.50 µs (25.39x) | 951.66 µs (125.50x) | 1.52 ms (201.12x) | 1.78 ms (235.59x) |
Realistic-1 | 172.58 µs | 2.08 ms (12.09x) | 25.01 ms (144.92x) | 42.50 ms (246.31x) | 71.75 ms (415.76x) |
Realistic-2 | 119.16 µs | 3.93 ms (33.00x) | ERROR | 26.49 ms (222.31x) | ERROR |
The above data was obtained from benchmarking the inlining of CSS in HTML, as described in the Usage section.
Note that the toronado
and pynliner
libraries both encountered errors when used to inline CSS in the last scenario.
The benchmarking code is available in the benches/bench.py
file. The tests were conducted using the stable rustc 1.70
on Python 3.11.0
.
Comparison with other libraries
Besides performance, css-inline
differs from other Python libraries for CSS inlining.
- Generally supports more CSS features than other libraries (for example,
toronado
andpynliner
do not support pseudo-elements); - It has fewer configuration options and not as flexible as
premailer
; - Works on fewer platforms than LXML-based libraries (
premailer
,inlinestyler
,toronado
, and optionallypynliner
); - Does not have debug logs yet;
- Supports only HTML 5.
Python support
css_inline
supports CPython 3.7, 3.8, 3.9, 3.10, 3.11 and PyPy 3.7, 3.8, 3.9.
Further reading
If you want to know how this library was created & how it works internally, you could take a look at these articles:
License
This project is licensed under the terms of the 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.10.3-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72591253afc8347716c4316f743f8d813f5127a3e8d0975888edd73ba3ffa409 |
|
MD5 | 05da5d7c061ace4a2dc9ed7c971d6abb |
|
BLAKE2b-256 | f002ec6fbfeb00d1886631685146081f01c9aea1769476bf3d959a84f28b809e |
Hashes for css_inline-0.10.3-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 060b0042cbfa5f7bb1569d01f2d4c257994a6913b27b390618320de68ecb8af5 |
|
MD5 | deb26ed7d33e2ecdeb1275414389d65c |
|
BLAKE2b-256 | e4b96c6aa304141c803dc73099879519218635f1d082493f1cf42168dcff74bc |
Hashes for css_inline-0.10.3-pp39-pypy39_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5b7b2ae474c75eb7783d052744651d1bd72ebe54b860e3eb62febcdaba35050 |
|
MD5 | 5e1e98a78048d917d61579edee1b4077 |
|
BLAKE2b-256 | 96df9374b7aa21152cd3b9e9262c1c62f459675537ccf139a301f41ac346f555 |
Hashes for css_inline-0.10.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f511f9a283e81a1c97b0f5363b3c55944497e478cd21d6bda573d7646468789 |
|
MD5 | 796be0627032ee5a27e1da9f686621da |
|
BLAKE2b-256 | 2a9104d2ee697ae17ec8d3685ad40e540700e764cdabcacd254841bb7fadb70b |
Hashes for css_inline-0.10.3-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6925375c3a03485566082efe558e51c3235bd7870a689847c1778e92b5e79ff |
|
MD5 | 799f8170c5193b0041110bceca8c78db |
|
BLAKE2b-256 | 138990fc5d3354aff0769c81548a426fafc113ca6d2b8649aa48abcff02fadbb |
Hashes for css_inline-0.10.3-pp38-pypy38_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 545b6e8a553a73ea03a4f481c34ba3d2e83a13aa66494115b7926a525059d887 |
|
MD5 | 28f93440ab3fa4967a8e9e7a22800a1e |
|
BLAKE2b-256 | e5c775bbe57390965e3d6704a8cfc22645e8380b9f982c66df4522c297a9b0e0 |
Hashes for css_inline-0.10.3-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3c018fd61b6d9d206ce8cb0c908612e7962b6781f638fd4bb2e81547e163022 |
|
MD5 | cd4eb6b76985456a2732c609a449b58d |
|
BLAKE2b-256 | d2a3c37440dfb9ccdde2e7b8e6e2afa8e238b3ff103ab201c99bcd2ef6631b7c |
Hashes for css_inline-0.10.3-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41789e225ef5d25debd6ab7df99862ba792855d20896dc772b41876021cdae7a |
|
MD5 | d5555837a9dd31585d8889367a6e3fa4 |
|
BLAKE2b-256 | 906a0fca4a196a7ea3611092629b2491f2431f38d4f98d65860b7b588859760a |
Hashes for css_inline-0.10.3-pp37-pypy37_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e816f38b66f64ad0cd197d1a8c06e738192186653920544285a2698e1c923d29 |
|
MD5 | a63324e8fd306d8f9d6cc78e293a6cce |
|
BLAKE2b-256 | e60000922bae12c1fe9336bc299a825df8942f006c5d98cb134ea8748d6ce38e |
Hashes for css_inline-0.10.3-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c84173269d77ad6b67d0808dfe6dd99fef0b31fff8b5981a6410cbb4b564719e |
|
MD5 | e7ae4df93922e86d235ad3884ee36df4 |
|
BLAKE2b-256 | e8a27bf5d5f1063932c254cf93725435f14bdc69e73c9c2b52af7a128b3ae989 |
Hashes for css_inline-0.10.3-cp37-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f59bb69f4194101528a9d9fdf61d6153b8e35f6448615091c5c74b0996cbbd56 |
|
MD5 | 2d7f1f6ad1bfbb6d5a06aea9ba7a0257 |
|
BLAKE2b-256 | 6df4ced9785908bc1113e48c75b022a59c393b95b63a7916dcdbb52abb6fced1 |
Hashes for css_inline-0.10.3-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abe3733ad31cc151e4e685e0222ab83e4917d5045e1776c5bdb45b63102de9b4 |
|
MD5 | 06d594d82bb10eebcb1144448219d9ff |
|
BLAKE2b-256 | c1e820271b1435ccae3d31f2dd77fa05093c764c242b243f2830c943bc062a3a |
Hashes for css_inline-0.10.3-cp37-abi3-musllinux_1_2_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80c589e27993a870ca49857f3da31329edbc8c426ef0e98c1752eb9c1ab33511 |
|
MD5 | 816a0cd865862ce973e8854fc8e7c216 |
|
BLAKE2b-256 | 581b09618707cb1821c24a190e73a95af19e6fc2b488b0aa755a58ef6981d627 |
Hashes for css_inline-0.10.3-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97e0db497895bb47e341857e13cc8ad010f18382581f6195a1827faff0698b26 |
|
MD5 | 1e790c1f8d1b4c7c2fb28bf524245938 |
|
BLAKE2b-256 | fadecccac59b7e20e704e85fce0a20c873b027c9a1eb883c99f3a391e4ec711e |
Hashes for css_inline-0.10.3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9408ce554575174f50f5a4b9e65822ffa4585be6165711a9881db5794d436dc |
|
MD5 | f4509ca235bb86a1d6976c703f950004 |
|
BLAKE2b-256 | 1b04f6889885c61a636c3590031a3cfa019b2c844985558ae97a121453a9497d |
Hashes for css_inline-0.10.3-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b67f4204765eec4545da95995fceebd7ff792aefa6ba3bff6e631a3e38c5041 |
|
MD5 | 8af198841bc0fe49d75417d70854900f |
|
BLAKE2b-256 | ea6f9d1d25ecfcb343c11455f122db2e83acde4c04e3cd332cb51f7a5663bd78 |
Hashes for css_inline-0.10.3-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d0e08d7a0cdd82b541d78b143469a4e7def4a065c14138ed12ecfb077a58af7 |
|
MD5 | a041eda5288ab43ff9d9f4dc120d9b56 |
|
BLAKE2b-256 | fb356e246af41d00fc72433337bf4d57057a4e931c87fb036b8399cbe4ee8e68 |
Hashes for css_inline-0.10.3-cp37-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e934e548ecb6672820eb4dfab635fd42c05bac799dfee48d853b51a0abd4230f |
|
MD5 | 3294d2987fefbbe0dbb90d30592506cd |
|
BLAKE2b-256 | 0befa947adb9015b5b43c050a1f437cd103fbb6e81f61d30e7d029feda1952ad |
Hashes for css_inline-0.10.3-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06b1df40f2f5f778ef1a9a09ed4655adf31ec82dfb0f4b1f5319e1b87551058d |
|
MD5 | c524b991835ccc6bd189734f5b4e5924 |
|
BLAKE2b-256 | f51c61eef32f1838703d2bccb5e8ba1bb38495a12a61addf03419644f0cf179d |
Hashes for css_inline-0.10.3-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d0595832d5ef442469412ca2f9a541291c943ea22b03e1906d7312aa4b11461 |
|
MD5 | 97af0d556fb7b7e8fd82095bbffe1eb7 |
|
BLAKE2b-256 | 185cff153f801482537b7491ee480492fc831b04bbdab2179a4dbda1f5f86af7 |