HTML to htpy Converter
Convert HTML markup to Python htpy code instantly. Supports attributes, CSS selectors, nesting, and configurable import styles. Free online tool.
Convert HTML markup to Python htpy code instantly. Supports attributes, CSS selectors, nesting, and configurable import styles. Free online tool.
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.
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:
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.
| HTML Element | htpy Function | Void/Self-Closing | Common Attributes |
|---|---|---|---|
<div> | div | No | id, class, style, data-* |
<span> | span | No | id, class, style |
<p> | p | No | id, class |
<a> | a | No | href, target, rel |
<img> | img | Yes | src, alt, width, height |
<input> | input | Yes | type, name, value, placeholder |
<br> | br | Yes | - |
<hr> | hr | Yes | class |
<meta> | meta | Yes | name, content, charset |
<link> | link | Yes | rel, href, type |
<ul> | ul | No | id, class |
<ol> | ol | No | start, type |
<li> | li | No | value |
<table> | table | No | id, class, border |
<form> | form | No | action, method, enctype |
<button> | button | No | type, name, disabled |
<textarea> | textarea | No | name, rows, cols, placeholder |
<select> | select | No | name, multiple, size |
<option> | option | No | value, selected |
<header> | header | No | id, class |
<footer> | footer | No | id, class |
<nav> | nav | No | id, class, aria-label |
<section> | section | No | id, class |
<article> | article | No | id, class |
<main> | main | No | id, class |
<h1> - <h6> | h1 - h6 | No | id, class |
<source> | source | Yes | src, type, media |
<track> | track | Yes | src, kind, srclang, label |
<wbr> | wbr | Yes | - |
<col> | col | Yes | span |
<embed> | embed | Yes | src, type, width, height |