HTML in Python
work-in-progress
contributions appreciated
pyxy
lets you put HTML directly in Python code. You can think of it as JSX for Python.
It builds on the fantastic work of pyxl, which is a similar project predating JSX.
Compared to pyxl, it takes a new approach that makes other tools do most of the work:
parso
provides the tokenization and parsing, using a custom grammar to enable handling XMLhtpy
is used to rebuild markup
Here's a minimal example:
def is_logged_in() -> bool:
return False
animal_images = ["cat.png", "dog.png", "cow.png"]
status_image = "logged-in.png" if is_logged_in() else "logged-out.png"
def demo():
return (
<div>
<img src={status_image} />
<ul>
{ <li><img src={image_file} /></li> for image_file in animal_images }
</ul>
</div>
)
print(demo())
<div><img src="logged-out.png"><ul><li><img src="cat.png"></li><li><img src="dog.png"></li><li><img src="cow.png"></li></ul></div>
- PyCharm Plugin
- Sublime Text syntax highlighting
- VSCode - not yet supported, see #10
ruff
- partial support, see #8mypy
- not yet supported, see #9pyright
- not yet supported, see #11black
- not yet supported, see #7
- dropbox/pyxl - The original
- gvanrossum/pyxl3 - Python 3 support
- pyxl4/pyxl4 - A fork created when
pyxl3
was no longer maintained - twidi/mixt - Another fork of Pyxl with a lot of features
- michaeljones/packed - A unique effort that uses decorators and a compilation step
- pelme/htpy - Not HTML-in-Python, but close. A great solution to generating HTML with Python
- RudreshVeerkhare/ReactPy - Another implementation of HTML in Python
- christoffer/pycharm-pyxl - Pyxl support for PyCharm
- yyjhao/sublime-pyxl - Pyxl syntax highlighting for Sublime Text
- ECMAScript 4 XML (E4X) - The oldest example I can find of XML being embedded in another language (2004!)
- JSX - A document detailing the motivations for JSX over other alternatives
- gox - HTML in Go
- templ - HTML in Go (but templates?)
- rsx - HTML in Rust
- syn-rsx - HTML in Rust
- rstml - HTML in Rust
- LuaX - HTML in Lua
- gccx - HTML in C++
- rux - HTML in Ruby
- php