Skip to main content

Fast CSS inlining written in Rust

Project description

Build Version 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;

  • 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.

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.7.8.tar.gz (21.4 kB view hashes)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.10 Windows x86-64

css_inline-0.7.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

css_inline-0.7.8-cp310-cp310-macosx_11_0_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

css_inline-0.7.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

css_inline-0.7.8-cp39-cp39-macosx_11_0_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

css_inline-0.7.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

css_inline-0.7.8-cp38-cp38-macosx_11_0_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ x86-64

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

Uploaded CPython 3.7m Windows x86-64

css_inline-0.7.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.4 MB view hashes)

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

css_inline-0.7.8-cp37-cp37m-macosx_11_0_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.7m macOS 11.0+ x86-64

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

Uploaded CPython 3.6m Windows x86-64

css_inline-0.7.8-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.4 MB view hashes)

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

css_inline-0.7.8-cp36-cp36m-macosx_11_0_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.6m macOS 11.0+ 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