This guide walks you through building a complete HTML page from scratch using py2html. In fewer than 30 lines of Python you will create a heading, a paragraph, a button, a link, an image, and a containerDocumentation 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.
Frame — then render the whole thing to a formatted .html file.
Make sure py2html is installed and importable before continuing. See the Installation guide if you have not set it up yet.
Full example
The code below demonstrates every element covered in this quickstart. Read through it once, then follow the step-by-step breakdown underneath.Step-by-step breakdown
Create the Parent object
Every py2html page starts with a At this point
Parent. It maintains an ordered list of all elements you add and is responsible for rendering the final HTML.page holds an empty element list. The root <html>, <head>, and <body> tags are added automatically when you call getHTML().You can also configure the Parent with config() after construction:config() accepts title (sets the document title string) and style (must be a string).Add a Heading
Heading renders an <h1> through <h6> tag. The level parameter controls which heading rank is used (default 1).level=1, text="Py2HTML Heading", fg="#333", bg="transparent", bd="none", bdradius="0px", padx=0, pady=0.The method appends the heading to page.tags and returns the integer index of the new element — useful if you want to reference it later with page[index].Add a Label and a Button
Label renders a <p> tag. Button renders a <button> tag. Both accept the same colour and padding parameters.Button: fg="#00a651", bg="#deffee", bd="none", bdradius="50px", text="Py2HTML", padx=15, pady=15.Default values for Label: fg="#000", bg="#fff", bd="none", bdradius="50px", text="Py2HTML", padx=15, pady=15.Notice the [* … *] syntax inside the Label text. py2html’s inline markup processor converts this to <strong>…</strong> automatically.py2html also provides Text for multiline text input (<textarea>), with the same parameter set as Label.Add a Link and an Image
Link renders an <a> tag. The href parameter is kept raw (not HTML-escaped as content) so that valid URLs are preserved correctly.Image renders a self-closing <img /> tag. Supply width and height as CSS strings such as "400px" or "50%".Link: href="https://example.com", text="Click Here", fg="#00a651", bg="transparent", bd="none", bdradius="0px", padx=0, pady=0, text_decor="underline".Default values for Image: src="", alt="", width=None, height=None, bd="none", bdradius="0px".Group elements in a Frame
Frame renders a <div>. To place multiple elements inside it, first create a child Parent, add elements to that, then pass it as the content argument.content_manager="flex" enables flexbox on the container. The flex_config dictionary maps directly to CSS flex properties. The content_manager parameter (default "block") controls the CSS display property of the <div>.Full Frame signature: Frame(fg="#000", bg="#fff", bd="none", bdradius="50px", content="Py2HTML", padx=15, pady=15, width="auto", height="auto", marginx=0, marginy=0, content_manager="block", flex_config={...}, position="static", offset={...}).Render to HTML and save to file
Call
getHTML(format=True) to produce a complete, prettified HTML string. Use saveToFile() to write it to disk in one call, or save() to write prettified HTML to an already-open file object.saveToFile(filename) opens the file in write mode and calls getHTML() internally (without format=True), so the saved HTML is compact. save(destination) calls getHTML(format=True) and writes the prettified result to the file object you provide.str(page) (or page.__str__()) is equivalent to page.getHTML(format=True) and returns a prettified HTML string directly.Tag indexing and deletion
Every element method returns the integer index of the newly added tag inpage.tags. You can use page[index] to retrieve the raw tag dictionary, or del page[index] to remove it.
Calling the Parent directly
Calling aParent instance like a function adds a TextNode from one or more string arguments:
sep keyword argument (default " ") controls the separator used when joining multiple positional arguments.
Expected HTML output (abbreviated)
Running the example produces a document similar to the following. The exact whitespace depends onBeautifulSoup.prettify().
Next steps
Now that you have a working page, explore the rest of the element API:- Add inline JavaScript with
page.Script(code="alert('hello!')") - Insert raw HTML with
page.TextNode(text="<hr>", escape=False) - Use any HTML tag name with
page.CustomTag(tag_name="article", content="…") - Build multi-section layouts by nesting
Framecontainers inside each other