User Rating 0.0 โ˜…โ˜…โ˜…โ˜…โ˜…
Total Usage 0 times
Controls the import line at the top of output
HTML Input
0 characters
htpy Output
ย 
Is this tool helpful?

Your feedback helps us improve.

โ˜… โ˜… โ˜… โ˜… โ˜…

About

The htpy library renders HTML using pure Python function calls, replacing traditional template languages (Jinja2, Django Templates) with type-checked, composable code. Converting existing HTML to htpy by hand is tedious and error-prone. A misplaced parenthesis or forgotten comma in deeply nested structures silently produces malformed output. This tool parses your HTML through the browser's native DOMParser API, walks the resulting node tree, and emits syntactically correct htpy Python code. It handles attribute-to-keyword mapping, CSS selector shorthand (["#id.class"]), void elements, text node escaping, and configurable import styles. Note: the converter assumes well-formed HTML. Malformed input (unclosed tags, invalid nesting) is corrected by the browser's parser before conversion, which may alter your intended structure.

html to htpy htpy converter python html html2htpy htpy generator python templating html parser

Formulas

The conversion follows a deterministic tree-walking algorithm. Given an HTML element E with tag name t, attributes set A = {a1, a2, โ€ฆ, an}, and children C = [c1, c2, โ€ฆ, cm], the output function call is:

htpy(E) = t(selector, kwargs)[htpy(c1), โ€ฆ, htpy(cm)]

Where selector is the optional CSS shorthand string built from id and class attributes: selector = "#" + id + "." + join(".", classes). The kwargs dict contains remaining attributes with Python-safe key names: for โ†’ for_, class โ†’ class_ (when shorthand disabled), hyphenated attributes like data-x use dict unpacking syntax. Text nodes map to Python string literals. Void elements (where m = 0 by spec) omit the bracket notation entirely.

Reference Data

HTML Elementhtpy FunctionVoid/Self-ClosingCommon Attributes
<div>divNoid, class, style, data-*
<span>spanNoid, class, style
<p>pNoid, class
<a>aNohref, target, rel
<img>imgYessrc, alt, width, height
<input>inputYestype, name, value, placeholder
<br>brYes -
<hr>hrYesclass
<meta>metaYesname, content, charset
<link>linkYesrel, href, type
<ul>ulNoid, class
<ol>olNostart, type
<li>liNovalue
<table>tableNoid, class, border
<form>formNoaction, method, enctype
<button>buttonNotype, name, disabled
<textarea>textareaNoname, rows, cols, placeholder
<select>selectNoname, multiple, size
<option>optionNovalue, selected
<header>headerNoid, class
<footer>footerNoid, class
<nav>navNoid, class, aria-label
<section>sectionNoid, class
<article>articleNoid, class
<main>mainNoid, class
<h1> - <h6>h1 - h6Noid, class
<source>sourceYessrc, type, media
<track>trackYessrc, kind, srclang, label
<wbr>wbrYes -
<col>colYesspan
<embed>embedYessrc, type, width, height

Frequently Asked Questions

The tool uses the browser's native DOMParser API, which applies the HTML5 error recovery algorithm before conversion. This means unclosed tags get auto-closed, misnested elements get rearranged, and invalid nesting (e.g., a
inside a ) is corrected. The htpy output reflects the corrected DOM, not the raw input string. If structural fidelity matters, validate your HTML first.
Python identifiers cannot contain hyphens. The converter outputs hyphenated attributes using dictionary unpacking syntax: for example, data-value="5" becomes {"data-value": "5"} passed to the element call. The htpy library supports this pattern natively.
When enabled (default), the id and class attributes are extracted and combined into a CSS-like selector string as the first argument: div("#main.container.wide"). When disabled, they appear as keyword arguments instead: div(id="main", class_="container wide"). Shorthand produces more concise code, but explicit kwargs may be clearer for dynamic attribute patterns.
Yes. The text content inside script and style elements is preserved as a raw Python string literal. However, be aware that DOMParser may interpret certain sequences (like appearing in a string) during initial parsing. For complex inline scripts, convert them separately.
Boolean attributes without values are converted to Python keyword arguments set to True: e.g., becomes input(disabled=True). This follows htpy's convention for boolean attribute rendering.
The generated code produces standard htpy element trees that are fully compatible with htpy's render_node(), string conversion, and streaming/chunked rendering. No modifications are needed. The output is valid Python that can be pasted directly into an htpy-based application.