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.
Release files for css-inline 0.7.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| css_inline-0.7.7.tar.gz | 21.1 kB | Details |
Built distributions (wheels)
Total release size: 24.7 MB
Release files / css_inline-0.7.7.tar.gz
| Download URL | css_inline-0.7.7.tar.gz |
|---|---|
| Size | 21.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f24569f9235fed42e419f01c43d62076b15176badec12a9dde7a9eddf5ee989
|
|
BLAKE2b-256 checksum How to use checksums |
f8a63db6d9db7b9862cf3780570a876f89871a0b5196e2dbae217c7d593076fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp310-cp310-win_amd64.whl
| Download URL | css_inline-0.7.7-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
32d8ef72445c06e6ce1a699ba1f459d7450104a77fee2a4c860b12e42b508c1e
|
|
BLAKE2b-256 checksum How to use checksums |
293d102c1315e48ec6de2eaed70ecee32dd960b0fbc44d2a138f8f92926041a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | css_inline-0.7.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
c84ac25b4531c74569a50508079b3b8c7299c820a233b360ef0d1c1b37cfbcd1
|
|
BLAKE2b-256 checksum How to use checksums |
918217ee69f7e1203b1dfa11d0cd38d78103208adfaa74558a6d5ff2919cd3c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp310-cp310-macosx_11_0_x86_64.whl
| Download URL | css_inline-0.7.7-cp310-cp310-macosx_11_0_x86_64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.10 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
9b1f2f54ddca7df4e9d3efc449e9118f76bd5162cefcaa0ac551ebd8e0ba7845
|
|
BLAKE2b-256 checksum How to use checksums |
88f094b88566147618476004bc40a799c7a89f226dc709bdb7c608bd2d17653e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp39-cp39-win_amd64.whl
| Download URL | css_inline-0.7.7-cp39-cp39-win_amd64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.9 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
65bb9ff96691ac073ac176c1c41cf1443dda93dce881fb69a94215c9ea1b0223
|
|
BLAKE2b-256 checksum How to use checksums |
513b38d92b7dea0a0717172c111fdc4091080c1ebc3c952fa12eee4108046b72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | css_inline-0.7.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
f7607fbdb5042e3ee166d97497ed80d146a5a8026f56afbd622845dc3d2937a4
|
|
BLAKE2b-256 checksum How to use checksums |
b9641cd7a661a768053b3c431e9155ac98699962e716b61e71f92182380fa2df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp39-cp39-macosx_11_0_x86_64.whl
| Download URL | css_inline-0.7.7-cp39-cp39-macosx_11_0_x86_64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.9 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
5282c21c5f81bd1af0fbd09fbdeaa9c2796cab48cd810997ced12036cf159a6e
|
|
BLAKE2b-256 checksum How to use checksums |
e9738c6afb6ab890872b875cd76e41e2efbe83f60da9de3e291a8850b1a97c7e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp38-cp38-win_amd64.whl
| Download URL | css_inline-0.7.7-cp38-cp38-win_amd64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.8 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
f09853736618e73505451308b33b9323b3479ccecc5db3aec3431ae5793374b7
|
|
BLAKE2b-256 checksum How to use checksums |
26167af5f9102ac566292bb2f21a21edeb23e728ad57ee9901e7fb8ef0e3a486
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | css_inline-0.7.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
e1d17f74775e0271e52abc982f809d4c83bb3e40580179298b005b6999a0714d
|
|
BLAKE2b-256 checksum How to use checksums |
6698abaaa5e9ceeaf52c5a63c58c52c535ad0c311d9d87af30aff4b208891139
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp38-cp38-macosx_11_0_x86_64.whl
| Download URL | css_inline-0.7.7-cp38-cp38-macosx_11_0_x86_64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.8 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
ab4de3110e3f54ce52389a3549c06604458df0d31953bc38e3cc17bc4c8e4b68
|
|
BLAKE2b-256 checksum How to use checksums |
fe5d3bf295e71fa81d8e43a747444b5716e3dda05fd22b6094d373267ec58d3c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp37-cp37m-win_amd64.whl
| Download URL | css_inline-0.7.7-cp37-cp37m-win_amd64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.7 CPython 3.7 pymalloc Windows x86-64 |
|
SHA-256 checksum How to use checksums |
ad0f76a62e30e54201dc78340ccf7ac07ebd2537e4cbfeb1c79f56398d38957e
|
|
BLAKE2b-256 checksum How to use checksums |
2bc8b7065777b8053a0ca78aa8c34c5616274f1788ae64b7d045df8f6db1d485
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | css_inline-0.7.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
f8a1955e9cb3aeb6e0a7626cef15674c60f7e993ffaa77e1803afbc9edad2b90
|
|
BLAKE2b-256 checksum How to use checksums |
ace5a309e5245a5d152f4a7ac42364e9de9d924b14c579ccbcef2c5ce43ec0d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp37-cp37m-macosx_11_0_x86_64.whl
| Download URL | css_inline-0.7.7-cp37-cp37m-macosx_11_0_x86_64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.7 CPython 3.7 pymalloc macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
11093146bd89ca7148e3d6b49c12e12ec0528f311800200f9e0806ea37a1c9cd
|
|
BLAKE2b-256 checksum How to use checksums |
d57bc6820bc2ba5c57cdfe83278e920efeb761d585f2a7a140f77cab164e8c3c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp36-cp36m-win_amd64.whl
| Download URL | css_inline-0.7.7-cp36-cp36m-win_amd64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.6 CPython 3.6 pymalloc Windows x86-64 |
|
SHA-256 checksum How to use checksums |
b5ae053e08ed87f3eeba077ebafb71482cf81a0e836bd67aaf006751b74ffa61
|
|
BLAKE2b-256 checksum How to use checksums |
3c6c0a12f4759fd234e2297fe01b6e0f7e9b23e0fd4e27a738d29a952fd6a82e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | css_inline-0.7.7-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
b1bd56bb980d29d502d7df86a49e0d69abbe01b886d3346d07343124e5159fdc
|
|
BLAKE2b-256 checksum How to use checksums |
a62db7b145ecd0e270d5299f68a05918cf4664cb9498d2766622ab70e1b0a2ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / css_inline-0.7.7-cp36-cp36m-macosx_11_0_x86_64.whl
| Download URL | css_inline-0.7.7-cp36-cp36m-macosx_11_0_x86_64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.6 CPython 3.6 pymalloc macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
680269e9567db4bcca2e02abaa5b2121628fe4c9b1872bd9796705f41247fc24
|
|
BLAKE2b-256 checksum How to use checksums |
178e4f31257b13ab1bc49ebaf966e34c3f9011421aede716b23f89bcc9446e79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|