Skip to main content

Fast CSS inlining written in Rust

Project description

css_inline

Build PyPI Python versions License

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;
  • Out-of-document 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

Pre-compiled wheels for most popular platforms are provided. If your platform is not in the support table below, you will need a Rust compiler to build this package from source. The minimum supported Rust version is 1.54.

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-21x faster than the nearest alternative.

For inlining CSS in the html document from the Usage section above there is the following breakdown in the benchmarks:

  • css_inline 0.7.8 - 21.76 us
  • premailer 3.10.0 - 461.54 us (x21.21)
  • toronado 0.1.0 - 1.87 ms (x85.93)
  • inlinestyler 0.2.4 - 2.85 ms (x130.97)
  • pynliner 0.8.0 - 3.34 ms (x153.49)

And for a more realistic email:

  • css_inline 0.7.8 - 433.39 us
  • premailer 3.10.0 - 3.9 ms (x9.01)
  • toronado 0.1.0 - 43.89 ms (x101.27)
  • inlinestyler 0.2.4 - 75.77 ms (x174.83)
  • pynliner 0.8.0 - 123.6 ms (x285.19)

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, Linux x86_64 on i8700K, and 32GB RAM.

Python support

css_inline supports CPython 3.6, 3.7, 3.8, 3.9, 3.10, and PyPy 3.7 and 3.8.

The following wheels are available:

manylinux
musllinux
x86_64
manylinux
musllinux
aarch64
macOS Intel macOS ARM64 Windows 64bit Windows 32bit
CPython 3.6 ✔️ ✔ ️ ✔️ ✔️
CPython 3.7 ✔️ ✔️ ✔️
CPython 3.8 ✔️ ✔️ ✔️ ✔️
CPython 3.9 ✔️ ✔️ ✔️ ✔️
CPython 3.10 ✔️ ✔️ ✔️ ✔️
PyPy 3.7 v7.3 ✔¹ ✔¹ ✔️ N/A ✔️ N/A
PyPy 3.8 v7.3 ✔¹ ✔¹ ✔️ N/A ✔️ N/A

¹ PyPy is only supported for manylinux wheels.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

css_inline-0.8.1.tar.gz (24.0 kB view hashes)

Uploaded Source

Built Distributions

css_inline-0.8.1-pp38-pypy38_pp73-win_amd64.whl (2.6 MB view hashes)

Uploaded PyPy Windows x86-64

css_inline-0.8.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

css_inline-0.8.1-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

css_inline-0.8.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (3.0 MB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

css_inline-0.8.1-pp37-pypy37_pp73-win_amd64.whl (2.6 MB view hashes)

Uploaded PyPy Windows x86-64

css_inline-0.8.1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

css_inline-0.8.1-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

css_inline-0.8.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (1.5 MB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

css_inline-0.8.1-cp310-cp310-win_amd64.whl (1.3 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

css_inline-0.8.1-cp310-cp310-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.10 Windows x86

css_inline-0.8.1-cp310-cp310-musllinux_1_1_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

css_inline-0.8.1-cp310-cp310-musllinux_1_1_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

css_inline-0.8.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

css_inline-0.8.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

css_inline-0.8.1-cp310-cp310-macosx_11_0_arm64.whl (1.5 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

css_inline-0.8.1-cp310-cp310-macosx_10_9_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

css_inline-0.8.1-cp39-cp39-win_amd64.whl (1.3 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

css_inline-0.8.1-cp39-cp39-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.9 Windows x86

css_inline-0.8.1-cp39-cp39-musllinux_1_1_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

css_inline-0.8.1-cp39-cp39-musllinux_1_1_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

css_inline-0.8.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

css_inline-0.8.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

css_inline-0.8.1-cp39-cp39-macosx_11_0_arm64.whl (1.5 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

css_inline-0.8.1-cp39-cp39-macosx_10_9_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

css_inline-0.8.1-cp38-cp38-win_amd64.whl (1.3 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

css_inline-0.8.1-cp38-cp38-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.8 Windows x86

css_inline-0.8.1-cp38-cp38-musllinux_1_1_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

css_inline-0.8.1-cp38-cp38-musllinux_1_1_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

css_inline-0.8.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

css_inline-0.8.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

css_inline-0.8.1-cp38-cp38-macosx_11_0_arm64.whl (1.5 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

css_inline-0.8.1-cp38-cp38-macosx_10_9_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

css_inline-0.8.1-cp37-cp37m-win_amd64.whl (1.3 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

css_inline-0.8.1-cp37-cp37m-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.7m Windows x86

css_inline-0.8.1-cp37-cp37m-musllinux_1_1_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

css_inline-0.8.1-cp37-cp37m-musllinux_1_1_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ ARM64

css_inline-0.8.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

css_inline-0.8.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

css_inline-0.8.1-cp37-cp37m-macosx_10_9_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

css_inline-0.8.1-cp36-cp36m-win_amd64.whl (1.3 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

css_inline-0.8.1-cp36-cp36m-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.6m Windows x86

css_inline-0.8.1-cp36-cp36m-musllinux_1_1_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

css_inline-0.8.1-cp36-cp36m-musllinux_1_1_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ ARM64

css_inline-0.8.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

css_inline-0.8.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ARM64

css_inline-0.8.1-cp36-cp36m-macosx_10_9_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

css_inline-0.8.1-cp36-abi3-macosx_10_10_universal2.whl (2.9 MB view hashes)

Uploaded CPython 3.6+ macOS 10.10+ universal2 (ARM64, x86-64)

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page