Skip to main content

Fast CSS inlining written in Rust

Project description

Build Version Python versions License

Fast CSS inlining for Python implemented in Rust.

Features:

  • Removing style tags after inlining;

  • Resolving external stylesheets (including local files);

  • … more features will be available soon

NOTE. This library is in active development and provides a small number of features at the moment, see Limitations sections below for more information.

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)

If you want to inline many HTML documents then you can utilize inline_many that processes the input in parallel.

import css_inline

css_inline.inline_many(["...", "..."])

For customization options use 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 good performance characteristics. In comparison with other Python projects, it is ~12-15x faster than the nearest competitor.

For inlining CSS in the html document from the Usage section above we have the following breakdown in our benchmarks:

  • css_inline 0.3.0 - 24.38 us

  • premailer 3.7.0 - 339.03 us (x13.9)

  • inlinestyler 0.2.4 - 2.44 ms (x100.46)

  • pynliner 0.8.0 - 2.79 ms (x114.66)

You can take a look at the benchmarks’ code at benches/bench.py file. The results above were measured with rustc 1.46, Python 3.8 on i8700K, and 32GB RAM.

Limitations

Currently (as of 0.3.0) there are the following notable limitations:

  • Inlined CSS is not minimized (#12)

  • class and id attributes are not removed (#13)

Python support

css_inline supports Python 3.5, 3.6, 3.7, and 3.8.

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

Uploaded Source

Built Distributions

css_inline-0.3.0-cp38-cp38-win_amd64.whl (852.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

css_inline-0.3.0-cp38-cp38-manylinux2014_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.8

css_inline-0.3.0-cp38-cp38-macosx_10_15_x86_64.whl (904.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

css_inline-0.3.0-cp37-cp37m-win_amd64.whl (852.9 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

css_inline-0.3.0-cp37-cp37m-manylinux2014_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.7m

css_inline-0.3.0-cp37-cp37m-macosx_10_15_x86_64.whl (904.3 kB view hashes)

Uploaded CPython 3.7m macOS 10.15+ x86-64

css_inline-0.3.0-cp36-cp36m-win_amd64.whl (853.2 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

css_inline-0.3.0-cp36-cp36m-manylinux2014_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.6m

css_inline-0.3.0-cp36-cp36m-macosx_10_15_x86_64.whl (904.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.15+ x86-64

css_inline-0.3.0-cp35-cp35m-win_amd64.whl (853.1 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

css_inline-0.3.0-cp35-cp35m-manylinux2014_x86_64.whl (3.5 MB view hashes)

Uploaded CPython 3.5m

css_inline-0.3.0-cp35-cp35m-macosx_10_15_x86_64.whl (904.4 kB view hashes)

Uploaded CPython 3.5m macOS 10.15+ 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