Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/nishad12323/py2html/llms.txt

Use this file to discover all available pages before exploring further.

py2html lets you construct full HTML documents entirely in Python. Instead of writing raw markup or learning a template language, you create a Parent object, call methods on it to add elements, and render the result by calling getHTML(). The mental model maps closely to Tkinter: widgets are placed on a root window, and the window renders itself — except here the output is a valid HTML file instead of a GUI.

Core concept

Every py2html page starts with a single Parent instance. All HTML elements — headings, paragraphs, buttons, images, links, and containers — are added by calling methods directly on that object. When you are ready to render, getHTML() walks the internal tag list and produces a complete <html>…</html> document.
import py2html

page = py2html.Parent()
page.Heading(level=1, text="Hello, py2html!")
page.Label(text="Build HTML without writing a single angle bracket.")
page.Button(text="Get Started")

print(page.getHTML(format=True))
No Jinja2 filters, no f-string soup, no BeautifulSoup soup-ing of your own output. The entire document lives in Python.

Key capabilities

Rich element library

Add Button, Label, Text, Heading, Image, Link, Frame, Script, TextNode, and CustomTag elements using straightforward method calls. Every element exposes foreground colour, background colour, border, border radius, and padding parameters.

Inline text markup

Apply font-size spans, bold, italic, inline code, code blocks, and centred text directly inside any text string using a lightweight bracket syntax — for example [* bold *] or [xl large xl].

Flexbox layouts via Frame

The Frame element renders as a <div> and accepts a content_manager parameter ("block", "flex", etc.) plus a flex_config dictionary that maps directly to CSS align-items, justify-content, flex-direction, flex-wrap, and gap.

Auto-escaping

All text content passed to element methods is automatically escaped — &, <, >, ", and ' are converted to their HTML entity equivalents before being written into the document.

Pretty-print output

Passing format=True to getHTML() runs the output through BeautifulSoup.prettify(), producing neatly indented HTML that is easy to read and diff.

Nested containers

A Frame can receive another Parent instance as its content argument, enabling arbitrarily deep nesting. The inner Parent is rendered recursively when getHTML() is called on the root.

JavaScript integration

Script(code="…") embeds inline JavaScript; Script(src="…") adds an external script reference. Script content is intentionally not HTML-escaped so that JS operators and string literals pass through unchanged.

Optional GUI editor

An optional Tkinter-based visual editor (py2htmlGUI.py) lets you build pages interactively with a live HTML preview. It is installed separately via the py2html_installer.py script.

Inline text markup reference

Inside any text value you can use the following bracket pairs:
SyntaxResult
[* bold *]Bold (<strong>)
[_ italic _]Italic (<em>)
[` code `]Inline code (<code>)
[``` block ```]Code block (<pre><code>)
[c centred c]Centred text (<center>)
[xl large xl]1.5 rem span
[s small s]0.8 rem span
Size variants available: xxs (0.4 rem), xs (0.6 rem), s (0.8 rem), m (1 rem), l (1.2 rem), xl (1.5 rem), xxl (2 rem), xxxl (3 rem).

Parent object API overview

All functionality lives on the Parent class. The table below lists every public method and special method.
Method / attributeDescription
Parent()Create a new document root (or nested container). Initialises an empty tags list and html string.
config(**kwargs)Configure the Parent with keyword arguments. Accepts title (sets self.title) and style (sets self.style, must be a string).
getHTML(format=False)Render the full HTML document. When format=True, the output is prettified with BeautifulSoup.prettify().
saveToFile(filename)Open filename in write mode and write the unformatted HTML from getHTML().
save(destination)Write prettified HTML (getHTML(format=True)) to an already-open file object destination.
escapeCharacters(text)Escape &, <, >, ", and ', then apply inline markup substitutions (bold, italic, size spans, etc.). Called automatically on all text content.
Button(...)Add a <button> element.
Label(...)Add a <p> element.
Text(...)Add a <textarea> element.
Heading(level=1, ...)Add an <h1><h6> element.
Image(src="", ...)Add a self-closing <img /> element.
Link(href=..., ...)Add an <a> element.
Frame(content=..., ...)Add a <div> container. Pass a Parent instance as content to nest child elements.
Script(code="", src=None, ...)Add a <script> tag with inline code or an external src.
TextNode(text="", escape=True)Add a raw text node. Set escape=False to inject unescaped HTML.
CustomTag(tag_name="div", ...)Add any HTML tag by name.
__call__(*args, escape=True, sep=" ")Call the Parent like a function to add a text node from positional string arguments.
__getitem__(index)Retrieve a tag dictionary by its integer index in self.tags.
__delitem__(index)Delete a tag from self.tags by index.
__str__()Return the prettified HTML document (equivalent to getHTML(format=True)).

Optional GUI editor

When you install py2html with the Install Py2Html GUI option selected, the installer also writes py2htmlGUI.py to the same directory. Launching that file opens a Tkinter window with a hierarchical element tree on the left, property dialogs for every element type, a live HTML preview pane, and export to .html or .json project files. The GUI requires tkinterweb, webview, and sv_ttk in addition to the core dependencies.

Build docs developers (and LLMs) love