TheDocumentation 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.
Parent class is the single building block every py2html project starts with. It acts as both a document root and a reusable container: it keeps an internal, ordered list of element dictionaries (self.tags), exposes methods that append to that list, and renders the whole structure to an HTML string whenever you call getHTML(). Once you understand how Parent manages its tag list, every other part of the library falls into place.
Internal Structure: self.tags
When you instantiate Parent, two attributes are created:
self.html— a working string used during rendering (reset on eachgetHTML()call).self.tags— a plain Pythonlistof dictionaries, one per element.
Button, Label, Frame) appends a dictionary to self.tags that describes the element’s tag type, text content, and style values. The list order is exactly the order the elements appear in the rendered HTML.
Adding Elements and the Returned Index
Every element method returnslen(self.tags) - 1 — the zero-based index of the element it just added. Store this value to reference the tag dict later via __getitem__, or to delete it with __delitem__.
Indices shift when you delete an element. If you delete index 0, what was index 1 becomes index 0. Re-query
len(page.tags) after any deletion if you rely on stored indices.__getitem__ and __delitem__
Parent supports bracket-style access and deletion:
IndexError for out-of-range indices.
__call__: Adding a TextNode Inline
Calling a Parent instance directly is shorthand for TextNode(). All positional arguments are joined with the sep keyword (default " ") and appended as a text node. The escape keyword (default True) controls HTML-escaping. __call__ returns self.tags[-1] — the tag dictionary that was just appended (not an integer index).
__str__: Human-Readable HTML
str(page) calls getHTML(format=True, insideframe=False), which runs the output through BeautifulSoup’s prettify() for indented, readable HTML. This is the same output that save() writes to a file.
config(**kwargs): Document-Level Settings
Use config to set the document title and an optional global stylesheet string:
| kwarg | Type | Effect |
|---|---|---|
title | str | Sets self.title on the Parent |
style | str | Sets self.style; must be a string |
config() raises ValueError if style is not a string.The getHTML() Rendering Pipeline
getHTML(format=False, insideframe=False) iterates over self.tags and builds the HTML string in three phases:
- Document wrapper — unless
insideframe=True, a full<html><head><title>…</title></head><body …>shell is prepended. - Element rendering — for each tag dict, style parts are assembled into a
style="…"attribute, extra attrs are serialised, and the opening/closing tags are written. - Closing wrapper —
</body></html>is appended (unlessinsideframe=True).
format=True, the final string is passed through bs4.BeautifulSoup.prettify() for indentation.
Self-Closing vs Paired Tags
getHTML keeps an internal list of self-closing tags that are rendered as <tag … /> with no closing tag or content:
button, p, div, a, h1–h6, textarea) is rendered as a <tag>…content…</tag> pair.
Nesting with Frame()
Frame() accepts another Parent instance as its content argument. When it detects a Parent, it stores content.tags (the inner list) instead of a plain string. During rendering, getHTML creates a temporary Parent, assigns that inner tag list, and calls getHTML(insideframe=True) on it — producing nested HTML without a duplicate <html> wrapper.
Because
Frame stores content.tags by reference at the time of the call, mutations to card.tags after the Frame() call will not be reflected. Build inner Parent objects fully before passing them to Frame.