Skip to main content

Overview

markupy is a plain Python alternative to traditional templates engines for generating HTML code.

Writing this code in Python with markupy...

# Import "elements" like they were regular Python objects
from markupy.elements import A, Body, Head, Html, Li, P, Title, Ul

menu = [("Home", "/"), ("About us", "/about"), ("Contact", "/contact")]
print(
    Html[
        Head[Title["My website"]],
        Body[
            P["Table of contents:"],
            Ul(".menu")[(Li[A(href=url)[title]] for title, url in menu)],
        ],
    ]
)

...will generate this HTML:

<!doctype html>
<html>
  <head>
    <title>My website</title>
  </head>
  <body>
    <p>Table of contents:</p>
    <ul class="menu">
      <li><a href="/">Home</a></li>
      <li><a href="/about">About us</a></li>
      <li><a href="/contact">Contact</a></li>
    </ul>
  </body>
</html>

Seems interesting? Try it by yourself with our online html2markupy converter.

Motivation

Like most Python web developers, we have relied on template engines (Jinja, Django, ...) since forever to generate HTML on the server side. Although this is fine for simple needs, when your site grows bigger, you might start facing some issues:

  • More an more Python code get put into unreadable and untestable macros

  • Extends and includes make it very hard to track required parameters

  • Templates are very permissive regarding typing making it more error prone

If this is you struggling with templates, then you should definitely give markupy a try!

Inspiration

markupy started as a fork of htpy. Even though the two projects are still conceptually very similar, we started markupy in order to support a slightly different syntax to optimize readability, reduce risk of conflicts with variables, and better support for non native html attributes syntax. On top of that, markupy provides a first class support for class based components.

Key Features

  • Leverage static types: Use mypy or pyright to type check your code.

  • Great debugging: Avoid cryptic stack traces from templates. Use your favorite Python debugger.

  • Easy to extend: There is no special way to define template tags/filters. Just call regular functions.

  • Works with existing Python web framework: Works great with Django, Flask or any other Python web framework!

  • Works great with htmx: markupy makes for a great experience when writing server rendered partials/components.

  • Create reusable components: Define components, snippets, complex layouts/pages as regular Python variables or functions.

  • Familiar concepts from React: React helped make it popular writing HTML with a programming language. markupy uses a lot of similar constructs.

  • Customizable: Use or create 3rd party libraries to leverage the power of markupy

Philosophy

markupy generates HTML elements and attributes and provide a few helpers.

markupy does not enforce any particular pattern or style to organize your pages, components and layouts. That does not mean that markupy cannot be used to build sophisticated web pages or applications.

Rather the opposite: you are encouraged the leverage the power of Python to structure your project. Use modules, classes, functions, decorators, list comprehension, generators, conditionals, static typing and any other feature of Python to organize your components. This gives you a lot of power and makes markupy scale from a small one file project to bigger applications.

Installation

markupy is available on PyPI. You may install the latest version using pip:

pip install markupy

Documentation

The full documentation is available at markupy.witiz.com:

Metadata

Release files for markupy 2.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markupy 2.5.0
File Size Uploaded Provenance Actions
markupy-2.5.0.tar.gz 91.6 kB Without provenance Details for markupy-2.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for markupy 2.5.0
File Interpreter ABI Platform Actions
markupy-2.5.0-py3-none-any.whl Python 3 none any Details for markupy-2.5.0-py3-none-any.whl

Total release size: 115.0 kB

Release files / markupy-2.5.0.tar.gz

Download URL markupy-2.5.0.tar.gz
Size 91.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1b79dd1d8d93bb4dd49a5e0ffb66a108a8ae9f5499e80df52587f8e5e87ec1fb
BLAKE2b-256 checksum
How to use checksums
0c6f997834eeb410def1a1ff4bc8e6390de00a3e4f76ea8e170cd41281dbf136
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Release files / markupy-2.5.0-py3-none-any.whl

Download URL markupy-2.5.0-py3-none-any.whl
Size 23.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd4a4f320ca40e38329b8729f4c06c03bd1257672c4e71f63a2d85a0c6d6be1a
BLAKE2b-256 checksum
How to use checksums
5da8e0ddf41b97a57026409d0529a1dc0a433ddfb43938fee09409b3ee0994d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Security for markupy 2.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page