Skip to main content

markupy - HTML in Python

Project description

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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markupy-2.2.3.tar.gz (89.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markupy-2.2.3-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file markupy-2.2.3.tar.gz.

File metadata

  • Download URL: markupy-2.2.3.tar.gz
  • Upload date:
  • Size: 89.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for markupy-2.2.3.tar.gz
Algorithm Hash digest
SHA256 1bc2d73634b4c48bced6de9a8dd29b90bcbf48cfcae610fad205db747abf5db0
MD5 6d3d27ecc0bc7e57b24269b159c33221
BLAKE2b-256 31e54b413f78c05dd1efd7f2112e552642954399555594d0ecffa164b49c3531

See more details on using hashes here.

File details

Details for the file markupy-2.2.3-py3-none-any.whl.

File metadata

  • Download URL: markupy-2.2.3-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for markupy-2.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9afe8c0646e75b89b19d13e23fcaee279b5e1d89f8c59c8f772ca3f686a77da4
MD5 53063b59299cf1f0098c0f9c852a5156
BLAKE2b-256 5f76ed2ab2aeaf93f17b72ac64a54bab40590d009d8a5513eaf8918396f4a222

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page