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 ~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


Download files

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

Files for css-inline, version 0.7.5
Filename, size File type Python version Upload date Hashes
Filename, size css_inline-0.7.5-cp36-cp36m-macosx_10_15_x86_64.whl (1.5 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size css_inline-0.7.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size css_inline-0.7.5-cp36-cp36m-win_amd64.whl (1.3 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size css_inline-0.7.5-cp37-cp37m-macosx_10_15_x86_64.whl (1.5 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size css_inline-0.7.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size css_inline-0.7.5-cp37-cp37m-win_amd64.whl (1.3 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size css_inline-0.7.5-cp38-cp38-macosx_10_15_x86_64.whl (1.5 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size css_inline-0.7.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size css_inline-0.7.5-cp38-cp38-win_amd64.whl (1.3 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size css_inline-0.7.5-cp39-cp39-macosx_10_15_x86_64.whl (1.5 MB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size css_inline-0.7.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size css_inline-0.7.5-cp39-cp39-win_amd64.whl (1.3 MB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size css_inline-0.7.5.tar.gz (20.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page