airium


Nameairium JSON
Version 0.2.6 PyPI version JSON
download
home_pagehttps://gitlab.com/kamichal/airium
SummaryEasy and quick html builder with natural syntax correspondence (python->html). No templates needed. Serves pure pythonic library with no dependencies.
upload_time2023-09-17 20:12:45
maintainerMichał Kaczmarczyk
docs_urlNone
authorMichał Kaczmarczyk
requires_python
licenseMIT license
keywords natural html generator compiler template-less
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Airium

Bidirectional `HTML`-`python` translator.

[![PyPI version](https://img.shields.io/pypi/v/airium.svg)](https://pypi.python.org/pypi/airium/)
[![pipeline status](https://gitlab.com/kamichal/airium/badges/master/pipeline.svg)](https://gitlab.com/kamichal/airium/-/commits/master)
[![coverage report](https://gitlab.com/kamichal/airium/badges/master/coverage.svg)](https://gitlab.com/kamichal/airium/-/commits/master)
[![PyPI pyversion](https://img.shields.io/pypi/pyversions/AIRIUM.svg)](https://pypi.org/project/airium/)
[![PyPI license](https://img.shields.io/pypi/l/AIRIUM.svg)](https://pypi.python.org/pypi/airium/)
[![PyPI status](https://img.shields.io/pypi/status/AIRIUM.svg)](https://pypi.python.org/pypi/airium/)

Key features:

- simple, straight-forward
- template-less (just the python, you may say goodbye to all the templates)
- DOM structure is strictly represented by python indentation (with context-managers)
- gives much cleaner `HTML` than regular templates
- equipped with reverse translator: `HTML` to python
- can output either pretty (default) or minified `HTML` code

# Generating `HTML` code in python using `airium`

#### Basic `HTML` page (hello world)

```python
from airium import Airium

a = Airium()

a('<!DOCTYPE html>')
with a.html(lang="pl"):
    with a.head():
        a.meta(charset="utf-8")
        a.title(_t="Airium example")

    with a.body():
        with a.h3(id="id23409231", klass='main_header'):
            a("Hello World.")

html = str(a)  # casting to string extracts the value
# or directly to UTF-8 encoded bytes:
html_bytes = bytes(a)  # casting to bytes is a shortcut to str(a).encode('utf-8')

print(html)
```

Prints such a string:

```html
<!DOCTYPE html>
<html lang="pl">
  <head>
    <meta charset="utf-8" />
    <title>Airium example</title>
  </head>
  <body>
    <h3 id="id23409231" class="main_header">
      Hello World.
    </h3>
  </body>
</html>
```

In order to store it as a file, just:

```python
with open('that/file/path.html', 'wb') as f:
    f.write(bytes(html))
```

#### Simple image in a div

```python
from airium import Airium

a = Airium()

with a.div():
    a.img(src='source.png', alt='alt text')
    a('the text')

html_str = str(a)
print(html_str)
```

```html

<div>
    <img src="source.png" alt="alt text"/>
    the text
</div>
```

#### Table

```python
from airium import Airium

a = Airium()

with a.table(id='table_372'):
    with a.tr(klass='header_row'):
        a.th(_t='no.')
        a.th(_t='Firstname')
        a.th(_t='Lastname')

    with a.tr():
        a.td(_t='1.')
        a.td(id='jbl', _t='Jill')
        a.td(_t='Smith')  # can use _t or text

    with a.tr():
        a.td(_t='2.')
        a.td(_t='Roland', id='rmd')
        a.td(_t='Mendel')

table_str = str(a)
print(table_str)

# To store it to a file:
with open('/tmp/airium_www.example.com.py') as f:
    f.write(table_str)
```

Now `table_str` contains such a string:

```html

<table id="table_372">
  <tr class="header_row">
    <th>no.</th>
    <th>Firstname</th>
    <th>Lastname</th>
  </tr>
  <tr>
    <td>1.</td>
    <td id="jbl">Jill</td>
    <td>Smith</td>
  </tr>
  <tr>
    <td>2.</td>
    <td id="rmd">Roland</td>
    <td>Mendel</td>
  </tr>
</table>
```

### Chaining shortcut for elements with only one child

_New in version 0.2.2_

Having a structure with large number of `with` statements:

```python
from airium import Airium

a = Airium()

with a.article():
    with a.table():
        with a.thead():
            with a.tr():
                a.th(_t='Column 1')
                a.th(_t='Column 2')
        with a.tbody():
            with a.tr():
                with a.td():
                    a.strong(_t='Value 1')
                a.td(_t='Value 2')

table_str = str(a)
print(table_str)
```

You may use a shortcut that is equivalent to:

```python
from airium import Airium

a = Airium()

with a.article().table():
    with a.thead().tr():
        a.th(_t="Column 1")
        a.th(_t="Column 2")
    with a.tbody().tr():
        a.td().strong(_t="Value 1")
        a.td(_t="Value 2")

table_str = str(a)
print(table_str)
```

```html

<article>
  <table>
    <thead>
      <tr>
        <th>Column 1</th>
        <th>Column 2</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <strong>Value 1</strong>
        </td>
        <td>Value 2</td>
      </tr>
    </tbody>
  </table>
</article>
```

# Options

### Pretty or Minify

By default, airium biulds `HTML` code indented with spaces and with line breaks being line feed `\n` characters.
It can be changed while creating an `Airium` instance. In general all avaliable arguments whit their default values are:

```python
a = Airium(
    base_indent='  ',  # str
    current_level=0,  # int
    source_minify=False,  # bool
    source_line_break_character="\n",  # str
)
```

#### minify

That's a mode when size of the code is minimized, i.e. contains as less whitespaces as it's possible.
The option can be enabled with `source_minify` argument, i.e.:

```python
a = Airium(source_minify=True)
```

In case if you need to explicitly add a line break in the source code (not the `<br/>`):

```python
a = Airium(source_minify=True)
a.h1(_t="Here's your table")
with a.table():
    with a.tr():
        a.break_source_line()
        a.th(_t="Cell 11")
        a.th(_t="Cell 12")
    with a.tr():
        a.break_source_line()
        a.th(_t="Cell 21")
        a.th(_t="Cell 22")
    a.break_source_line()
a.p(_t="Another content goes here")
```

Will result with such a code:

```html
<h1>Here's your table</h1><table><tr>
<th>Cell 11</th><th>Cell 12</th></tr><tr>
<th>Cell 21</th><th>Cell 22</th></tr>
</table><p>Another content goes here</p>
```

Note that the `break_source_line` cannot be used
in [context manager chains](#chaining-shortcut-for-elements-with-only-one-child).

#### indent style

The default indent of the generated HTML code has two spaces per each indent level.
You can change it to `\t` or 4 spaces by setting `Airium` constructor argument, e.g.:

```python
a = Airium(base_indent="\t")  # one tab symbol
a = Airium(base_indent="    ")  # 4 spaces per each indentation level
a = Airium(base_indent=" ")  # 1 space per one level
# pick one of the above statements, it can be mixed with other arguments
```

Note that this setting is ignored when `source_minify` argument is set to `True` (see above).

There is a special case when you set the base indent to empty string. It would disable indentation,
but line breaks will be still added. In order to get rid of line breaks, check the `source_minify` argument.

#### indent level

The `current_level` being an integer can be set to non-negative
value, wich will cause `airium` to start indentation with level offset given by the number.

#### line break character

By default, just a line feed (`\n`) is used for terminating lines of the generated code.
You can change it to different style, e.g. `\r\n` or `\r` by setting `source_line_break_character` to the desired value.

```python
a = Airium(source_line_break_character="\r\n")  # windows' style
```

Note that the setting has no effect when `source_minify` argument is set to `True` (see above).

# Using airium with web-frameworks

Airium can be used with frameworks like Flask or Django. It can completely replace
template engines, reducing code-files scater, which may bring better code organization, and some other reasons.

Here is an example of using airium with django. It implements reusable `basic_body` and a view called `index`.

```python
# file: your_app/views.py
import contextlib
import inspect

from airium import Airium
from django.http import HttpResponse


@contextlib.contextmanager
def basic_body(a: Airium, useful_name: str = ''):
    """Works like a Django/Ninja template."""

    a('<!DOCTYPE html>')
    with a.html(lang='en'):
        with a.head():
            a.meta(charset='utf-8')
            a.meta(content='width=device-width, initial-scale=1', name='viewport')
            # do not use CSS from this URL in a production, it's just for an educational purpose
            a.link(href='https://unpkg.com/@picocss/pico@1.4.1/css/pico.css', rel='stylesheet')
            a.title(_t=f'Hello World')

        with a.body():
            with a.div():
                with a.nav(klass='container-fluid'):
                    with a.ul():
                        with a.li():
                            with a.a(klass='contrast', href='./'):
                                a.strong(_t="⌨ Foo Bar")
                    with a.ul():
                        with a.li():
                            a.a(klass='contrast', href='#', **{'data-theme-switcher': 'auto'}, _t='Auto')
                        with a.li():
                            a.a(klass='contrast', href='#', **{'data-theme-switcher': 'light'}, _t='Light')
                        with a.li():
                            a.a(klass='contrast', href='#', **{'data-theme-switcher': 'dark'}, _t='Dark')

                with a.header(klass='container'):
                    with a.hgroup():
                        a.h1(_t=f"You're on the {useful_name}")
                        a.h2(_t="It's a page made by our automatons with a power of steam engines.")

            with a.main(klass='container'):
                yield  # This is the point where main content gets inserted

            with a.footer(klass='container'):
                with a.small():
                    margin = 'margin: auto 10px;'
                    a.span(_t='© Airium HTML generator example', style=margin)

            # do not use JS from this URL in a production, it's just for an educational purpose
            a.script(src='https://picocss.com/examples/js/minimal-theme-switcher.js')


def index(request) -> HttpResponse:
    a = Airium()
    with basic_body(a, f'main page: {request.path}'):
        with a.article():
            a.h3(_t="Hello World from Django running Airium")
            with a.p().small():
                a("This bases on ")
                with a.a(href="https://picocss.com/examples/company/"):
                    a("Pico.css / Company example")

            with a.p():
                a("Instead of a HTML template, airium has been used.")
                a("The whole body is generated by a template "
                  "and the article code looks like that:")

            with a.code().pre():
                a(inspect.getsource(index))

    return HttpResponse(bytes(a))  # from django.http import HttpResponse
```

Route it in `urls.py` just like a regular view:

```python
# file: your_app/urls.py
from django.contrib import admin
from django.urls import path

import your_app

urlpatterns = [
    path('index/', your_app.views.index),
    path('admin/', admin.site.urls),
]
```

The result ing web page on my machine looks like that:

![Airium/Django templateless example](airium_django_example.png)

# Reverse translation

Airium is equipped with a transpiler `[HTML -> py]`.
It generates python code out of a given `HTML` string.

### Using reverse translator as a binary:

Ensure you have [installed](#installation) `[parse]` extras. Then call in command line:

```bash
airium http://www.example.com
```

That will fetch the document and translate it to python code.
The code calls `airium` statements that reproduce the `HTML` document given.
It may give a clue - how to define `HTML` structure for a given
web page using `airium` package.

To store the translation's result into a file:

```bash
airium http://www.example.com > /tmp/airium_example_com.py
```

You can also parse local `HTML` files:

```bash
airium /path/to/your_file.html > /tmp/airium_my_file.py
```

You may also try to parse your Django templates. I'm not sure if it works,
but there will be probably not much to fix.

### Using reverse translator as python code:

```python
from airium import from_html_to_airium

# assume we have such a page given as a string:
html_str = """\
<!DOCTYPE html>
<html lang="pl">
  <head>
    <meta charset="utf-8" />
    <title>Airium example</title>
  </head>
  <body>
    <h3 id="id23409231" class="main_header">
      Hello World.
    </h3>
  </body>
</html>
"""

# to convert the html into python, just call:

py_str = from_html_to_airium(html_str)

# airium tests ensure that the result of the conversion is equal to the string:
assert py_str == """\
#!/usr/bin/env python
# File generated by reverse AIRIUM translator (version 0.2.6).
# Any change will be overridden on next run.
# flake8: noqa E501 (line too long)

from airium import Airium

a = Airium()

a('<!DOCTYPE html>')
with a.html(lang='pl'):
    with a.head():
        a.meta(charset='utf-8')
        a.title(_t='Airium example')
    with a.body():
        a.h3(klass='main_header', id='id23409231', _t='Hello World.')
"""
```

### <a name="transpiler_limitations">Transpiler limitations</a>

> so far in version 0.2.2:

- result of translation does not keep exact amount of leading whitespaces
  within `<pre>` tags. They come over-indented in python code.

This is not however an issue when code is generated from python to `HTML`.

- although it keeps the proper tags structure, the transpiler does not
  chain all the `with` statements, so in some cases the generated
  code may be much indented.

- it's not too fast

# <a name="installation">Installation</a>

If you need a new virtual environment, call:

```bash
virtualenv venv
source venv/bin/activate
```

Having it activated - you may install airium like this:

```bash
pip install airium
```

In order to use reverse translation - two additional packages are needed, run:

```bash
pip install airium[parse]
```

Then check if the transpiler works by calling:

```bash
airium --help
```

> Enjoy!

            

Raw data

            {
    "_id": null,
    "home_page": "https://gitlab.com/kamichal/airium",
    "name": "airium",
    "maintainer": "Micha\u0142 Kaczmarczyk",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "michal.s.kaczmarczyk@gmail.com",
    "keywords": "natural html generator compiler template-less",
    "author": "Micha\u0142 Kaczmarczyk",
    "author_email": "michal.s.kaczmarczyk@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/98/f843cd8969409e913b0535ae15771f86d35aed87484372de6fa4e48b283f/airium-0.2.6.tar.gz",
    "platform": null,
    "description": "## Airium\n\nBidirectional `HTML`-`python` translator.\n\n[![PyPI version](https://img.shields.io/pypi/v/airium.svg)](https://pypi.python.org/pypi/airium/)\n[![pipeline status](https://gitlab.com/kamichal/airium/badges/master/pipeline.svg)](https://gitlab.com/kamichal/airium/-/commits/master)\n[![coverage report](https://gitlab.com/kamichal/airium/badges/master/coverage.svg)](https://gitlab.com/kamichal/airium/-/commits/master)\n[![PyPI pyversion](https://img.shields.io/pypi/pyversions/AIRIUM.svg)](https://pypi.org/project/airium/)\n[![PyPI license](https://img.shields.io/pypi/l/AIRIUM.svg)](https://pypi.python.org/pypi/airium/)\n[![PyPI status](https://img.shields.io/pypi/status/AIRIUM.svg)](https://pypi.python.org/pypi/airium/)\n\nKey features:\n\n- simple, straight-forward\n- template-less (just the python, you may say goodbye to all the templates)\n- DOM structure is strictly represented by python indentation (with context-managers)\n- gives much cleaner `HTML` than regular templates\n- equipped with reverse translator: `HTML` to python\n- can output either pretty (default) or minified `HTML` code\n\n# Generating `HTML` code in python using `airium`\n\n#### Basic `HTML` page (hello world)\n\n```python\nfrom airium import Airium\n\na = Airium()\n\na('<!DOCTYPE html>')\nwith a.html(lang=\"pl\"):\n    with a.head():\n        a.meta(charset=\"utf-8\")\n        a.title(_t=\"Airium example\")\n\n    with a.body():\n        with a.h3(id=\"id23409231\", klass='main_header'):\n            a(\"Hello World.\")\n\nhtml = str(a)  # casting to string extracts the value\n# or directly to UTF-8 encoded bytes:\nhtml_bytes = bytes(a)  # casting to bytes is a shortcut to str(a).encode('utf-8')\n\nprint(html)\n```\n\nPrints such a string:\n\n```html\n<!DOCTYPE html>\n<html lang=\"pl\">\n  <head>\n    <meta charset=\"utf-8\" />\n    <title>Airium example</title>\n  </head>\n  <body>\n    <h3 id=\"id23409231\" class=\"main_header\">\n      Hello World.\n    </h3>\n  </body>\n</html>\n```\n\nIn order to store it as a file, just:\n\n```python\nwith open('that/file/path.html', 'wb') as f:\n    f.write(bytes(html))\n```\n\n#### Simple image in a div\n\n```python\nfrom airium import Airium\n\na = Airium()\n\nwith a.div():\n    a.img(src='source.png', alt='alt text')\n    a('the text')\n\nhtml_str = str(a)\nprint(html_str)\n```\n\n```html\n\n<div>\n    <img src=\"source.png\" alt=\"alt text\"/>\n    the text\n</div>\n```\n\n#### Table\n\n```python\nfrom airium import Airium\n\na = Airium()\n\nwith a.table(id='table_372'):\n    with a.tr(klass='header_row'):\n        a.th(_t='no.')\n        a.th(_t='Firstname')\n        a.th(_t='Lastname')\n\n    with a.tr():\n        a.td(_t='1.')\n        a.td(id='jbl', _t='Jill')\n        a.td(_t='Smith')  # can use _t or text\n\n    with a.tr():\n        a.td(_t='2.')\n        a.td(_t='Roland', id='rmd')\n        a.td(_t='Mendel')\n\ntable_str = str(a)\nprint(table_str)\n\n# To store it to a file:\nwith open('/tmp/airium_www.example.com.py') as f:\n    f.write(table_str)\n```\n\nNow `table_str` contains such a string:\n\n```html\n\n<table id=\"table_372\">\n  <tr class=\"header_row\">\n    <th>no.</th>\n    <th>Firstname</th>\n    <th>Lastname</th>\n  </tr>\n  <tr>\n    <td>1.</td>\n    <td id=\"jbl\">Jill</td>\n    <td>Smith</td>\n  </tr>\n  <tr>\n    <td>2.</td>\n    <td id=\"rmd\">Roland</td>\n    <td>Mendel</td>\n  </tr>\n</table>\n```\n\n### Chaining shortcut for elements with only one child\n\n_New in version 0.2.2_\n\nHaving a structure with large number of `with` statements:\n\n```python\nfrom airium import Airium\n\na = Airium()\n\nwith a.article():\n    with a.table():\n        with a.thead():\n            with a.tr():\n                a.th(_t='Column 1')\n                a.th(_t='Column 2')\n        with a.tbody():\n            with a.tr():\n                with a.td():\n                    a.strong(_t='Value 1')\n                a.td(_t='Value 2')\n\ntable_str = str(a)\nprint(table_str)\n```\n\nYou may use a shortcut that is equivalent to:\n\n```python\nfrom airium import Airium\n\na = Airium()\n\nwith a.article().table():\n    with a.thead().tr():\n        a.th(_t=\"Column 1\")\n        a.th(_t=\"Column 2\")\n    with a.tbody().tr():\n        a.td().strong(_t=\"Value 1\")\n        a.td(_t=\"Value 2\")\n\ntable_str = str(a)\nprint(table_str)\n```\n\n```html\n\n<article>\n  <table>\n    <thead>\n      <tr>\n        <th>Column 1</th>\n        <th>Column 2</th>\n      </tr>\n    </thead>\n    <tbody>\n      <tr>\n        <td>\n          <strong>Value 1</strong>\n        </td>\n        <td>Value 2</td>\n      </tr>\n    </tbody>\n  </table>\n</article>\n```\n\n# Options\n\n### Pretty or Minify\n\nBy default, airium biulds `HTML` code indented with spaces and with line breaks being line feed `\\n` characters.\nIt can be changed while creating an `Airium` instance. In general all avaliable arguments whit their default values are:\n\n```python\na = Airium(\n    base_indent='  ',  # str\n    current_level=0,  # int\n    source_minify=False,  # bool\n    source_line_break_character=\"\\n\",  # str\n)\n```\n\n#### minify\n\nThat's a mode when size of the code is minimized, i.e. contains as less whitespaces as it's possible.\nThe option can be enabled with `source_minify` argument, i.e.:\n\n```python\na = Airium(source_minify=True)\n```\n\nIn case if you need to explicitly add a line break in the source code (not the `<br/>`):\n\n```python\na = Airium(source_minify=True)\na.h1(_t=\"Here's your table\")\nwith a.table():\n    with a.tr():\n        a.break_source_line()\n        a.th(_t=\"Cell 11\")\n        a.th(_t=\"Cell 12\")\n    with a.tr():\n        a.break_source_line()\n        a.th(_t=\"Cell 21\")\n        a.th(_t=\"Cell 22\")\n    a.break_source_line()\na.p(_t=\"Another content goes here\")\n```\n\nWill result with such a code:\n\n```html\n<h1>Here's your table</h1><table><tr>\n<th>Cell 11</th><th>Cell 12</th></tr><tr>\n<th>Cell 21</th><th>Cell 22</th></tr>\n</table><p>Another content goes here</p>\n```\n\nNote that the `break_source_line` cannot be used\nin [context manager chains](#chaining-shortcut-for-elements-with-only-one-child).\n\n#### indent style\n\nThe default indent of the generated HTML code has two spaces per each indent level.\nYou can change it to `\\t` or 4 spaces by setting `Airium` constructor argument, e.g.:\n\n```python\na = Airium(base_indent=\"\\t\")  # one tab symbol\na = Airium(base_indent=\"    \")  # 4 spaces per each indentation level\na = Airium(base_indent=\" \")  # 1 space per one level\n# pick one of the above statements, it can be mixed with other arguments\n```\n\nNote that this setting is ignored when `source_minify` argument is set to `True` (see above).\n\nThere is a special case when you set the base indent to empty string. It would disable indentation,\nbut line breaks will be still added. In order to get rid of line breaks, check the `source_minify` argument.\n\n#### indent level\n\nThe `current_level` being an integer can be set to non-negative\nvalue, wich will cause `airium` to start indentation with level offset given by the number.\n\n#### line break character\n\nBy default, just a line feed (`\\n`) is used for terminating lines of the generated code.\nYou can change it to different style, e.g. `\\r\\n` or `\\r` by setting `source_line_break_character` to the desired value.\n\n```python\na = Airium(source_line_break_character=\"\\r\\n\")  # windows' style\n```\n\nNote that the setting has no effect when `source_minify` argument is set to `True` (see above).\n\n# Using airium with web-frameworks\n\nAirium can be used with frameworks like Flask or Django. It can completely replace\ntemplate engines, reducing code-files scater, which may bring better code organization, and some other reasons.\n\nHere is an example of using airium with django. It implements reusable `basic_body` and a view called `index`.\n\n```python\n# file: your_app/views.py\nimport contextlib\nimport inspect\n\nfrom airium import Airium\nfrom django.http import HttpResponse\n\n\n@contextlib.contextmanager\ndef basic_body(a: Airium, useful_name: str = ''):\n    \"\"\"Works like a Django/Ninja template.\"\"\"\n\n    a('<!DOCTYPE html>')\n    with a.html(lang='en'):\n        with a.head():\n            a.meta(charset='utf-8')\n            a.meta(content='width=device-width, initial-scale=1', name='viewport')\n            # do not use CSS from this URL in a production, it's just for an educational purpose\n            a.link(href='https://unpkg.com/@picocss/pico@1.4.1/css/pico.css', rel='stylesheet')\n            a.title(_t=f'Hello World')\n\n        with a.body():\n            with a.div():\n                with a.nav(klass='container-fluid'):\n                    with a.ul():\n                        with a.li():\n                            with a.a(klass='contrast', href='./'):\n                                a.strong(_t=\"\u2328 Foo Bar\")\n                    with a.ul():\n                        with a.li():\n                            a.a(klass='contrast', href='#', **{'data-theme-switcher': 'auto'}, _t='Auto')\n                        with a.li():\n                            a.a(klass='contrast', href='#', **{'data-theme-switcher': 'light'}, _t='Light')\n                        with a.li():\n                            a.a(klass='contrast', href='#', **{'data-theme-switcher': 'dark'}, _t='Dark')\n\n                with a.header(klass='container'):\n                    with a.hgroup():\n                        a.h1(_t=f\"You're on the {useful_name}\")\n                        a.h2(_t=\"It's a page made by our automatons with a power of steam engines.\")\n\n            with a.main(klass='container'):\n                yield  # This is the point where main content gets inserted\n\n            with a.footer(klass='container'):\n                with a.small():\n                    margin = 'margin: auto 10px;'\n                    a.span(_t='\u00a9 Airium HTML generator example', style=margin)\n\n            # do not use JS from this URL in a production, it's just for an educational purpose\n            a.script(src='https://picocss.com/examples/js/minimal-theme-switcher.js')\n\n\ndef index(request) -> HttpResponse:\n    a = Airium()\n    with basic_body(a, f'main page: {request.path}'):\n        with a.article():\n            a.h3(_t=\"Hello World from Django running Airium\")\n            with a.p().small():\n                a(\"This bases on \")\n                with a.a(href=\"https://picocss.com/examples/company/\"):\n                    a(\"Pico.css / Company example\")\n\n            with a.p():\n                a(\"Instead of a HTML template, airium has been used.\")\n                a(\"The whole body is generated by a template \"\n                  \"and the article code looks like that:\")\n\n            with a.code().pre():\n                a(inspect.getsource(index))\n\n    return HttpResponse(bytes(a))  # from django.http import HttpResponse\n```\n\nRoute it in `urls.py` just like a regular view:\n\n```python\n# file: your_app/urls.py\nfrom django.contrib import admin\nfrom django.urls import path\n\nimport your_app\n\nurlpatterns = [\n    path('index/', your_app.views.index),\n    path('admin/', admin.site.urls),\n]\n```\n\nThe result ing web page on my machine looks like that:\n\n![Airium/Django templateless example](airium_django_example.png)\n\n# Reverse translation\n\nAirium is equipped with a transpiler `[HTML -> py]`.\nIt generates python code out of a given `HTML` string.\n\n### Using reverse translator as a binary:\n\nEnsure you have [installed](#installation) `[parse]` extras. Then call in command line:\n\n```bash\nairium http://www.example.com\n```\n\nThat will fetch the document and translate it to python code.\nThe code calls `airium` statements that reproduce the `HTML` document given.\nIt may give a clue - how to define `HTML` structure for a given\nweb page using `airium` package.\n\nTo store the translation's result into a file:\n\n```bash\nairium http://www.example.com > /tmp/airium_example_com.py\n```\n\nYou can also parse local `HTML` files:\n\n```bash\nairium /path/to/your_file.html > /tmp/airium_my_file.py\n```\n\nYou may also try to parse your Django templates. I'm not sure if it works,\nbut there will be probably not much to fix.\n\n### Using reverse translator as python code:\n\n```python\nfrom airium import from_html_to_airium\n\n# assume we have such a page given as a string:\nhtml_str = \"\"\"\\\n<!DOCTYPE html>\n<html lang=\"pl\">\n  <head>\n    <meta charset=\"utf-8\" />\n    <title>Airium example</title>\n  </head>\n  <body>\n    <h3 id=\"id23409231\" class=\"main_header\">\n      Hello World.\n    </h3>\n  </body>\n</html>\n\"\"\"\n\n# to convert the html into python, just call:\n\npy_str = from_html_to_airium(html_str)\n\n# airium tests ensure that the result of the conversion is equal to the string:\nassert py_str == \"\"\"\\\n#!/usr/bin/env python\n# File generated by reverse AIRIUM translator (version 0.2.6).\n# Any change will be overridden on next run.\n# flake8: noqa E501 (line too long)\n\nfrom airium import Airium\n\na = Airium()\n\na('<!DOCTYPE html>')\nwith a.html(lang='pl'):\n    with a.head():\n        a.meta(charset='utf-8')\n        a.title(_t='Airium example')\n    with a.body():\n        a.h3(klass='main_header', id='id23409231', _t='Hello World.')\n\"\"\"\n```\n\n### <a name=\"transpiler_limitations\">Transpiler limitations</a>\n\n> so far in version 0.2.2:\n\n- result of translation does not keep exact amount of leading whitespaces\n  within `<pre>` tags. They come over-indented in python code.\n\nThis is not however an issue when code is generated from python to `HTML`.\n\n- although it keeps the proper tags structure, the transpiler does not\n  chain all the `with` statements, so in some cases the generated\n  code may be much indented.\n\n- it's not too fast\n\n# <a name=\"installation\">Installation</a>\n\nIf you need a new virtual environment, call:\n\n```bash\nvirtualenv venv\nsource venv/bin/activate\n```\n\nHaving it activated - you may install airium like this:\n\n```bash\npip install airium\n```\n\nIn order to use reverse translation - two additional packages are needed, run:\n\n```bash\npip install airium[parse]\n```\n\nThen check if the transpiler works by calling:\n\n```bash\nairium --help\n```\n\n> Enjoy!\n",
    "bugtrack_url": null,
    "license": "MIT license",
    "summary": "Easy and quick html builder with natural syntax correspondence (python->html). No templates needed. Serves pure pythonic library with no dependencies.",
    "version": "0.2.6",
    "project_urls": {
        "Homepage": "https://gitlab.com/kamichal/airium"
    },
    "split_keywords": [
        "natural",
        "html",
        "generator",
        "compiler",
        "template-less"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cbbcd174ebf44e4cf8cd967bca5e181e254bd2f95afbb5367471382713736bf5",
                "md5": "9c0e7ac18bfc5f0506d98f55ffa9b547",
                "sha256": "50af5cf491e084f27909e29a93550b4170e587cde01334d58c6249644ee8c6c2"
            },
            "downloads": -1,
            "filename": "airium-0.2.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9c0e7ac18bfc5f0506d98f55ffa9b547",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13313,
            "upload_time": "2023-09-17T20:12:43",
            "upload_time_iso_8601": "2023-09-17T20:12:43.423493Z",
            "url": "https://files.pythonhosted.org/packages/cb/bc/d174ebf44e4cf8cd967bca5e181e254bd2f95afbb5367471382713736bf5/airium-0.2.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c398f843cd8969409e913b0535ae15771f86d35aed87484372de6fa4e48b283f",
                "md5": "44ab3ddfde222b08107e8515e1312d31",
                "sha256": "ccab36b798b6cce3d0c5074e52ce8059f6e82991caae4985f42cadfad80b1de4"
            },
            "downloads": -1,
            "filename": "airium-0.2.6.tar.gz",
            "has_sig": false,
            "md5_digest": "44ab3ddfde222b08107e8515e1312d31",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 19496,
            "upload_time": "2023-09-17T20:12:45",
            "upload_time_iso_8601": "2023-09-17T20:12:45.238168Z",
            "url": "https://files.pythonhosted.org/packages/c3/98/f843cd8969409e913b0535ae15771f86d35aed87484372de6fa4e48b283f/airium-0.2.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-17 20:12:45",
    "github": false,
    "gitlab": true,
    "bitbucket": false,
    "codeberg": false,
    "gitlab_user": "kamichal",
    "gitlab_project": "airium",
    "lcname": "airium"
}
        
Elapsed time: 0.11652s