Skip to main content

Syntags: Write your markup in Python, easily

Features  •  Install  •  License  •  Docs

Syntags lets you generate HTML, XML, SVG, etc. by writing concise, real Python code.

Syntags was built from the ground up for readable code. First-class support for components, custom elements, and fragments is baked into the design.

Syntags takes a different approach to Pyxl — taking Python's own syntax and repurposing it, rather than using a custom character encoding and transforming your invalid code into valid code.

  • The code you write is real Python, no magic.
  • You don't need to learn the quirks of a new syntax.
  • It's easy to get started with and integrate into your code.
  • Highlighting works, and your linter will only be slightly angry.

A brief note on the syntax ~

PEP 8 isn't gospel. Creating a beautiful syntax means ignoring certain parts of it, and that's okay! The second section, "A Foolish Consistency is the Hobgoblin of Little Minds," even summarises this.

View a relevant excerpt

. . . know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best.

Some other good reasons to ignore a particular guideline:

  1. When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP.

  2. . . .

Check out the Syntags styleguide for recommendations on how to maintain good code quality.

Features

an expressive syntax that makes sense

Structured like a programming language, because it is one. No context switching between templating and programming.

import syntags as tags
from syntags.html import *

app = html (lang="en") [
  head [
    title ["Some basic markup"]
  ],
  body [
    main [
      ...  # etc...
    ]
  ]
]

rendered = tags.render(app)

intuitive syntax shorthands

It's effortless to write compact code that's easy to read and maintain.

body [
  div .main_wrapper / main [
    ...
  ]
]

# The same, but without the shorthand:
body [
  div (classes={"main-wrapper"}) [
    main [
      ...
    ]
  ]
]

reusable, declarative components

Similar to React, but very much Python.

import syntags as tags
from syntags.html import *

@tags.component
def hello_message(name, **attrs):
  return div [
    f"Hello {name}"
  ]


rv = tags.render(hello_message (name="world"))
View the React JSX equivalent
import React from "react";
import ReactDOMServer from "react-dom/server";

function HelloMessage({ name, ...props }) {
  return (
    <div>
      Hello {name}
    </div>
  );
}


rv = ReactDOMServer.renderToStaticMarkup(<HelloMessage name="world" />)

Keep in mind, this is in JSX. Compiling it is yet another step. Syntags' syntax is pure Python, no intermediate steps required.

first-class fragments, no wizardry required

Fragments are just a sequence of elements, and they can be nested infinitely.

HEAD_DATA = [
  link (rel="canonical", href="https://rob.ac/"),
  link (rel="stylesheet", href="dist/css/layout.css"),
  link (rel="shortcut icon", href="favicon.ico", type="image/x-icon")
]

html (lang="en") [
  head [
    HEAD_DATA
  ],
  ...
]

simple, safe rendering

Everything becomes a string and gets escaped, except for raw strings.

import syntags as tags
from syntags.html import *

rendered = tags.render(
  html (lang="en") [
    tags.raw("""
    <head>
      <title>A good example</title>
      <meta charset="UTF-8">
    </head>
    """),
    body [
      p [
        "<b>This will get escaped</b>"
      ]
    ]
  ]
)

more than just HTML

Syntags comes with a bunch of namespaces to make your life easy.

  • HTML tags: html
  • XML prolog and tag factory: xml
  • Some predefined XML namespaces: svg, rss, sitemap
  • Common, useful components: ext

Install

You can use Pip, or anything that can install from PyPI, such as Poetry or Pipenv.

$ pip install syntags
+ Markdown component (syntags.ext.markdown)
$ pip install syntags[markdown]

License

Syntags is licensed under the ISC license, a simplified version of the MIT license. You can modify this project and use it in whatever you want! The only condition for redistribution is that you must include a copy of the license.

There's a copy in syntags/__init__.py, so you'll be fine as long as you don't remove that! Otherwise, you can include the copy found in the root of the repository.

Metadata

Release files for syntags 0.2.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 syntags 0.2.0
File Size Uploaded Provenance Actions
syntags-0.2.0.tar.gz 18.7 kB Without provenance Details for syntags-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 38.5 kB

Release files / syntags-0.2.0.tar.gz

Download URL syntags-0.2.0.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
855723bd44ceef10f32f41125aca49bbb44fac6b40bfd5febc6c307122815588
BLAKE2b-256 checksum
How to use checksums
dbff552ab4a927b723bd94b02b1d1eaaa0ffa0cbe9c87a3bb878ff44dbe30f88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.3 CPython/3.8.1 Windows/10

Release files / syntags-0.2.0-py3-none-any.whl

Download URL syntags-0.2.0-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a312281c9d286b77ff9d976bc9a21ccfdc88def0261a933d0e910eb75dfc10d
BLAKE2b-256 checksum
How to use checksums
c4ff968e5782df4b49c5bef23eb3a6749221e913d0f2e35bd5bd2122655723c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.3 CPython/3.8.1 Windows/10

Security for syntags 0.2.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

0.2.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