Skip to main content
Pypi Package Version Supported Python Versions Wheel support Package format License info Package status Pipeline status

XPLANT

Pure pythonic tree structure builder and markup language translator. No dependencies, quite fast, pretty syntax, no templates.

  • Enter tree nodes with python’s context managers.

  • Cast the tree to given markup, just by calling str on it.

  • Enjoy 1:1 translation from python to given markup.

Basically it’s suited for XML and similar markups (SVG, HTML) but it can be used for probably any structured markup.

Written mostly to be an python inline html generator.

XML Example

from xplant import xml_plant

x = xml_plant()
with x.node("section_a", attribute_1=1):
    with x.node("nested_1", empty=True):
        pass
    with x.node("nested_2"):
        x.comment("Can handle also comments.")
        for number in range(3):
            x.leaf("a_number_{:02}".format(number), num=number)

Will give:

<?xml version="1.0" encoding="utf-8"?>
<section_a attribute_1="1">
  <nested_1 empty="true"></nested_1>
  <nested_2>
    <!-- Can handle also comments. -->
    <a_number_00 num="0" />
    <a_number_01 num="1" />
    <a_number_02 num="2" />
  </nested_2>
</section_a>

HTML generation Example

css = """
    ul.navigation {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #f2fff5;
    }
    li {
      float: left;
    }
    li a {
      display: block;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }
    code {
        background-color: #ded;
        padding: 4px;
        border-radius: 3px;
    }
"""
navigation = [
    ("Home", "../home.html", "red"),
    ("Things", "stuff.html", "green"),
    ("About", "../about.html", "blue"),
]
text = [
    "This page has ben generated with python's <code> xplant.html.html5_plant </code>.",
    "Enjoy pure pythonic <code>1:1 python -> xml</code> translation.",
    "break",
    "Did you ever had hard times with learning <code>HTML template language</code>? ",
    "It's a crude way to mix HTML with any logics like iterators, classes, conditions.",
    "break",
    "You know what? You already have all of it (and much more) in <code>python</code>! ",
    "HTML templates is a blind alley. HTML does not miss server-side scripting.",
    "The python miss a good HTML generator not vice versa.",
]

from xplant import html5_plant

x = html5_plant()

with x.html():
    with x.head():
        x.meta(charset="utf-8")
        x.meta(http_equiv="Content-Security-Policy")
        x.line("title", "no templates, no headache")
        # line is a helper for creating text in a xml tag
        with x.style():
            x.text(css)

    with x.body(style="margin:28px;"):
        with x.header():
            x.line("h2", "XPLANT", id="title")
            x.line("h4", "It makes good things for you")

        x.comment("HERE COMES THE NAVIGATION")
        with x.ul(id="navigation"):
            x.comment("CHECK OUT THIS LIST")
            for name, link_url, link_color in navigation:
                with x.li():
                    with x.a(href=link_url, style="color:%s;" % link_color):
                        x.text("%s in %s" % (name, link_color))

        x.comment("HERE COMES MAIN SECTION")
        with x.main(style="margin:20px;"):
            for paragraph in text:
                with x.p():
                    if paragraph == "break":
                        x.br()
                    else:
                        x.text(paragraph)
print(p)

Gives such a string:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Security-Policy" />
    <title>no templates, no headache</title>
    <style>
        ul.navigation {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #f2fff5;
        }
        li {
          float: left;
        }
        li a {
          display: block;
          color: white;
          text-align: center;
          padding: 14px 16px;
          text-decoration: none;
        }
        code {
            background-color: #ded;
            padding: 4px;
            border-radius: 3px;
        }
    </style>
  </head>
  <body style="margin:28px;">
    <header>
      <h2 id="title">XPLANT</h2>
      <h4>It makes good things for you</h4>
    </header>
    <!-- HERE COMES THE NAVIGATION -->
    <ul id="navigation">
      <!-- CHECK OUT THIS LIST -->
      <li><a href="../home.html" style="color:red;">Home in red</a></li>
      <li><a href="stuff.html" style="color:green;">Things in green</a></li>
      <li><a href="../about.html" style="color:blue;">About in blue</a></li>
    </ul>
    <!-- HERE COMES MAIN SECTION -->
    <main style="margin:20px;">
      <p>This page has ben generated with python's <code> xplant.html.html5_plant </code>.</p>
      <p>Enjoy pure pythonic <code>1:1 python -> xml</code> translation.</p>
      <p><br /></p>
      <p>Did you ever had hard times with learning <code>HTML template language</code>? </p>
      <p>It's a crude way to mix HTML with any logics like iterators, classes, conditions.</p>
      <p><br /></p>
      <p>You know what? You already have all of it (and much more) in <code>python</code>! </p>
      <p>HTML templates is a blind alley. HTML does not miss server-side scripting.</p>
      <p>The python miss a good HTML generator not vice versa.</p>
    </main>
  </body>
</html>

Release files for xplant 1.0.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 xplant 1.0.0
File Size Uploaded
xplant-1.0.0.tar.gz 17.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xplant 1.0.0
File Interpreter ABI Platform
xplant-1.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 32.4 kB

Release files / xplant-1.0.0.tar.gz

Download URL xplant-1.0.0.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5ccda5209d3f154af6440bdfc43e2909555a0b13338997ed90c084443fe51011
BLAKE2b-256 checksum
How to use checksums
b85eef6e96666610177c8fa47a975f60675f0e09179f80ef1d74b4ae0be968d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release files / xplant-1.0.0-py2.py3-none-any.whl

Download URL xplant-1.0.0-py2.py3-none-any.whl
Size 15.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
06aec5f8198f63cb06d2474497dd611e81b7c195afcc71138dee6a056b49785c
BLAKE2b-256 checksum
How to use checksums
d8a8915729d15d513ade5d7dff62f57308ada0edb584924bd84056bedfc809a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.3.2

1 release file

0.3.1

2 release files

0.3.0

2 release files

0.2.1

1 release file

0.2

2 release files

0.1.1

2 release files

0.1

2 release files

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