htexpr package

Submodules

htexpr.exceptions module

exception htexpr.exceptions.HtexprError

Bases: Exception

htexpr.htexpr module

Parse HTML with embedded Python expressions into Python code objects

class htexpr.htexpr.Htexpr(html, *, map_tag=None, map_attribute=None)

Bases: object

A code object that can be evaluated to effect a sequence of function calls.

code
eval(bindings={})

Evaluate the code object with the given bindings.

The bindings should include any global variables such as imports of dash.html. A more convenient method that captures these automatically is run().

Example:

from dash import html
htexpr.compile(
    "<div>[(<span>{i}</span>) for i in range(10) if i not in removed]</div>"
).eval({**globals(), "removed": {1, 2, 3}})
run(**bindings)

Evaluate the code object with the given bindings added to globals and locals.

The globals are obtained using sys._getframe(), which is intended “for internal and specialized purposes only”. A cleaner method that avoids this kind of magic is eval().

Example:

from dash import html as html
htexpr.compile(
    "<div>[(<span>{i}</span>) for i in range(10) if i not in removed]</div>"
).run(removed={1, 2, 3})
htexpr.htexpr.compile

Compile the html string into an Htexpr object.

Parameters:
  • map_tag – tuple of callables or mappings that return for tag names the corresponding pair (module name, function name); the default is htexpr.mappings.default which allows writing html tags in any case but does not convert the case of dcc or dash_table tags. For backward compatibility, a single callable is allowed.
  • map_attribute – mapping from attribute names to function parameters; attributes not found in the mapping are passed as-is. The default (mappings.default_attributes) maps class to className and some lower-case attributes to camel case, such as rowspan to rowSpan.
Returns:

the compiled code

Return type:

Htexpr

htexpr.htexpr.parse(html)
htexpr.htexpr.simplify(tree)
htexpr.htexpr.to_ast(tree, map_tag=None, map_attribute=None)
htexpr.htexpr.wrap_ast(body)

htexpr.mappings module

Functions or mappings for use with htexpr.compile().

default is a suitable value for the map_tag argument of compile(); it assumes that

  • dash.html is imported as html
  • dash.dcc is imported as dcc
  • dash.table is imported as dash_table

and allows you to type html tags in any case but requires title case for the others.

dbc_and_default is similar, but adds in the front dash_bootstrap_components as dbc. To type html tags shadowed by dbc tags, use non-title case.

If you import the modules under other names, you can instantiate the mapping functions with those, e.g.:

other_names = [html('dash_html_components'), dcc('dash_core_components')]

default_attributes is the default value for the map_attribute argument of compile()

htexpr.mappings.datatable

Mapping for DataTable.

htexpr.mappings.dbc

Mapping for dash bootstrap components.

The default assumes that dash_bootstrap_components is imported as dbc, which can be changed by setting module.

htexpr.mappings.dcc

Mapping for dash core components.

htexpr.mappings.html

Mapping for html tags.

Maps tags to title case, so you can type html tags as <audio> or <AuDiO> or whichever way.

Module contents