Skip to main content

View layer for Python VDOMs

Project description

viewdom

Docs and Code

viewdom brings modern frontend templating patterns to Python:

  • tagged to have language-centered templating (like JS tagged templates)

  • htm.py to generate virtual DOM structures from a template run (like the htm JS package)

  • viewdom to render a VDOM to a markup string, along with other modern machinery

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

Unreleased

  • Allow VDOM to contain any kind of sequence

0.4.1

  • Change typing: VDOM is now the list/node at the top, VDOMNode is the factory and an individual triple

  • Re-organize the examples

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

viewdom-0.4.2.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

viewdom-0.4.2-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file viewdom-0.4.2.tar.gz.

File metadata

  • Download URL: viewdom-0.4.2.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.0

File hashes

Hashes for viewdom-0.4.2.tar.gz
Algorithm Hash digest
SHA256 ad28b37f79319478b9936b1d06f96e9828057a041d5f25b4c5cd1c6be4a7a737
MD5 1ff4b4901110663b21bda3927462c6ac
BLAKE2b-256 9466cc247e39c0a9a39e77bad5d7bf4c647cc8cf00c5fe2c8664a88c7bbb2348

See more details on using hashes here.

File details

Details for the file viewdom-0.4.2-py2.py3-none-any.whl.

File metadata

  • Download URL: viewdom-0.4.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.0

File hashes

Hashes for viewdom-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 65a8842553303935ebd00dc2a2b3d6359889f39c8204bdf858d346da2c73c3a9
MD5 6b078b9f9230f2d40a633b6038d629df
BLAKE2b-256 a877604d5cb74b229e47df9e70f27638f7d72019f1cbada06607deca8ec5a748

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page