Skip to content

Latest commit

 

History

History
116 lines (69 loc) · 2.43 KB

extra-features.rst

File metadata and controls

116 lines (69 loc) · 2.43 KB

Extra Features

Optionally installable features of IDOM.

To install a stable set of features for IDOM simply run.

pip install idom[stable]

To install all extra features run

pip install idom[all]

Sanic Layout Server

At the moment this is the only supported :ref:`Layout Server` available for IDOM:

pip install idom[sanic]

Python Language Extension

IDOM includes a transpiler for writing JSX-like syntax in a normal Python file!

# dialect=html
from idom import html

message = "hello world!"
attrs = {"height": "10px", "width": "10px"}
model = html(f"<div ...{attrs}><p>{message}</p></div>")

assert model == {
    "tagName": "div",
    "attributes": {"height": "10px", "width": "10px"},
    "children": [{"tagName": "p", "children": ["hello world!"]}],
}

With Jupyter and IPython support:

%%dialect html
from idom import html
assert html(f"<div/>") == {"tagName": "div"}

That you can install with pip:

pip install idom[dialect]

Usage

  1. Import idom in your application's entrypoint.py
  2. Import your_module.py with a # dialect=html header comment.
  3. Inside your_module.py import html from idom
  4. Run python entrypoint.py from your console.

So here's the files you should have set up:

project
|-  entrypoint.py
|-  your_module.py

The contents of entrypoint.py should contain:

import idom  # this needs to be first!
import your_module

While your_module.py should contain the following:

# dialect=html
from idom import html
assert html(f"<div/>") == {"tagName": "div"}

And that's it!

How It Works

Once idom has been imported at your application's entrypoint, any following modules imported with a # dialect=html header comment get transpiled just before they're executed. This is accomplished by using Pyalect to hook a transpiler into Pythons import system. The :class:`~idom.dialect.HtmlDialectTranspiler` which implements Pyalect's :class:`~pyalect.dialect.Transpiler` interface using some tooling from htm.py.