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 pages are static HTML documents, but they can include fully interactive JavaScript through the Script() method. Calling Script() appends a <script> tag to the current Parent object, and you can use it for inline code, external file references, or ES module imports. Unlike text content added through Label() or Button(), the JavaScript you supply is stored and rendered completely raw — no HTML escaping is applied.

Script() signature

Script(code="", src=None, **kwargs)
ParameterTypeDescription
codestrInline JavaScript to embed inside the <script> tag. Stored raw, never HTML-escaped.
srcstrURL or path to an external .js file. Rendered as the src attribute.
**kwargsanyExtra HTML attributes passed to the <script> tag (e.g., type, defer, async_).
If you supply both code and src, py2html prints a warning to stderr and silently discards the inline code. Only the src attribute will appear in the rendered tag. Never supply both at once.

Inline JavaScript

Pass your JavaScript as a string to the code parameter. The content is placed verbatim between the opening and closing <script> tags. This means you can use any JavaScript syntax, including template literals, regular expressions, and angle brackets, without worrying about HTML entity encoding.
import py2html

page = py2html.Parent()
page.Heading(level=1, text="Interactive Page")
page.Button(text="Click me")

page.Script(code="""
document.querySelector('button').addEventListener('click', function () {
    alert('Hello from py2html!');
});
""")

page.saveToFile("interactive.html")
Inline JavaScript is stored as a raw string and is never run through py2html’s escapeCharacters() method. Characters like <, >, &, and " in your JavaScript will appear exactly as written in the output file. Only use inline scripts from trusted sources.

External script file

Use the src parameter to load a JavaScript file from a URL or a relative path. py2html HTML-escapes the src value itself (so the URL is safe to embed as an HTML attribute), but it generates no inline content between the tags.
import py2html

page = py2html.Parent()
page.Heading(level=1, text="My App")
page.Label(text="Powered by an external script.")

page.Script(src="https://cdn.example.com/app.js")

page.saveToFile("app.html")
This produces:
<script src="https://cdn.example.com/app.js"></script>

Passing HTML attributes via kwargs

Any keyword argument beyond code and src is rendered as an HTML attribute on the <script> tag. This covers common attributes like type, defer, and async.
import py2html

page = py2html.Parent()
page.Heading(level=1, text="ES Module Example")

# type="module" enables import/export syntax.
# defer=True ensures the script runs after the document is parsed.
page.Script(
    src="/static/js/main.module.js",
    type="module",
    defer=True,
)

page.saveToFile("module.html")
The kwargs are emitted directly as attribute strings. For example, defer=True renders as defer="True", and type="module" renders as type="module".
Place Script() calls at the end of your element list, just before calling getHTML() or saveToFile(). This mirrors the HTML best practice of loading scripts after the document body so elements are available in the DOM when the script runs.

Complete example

The following example builds a small page with a counter button, an external utility library, and a deferred inline initialiser script.
import py2html

page = py2html.Parent()

page.Heading(level=1, text="Click Counter", fg="#333")
page.Button(text="Click me — 0 clicks", fg="#fff", bg="#00a651", bdradius="8px")
page.Label(text="Watch the button text update as you click.")

# External dependency loaded async so it does not block rendering
page.Script(src="https://cdn.example.com/lodash.min.js", async_=True)

# Inline script wired to the button — runs after DOM is ready
page.Script(code="""
var count = 0;
var btn = document.querySelector('button');
btn.addEventListener('click', function () {
    count += 1;
    btn.textContent = 'Click me \u2014 ' + count + ' click' + (count === 1 ? '' : 's');
});
""")

page.saveToFile("counter.html")
print("Saved counter.html")

Build docs developers (and LLMs) love