View layer for Python VDOMs
Project description
viewdom
viewdom brings modern frontend templating patterns to Python:
Installation
Installation follows the normal Python packaging:
$ pip install viewdom
Quick Examples
Use htm to generate a VDOM, then render to convert to a string:
result = render(html('''<div>Hello World</div>'''))
# '<div>Hello World</div>'
If you’d like, you can split those into two steps:
vdom = html('''<div>Hello World</div>''')
result = render(vdom)
# '<div>Hello World</div>'
Insert variables from the local or global scope:
name = 'viewdom'
result = render(html('<div>Hello {name}</div>'))
# '<div>Hello viewdom</div>'
Expressions aren’t some special language, it’s just Python in inside curly braces:
name = 'viewdom'
result = render(html('<div>Hello {name.upper()}</div>'))
# '<div>Hello VIEWDOM</div>'
Rendering something conditionally is also “just Python”:
message = 'Say Howdy'
not_message = 'So Sad'
show_message = True
result = render(html('''
<h1>Show?</h1>
{message if show_message else not_message}
'''))
# '<h1>Show?</h1>Say Howdy'
Looping? Yes, “just Python”:
message = 'Hello'
names = ['World', 'Universe']
result = render(html('''
<ul title="{message}">
{[
html('<li>{name}</li>')
for name in names
] }
</li>
'''))
Reusable components and subcomponents, passing props and children:
title = 'My Todos'
todos = ['first']
def Todo(label):
return html('<li>{label}</li>')
def TodoList(todos):
return html('<ul>{[Todo(label) for label in todos]}</ul>')
result = render(html('''
<h1>{title}</h1>
<{TodoList} todos={todos} />
'''))
# '<h1>My Todos</h1><ul><li>first</li></ul>'
Tired of passing props down a deep tree and want something like React context/hooks?
title = 'My Todos'
todos = ['first']
def Todo(label):
prefix = use_context('prefix')
return html('<li>{prefix}{label}</li>')
def TodoList(todos):
return html('<ul>{[Todo(label) for label in todos]}</ul>')
result = render(html('''
<{Context} prefix="Item: ">
<h1>{title}</h1>
<{TodoList} todos={todos} />
<//>
'''))
# '<h1>My Todos</h1><ul><li>Item: first</li></ul>'
Acknowledgments
The idea and code for viewdom – the rendering, the idea of a theadlocal context, obviously tagged and htm… essentially everything – come from Joachim Viide.
Changelog
0.4.0
Switch to a dataclass (frozen, slots) data structure for VDOMs, making it easy to assign type hints, do autocomplete, and have mypy get involved.
Add more examples and docs
0.3.0
Allow callable subcomponents, e.g. dataclasses with an __call__, to be rendered in html() calls
0.2.0
Switch docs to sphinx-book-theme and MyST
Publish to PyPI
0.1.0
First release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file viewdom-0.4.0.tar.gz
.
File metadata
- Download URL: viewdom-0.4.0.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.44.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22c9eba0c069d68f08f2640362702e03dc4e216b75375880586c31194816bc76 |
|
MD5 | b9520d88df41f547919a352f974f6485 |
|
BLAKE2b-256 | af56e08d0e1a4e14e67195ecf3ad75d058d85524232f76ede08bb5663ac39fb0 |
File details
Details for the file viewdom-0.4.0-py2.py3-none-any.whl
.
File metadata
- Download URL: viewdom-0.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.44.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed8ef10f2f05df1dc2450c0d26f734316ee279fdb6adfb762e8b91b1c9f733b9 |
|
MD5 | 5d50c9e02d8fc174476231049fb79200 |
|
BLAKE2b-256 | 560ca4265c4549b56ac9e433246e9a41d5b641bb91d3d11f7027e01f2df0e336 |