minify-html Python Library

0.18.1 · active · verified Sat Apr 11

minify-html is a Python binding for an extremely fast and smart HTML + JS + CSS minifier written in Rust. It offers advanced minification strategies that outperform other minifiers in both speed and effectiveness, handling invalid HTML and templating syntax. The library is actively maintained with frequent minor releases, often multiple times a month, and is currently at version 0.18.1.

Warnings

Install

Imports

Quickstart

Demonstrates how to minify an HTML string, including inline CSS and JavaScript, while configuring options like comment removal and doctype preservation.

from minify_html import minify

html_input = """
<!DOCTYPE html>
<html>
<head>
    <title> Test Page </title>
    <style>
        body { background-color: #f0f0f0; }
        h1 { color: blue; }
    </style>
</head>
<body>
    <!-- Main Content -->
    <h1> Hello, World! </h1>
    <p> This is a <span> test </span> paragraph. </p>
    <script>
        // Some JavaScript
        function greet() {
            console.log("Welcome!");
        }
        greet();
    </script>
</body>
</html>
"""

minified_html = minify(
    html_input,
    keep_comments=False,
    minify_css=True,
    minify_js=True,
    do_not_minify_doctype=True
)
print(minified_html)

view raw JSON →