Skip to main content

Declarative Python DSL for building HTML documents programmatically

Project description

Библиотека для декларативного формирования HTML-структуры на Python.

Текущая ветка 0.1.x рассматривается как финальная синхронная линия библиотеки. Дальнейшие крупные изменения, включая поддержку асинхронности, планируются в 0.2.0.

Установка

pip install git+https://github.com/opolonix/domica-html.git@v0.1.5

Для локальной разработки:

pip install -e .[dev]

Пример

from domica_html import html, div

doc = html()

with doc:
    div("hello world")

print(doc.render())

output:

<html>
    <div>
        hello world
    </div>
</html>

Реализация собственного компонента

from domica_html import html, div, inc, node_container, script, line
from contextvars import ContextVar
from collections import defaultdict
from typing import Type

external_tags: ContextVar[dict[Type, list["external_container"]]] = ContextVar("external_tags", default=defaultdict(list))

class external_container(node_container):
    def __init__(self, *, anchor=False):
        super().__init__(anchor=anchor)
        if not anchor:
            tags = external_tags.get()
            tags[self.__class__].append(self)

    def render(self):
        tags = external_tags.get()

        for child in tags[self.__class__]:
            if child is self: continue
            self.add_child(child)

        with inc(indent=inc.indent-1):
            return super().render()

class global_script(external_container): ...

doc = html()

with doc:
    with div():
        div("Hello world with some script", onclick="hello_on_click")
        with global_script():
            line("const hello_on_click = () => {")
            line(inc.char, "alert('hello!');")
            line("}")

    with script():
        global_script(anchor=True)

print(doc.render())

output:

<html>
    <div>
        <div onclick="hello_on_click">  
            Hello world with some script
        </div>
    </div>
    <script>
        const hello_on_click = () => {  
            alert('hello!');
        }
    </script>
</html>

Релизы

  • 0.1.5: финальная стабильная синхронная версия ветки 0.1.x

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

domica_html-0.1.4.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

domica_html-0.1.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file domica_html-0.1.4.tar.gz.

File metadata

  • Download URL: domica_html-0.1.4.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for domica_html-0.1.4.tar.gz
Algorithm Hash digest
SHA256 36e9457b5c9258d87d85bf6d0aad6b6c9b2cd0f8315e7d5fa2c66e62a60a0966
MD5 f45cc89ad63b5da07853c05a21fca28f
BLAKE2b-256 6fe71ffc00d162a46f4b01ba0f31d490e2730c7d8397f51438da5159a462ba7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for domica_html-0.1.4.tar.gz:

Publisher: publish.yml on opolonix/domica-html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file domica_html-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: domica_html-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for domica_html-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 38605d94d364745ea3f186cc2d54dd90985fa79c767ddf154ee1413160381e4c
MD5 975993ad231b8bee57398a2283a5f6d4
BLAKE2b-256 97c12a59706d400b9b456d471bc2b8f23f43ffb24aae06c47c829f4f401287f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for domica_html-0.1.4-py3-none-any.whl:

Publisher: publish.yml on opolonix/domica-html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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