Genshi

0.7.10 · active · verified Thu Apr 16

Genshi is a Python library that provides an integrated set of components for parsing, generating, and processing HTML, XML, or other textual content for output generation on the web. It features a template language inspired by Kid. The library is actively maintained, with version 0.7.10 released in November 2025, and generally sees several patch releases per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple HTML template using `MarkupTemplate`, pass data to it, and render the output. It showcases basic variable substitution (`py:content` and `${expression}`) and iteration (`py:for`).

from genshi.template import MarkupTemplate

# Define a simple Genshi markup template string
template_string = '''
<html xmlns:py="http://genshi.edgewall.org/">
  <head>
    <title py:content="title"></title>
  </head>
  <body>
    <p>Hello, <em py:content="name"></em>!</p>
    <ul>
      <li py:for="item in items">${item}</li>
    </ul>
  </body>
</html>
'''

# Create a MarkupTemplate object
tmpl = MarkupTemplate(template_string)

# Data to render into the template
data = {
    'title': 'Genshi Example',
    'name': 'World',
    'items': ['Apple', 'Banana', 'Cherry']
}

# Generate the output stream
stream = tmpl.generate(**data)

# Render the stream to a string
output = stream.render('html')

print(output)

view raw JSON →