Skip to main content

HTML library

Project description

UiDOM

An HTML library for python

This library is inspired from dominate html library and takes it further. It supports jinja templating and many more features that we expect from an html library. We can even create Custom Elements and Web Components in UiDOM.

Installation

> pip install uidom

An Alpinejs toggle example

#!/usr/bin/env python
# app.py
"""
 This example should work as is. 
"""
from fastapi import FastAPI
from uidom import Document
from uidom.dom import Component, script, title, div
from uidom.routing.fastapi import StreamingRoute

document = Document(body=[
    script(src="https://unpkg.com/alpinejs@3.10.2/dist/cdn.min.js", defer=None, rel="prefetch")
    ])

api = FastAPI() 
api.router.route_class = StreamingRoute

class ToggleButton(Component):

    def render(self):
        with div(x_data={'open': 'true'}) as toggle:
            with div(x_on_click='open = !open'):
                div("Opened", x_show="open"), 
                div("Closed", x_show="!open"), 
        return toggle

class App(Component):

    def render(self, *args, **kwargs):
        return document(*args, **kwargs, , head=title('App Page'))


@api.get('/')
def index():
    return App(ToggleButton())

A Jinja template example

from uidom.dom import nav, ul, For, li, a, Var, JinjaElement
from collections import namedtuple as nt


class Nav(JinjaElement):
    def render(self):
        return nav(
            ul(
                For(
                    "item in menu_items",
                    li(a(Var("item.name"), href=Var("item.link"))),
                )
            )
        )

# or we can write Jinja Element directly

Nav = lambda: JinjaElement(nav(ul(For("item in menu_items", li(a(Var("item.name"), href=Var("item.link")))))))

nav_bar = Nav()
menu_url = nt("menu_url", "name link")

# nav_bar element is a jinja template and has an internal representation as follows 
<nav>
  <ul>
    {% for item in menu_items %}
      <li>
        <a href="{{ item.link }}">
          {{ item.name }}
        </a>
      </li>
    {% endfor %}
  </ul>
</nav>
# now we can use nav_bar just like we use jinja templates and render it as follows

nav_bar(
    menu_items=[
        menu_url("Home", r"\home.html"),
        menu_url("About", r"\about.html"),
        menu_url("Contact Us", r"\contact_us.html"),
    ]
)

# it creates an element as follows
<nav>
  <ul>
      <li>
        <a href="\home.html">
          Home
        </a>
      </li>
      <li>
        <a href="\about.html">
          About
        </a>
      </li>
      <li>
        <a href="\contact_us.html">
          Contact Us
        </a>
      </li>
  </ul>
</nav>

using markdown with uidom elements

from uidom.dom import MarkdownElement

em_text = MarkdownElement("*hello world*")

print(em_text)
# it returns following string
<p>
  <em>
    hello world
  </em>
</p>
# MarkdownElement can be used as follows too 

class HelloWorld(MarkdownElement):
    
    def render(self):
        return "*hello world*"

# now HelloWorld instance gives the same 
# output
print(HelloWorld())
<p>
  <em>
    hello world
  </em>
</p>

using raw html with uidom elements

from uidom.dom import *


class Modal(Component):

    def render(self, *args, **kwargs):
        return '''
<div x-data="{ open: false }">
    <!-- Button -->
    <button x-on:click="open = true" type="button"
        class="px-4 py-2 bg-white border border-black focus:outline-none focus:ring-4 focus:ring-aqua-400">
        Login
    </button>

    <!-- Modal -->
    <div x-show="open" x-on:keydown.escape.prevent.stop="open = false" role="dialog" aria-modal="true"
        x-id="['modal-title']" :aria-labelledby="$id('modal-title')" class="fixed inset-0 overflow-y-auto">
        <!-- Overlay -->
        <div x-show="open" x-transition.opacity class="fixed inset-0 bg-black bg-opacity-50"></div>

        <!-- Panel -->
        <div x-show="open" x-transition x-on:click="open = false"
            class="relative flex items-center justify-center min-h-screen p-4">
            <div x-on:click.stop x-trap.noscroll.inert="open"
                class="relative w-full max-w-2xl p-8 overflow-y-auto bg-white border border-black rounded-md">
               <form wire:submit.prevent="login" action="" class="">
                   <!-- Title -->
                <h2 class="text-3xl font-medium" :id="$id('modal-title')">Confirm</h2>
                <!-- Content -->
                <div class="space-y-3">
                    <div class="">
                        <input wire:model.defer="email" type="email" class="w-full rounded-md">
                    </div>

                    <div class="">
                        <input wire:model.defer="password" type="password" class="w-full rounded-md">
                    </div>
                </div>
                <!-- Buttons -->
                <div class="flex mt-8 space-x-2">
                    <button  type="submit" x-on:click="open = false"
                        class="px-4 py-2 bg-white border border-black focus:outline-none focus:ring-4 focus:ring-aqua-400">
                        Confirm
                    </button>
                    <button type="button" x-on:click="open = false"
                        class="px-4 py-2 bg-white border border-black focus:outline-none focus:ring-4 focus:ring-aqua-400">
                        Cancel
                    </button>
                </div>
               </form>
            </div>
        </div>
    </div>
</div>'''

LICENSE

this library is licensed under MIT (MIT-LICENSE)

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

uidom-0.4a12.tar.gz (112.1 kB view details)

Uploaded Source

Built Distribution

uidom-0.4a12-py3-none-any.whl (136.7 kB view details)

Uploaded Python 3

File details

Details for the file uidom-0.4a12.tar.gz.

File metadata

  • Download URL: uidom-0.4a12.tar.gz
  • Upload date:
  • Size: 112.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/5.15.0-84-generic

File hashes

Hashes for uidom-0.4a12.tar.gz
Algorithm Hash digest
SHA256 49a63e756ca771c9d9a6dce0c81be6e51e7129ffd546263328125470b94e8dbb
MD5 9b7a2bdf7476311835844d145958a1d6
BLAKE2b-256 d1bc46a463b8f748a8417fb2a1b0545adcb069c82cb49e68380ce7f4e69d7dab

See more details on using hashes here.

File details

Details for the file uidom-0.4a12-py3-none-any.whl.

File metadata

  • Download URL: uidom-0.4a12-py3-none-any.whl
  • Upload date:
  • Size: 136.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/5.15.0-84-generic

File hashes

Hashes for uidom-0.4a12-py3-none-any.whl
Algorithm Hash digest
SHA256 6f28d95350bb1a18721cd1fca0be45c0bac077a04fb1a67aef61a279f8eb060e
MD5 c88245fe120431bd23d14ccdef6d8ed0
BLAKE2b-256 6772849201db02cf866e257cc840ac23ca01658bb997018d5620b3d224cfbb3b

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