Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

htexpr compiles an html string into a Python expression

Project description

htexpr: Templating for Dash

PyPI MIT License CircleCI Github CI Black

htexpr compiles an html-like template syntax into Python expressions, allowing embedded Python expressions in attributes and content. It is inspired by JSX and intended to complement the excellent Dash package, which allows you to write single-page React apps in Python. For motivation and further instructions, see the documentation.

Example

A Unicode table::

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State

from htexpr import compile
import unicodedata

app = dash.Dash()
app.layout = compile("""
<div>
  <table style={"margin": "0 auto"}>
    <tr><th>char</th><th>name</th><th>category</th></tr>
       [
         (<tr style={'background-color': '#eee' if line % 2 else '#ccc'}>
            <td>{ char }</td>
            <td>{ unicodedata.name(char, '???') }</td>
            <td>{ unicodedata.category(char) }</td>
          </tr>)
         for line, char in enumerate(chr(i) for i in range(32, 128))
       ]
  </table>
</div>
""").run()

app.run_server(debug=True)

Further demonstrations:

Development status

I wrote this to help me with a particular project where I kept making bracketing mistakes. The code works for that project, but there are likely to be corner cases I haven't considered.

The Python grammar used here is quite simplistic: it recognizes strings and variously parenthesized expressions. By understanding more Python it would probably be possible to disambiguate between comparison operators and tags, and thus drop the requirement to enclose nested expressions in parentheses.

The error messages are not always helpful, and in particular the code objects don't yet have reliable line-number data.

Project details


Download files

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

Files for htexpr, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size htexpr-0.1.0-py2.py3-none-any.whl (10.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size htexpr-0.1.0.tar.gz (11.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page