MarkupPy: An HTML/XML Generator
MarkupPy is a Python module designed to simplify the generation of HTML/XML from Python code in an intuitive, lightweight, and Pythonic manner. It supports both Python 2 and 3 environments, offering a straightforward approach to creating markup. The library maintains an active status with intermittent releases, with version 1.18 being published in March 2025.
Warnings
- gotcha MarkupPy is a lightweight generator and not intended as a full-blown web framework, templating engine, or XML parser. For robust production environments or complex needs, alternatives like ElementTree are suggested.
- gotcha When adding content to `<pre>` tags, versions prior to 1.17 might insert extra newlines between additions. Use the `add_raw()` method (available from v1.17+) to preserve exact formatting without unwanted newlines.
- gotcha Direct embedding of inline CSS and JavaScript content via the `init()` method's `style_content` and `script_content` arguments is only supported in version 1.16 and newer.
- gotcha Due to the existence of the `page.add()` method for inserting raw text, 'add' cannot be used as an HTML/XML element name (e.g., `page.add(...)` for creating an `<add>` tag) as it would clash with the method.
- gotcha While MarkupPy states compatibility with Python 2 and 3, Python 2 has reached End-of-Life. It is strongly recommended to use MarkupPy with Python 3 for modern development and security.
Install
-
pip install MarkupPy
Imports
- markup
from MarkupPy import markup
Quickstart
from MarkupPy import markup
# Create an HTML page
page = markup.page()
page.init(title="My Title", lang="en")
page.h1("Welcome!")
page.p("Hello, World!")
page.p.close()
page.a("Visit GitHub", href="https://github.com/tylerbakke/MarkupPy")
print(page)