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.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
Hashes for viewdom-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c38ca7d6a6e2b38c6b630a6e1496c16d50135d19d82d6c59a68e65fa5d4ae55 |
|
MD5 | 7b0308ba091035303fb0ca87866a4267 |
|
BLAKE2b-256 | e537d7370b391795917d53d8c4b68dade6ca6c8194228e4e5f5705ea1cd76e92 |