API Documentation

DashML’s autogenerated API documentation.

Submodules

dashml.builder module

Builder module.

This is the class behind the main _ object exported by DashML. The builder is basically a few utilities wrapped around an lxml element factory, for ergonomic handling of HTML-specific quirks.

class dashml.builder.Builder

Bases: object

DashML Markup Builder.

Allows the creation of lxml.html elements via overriding __getattr__.

>>> from dashml import render
>>> from dashml.builder import Builder
>>> _ = Builder()
>>> render(_.p("Hello world!"))
'<p>Hello world!</p>'

Under the hood, __getattr__ curries the build method to create an element of the tag type you access. This is just syntatic sugar, and you can use build directly:

>>> from dashml import render
>>> from dashml.builder import Builder
>>> _ = Builder()
>>> render(_.build("p", "Hello world!"))
'<p>Hello world!</p>'
build(tag_name: str, *children, **props) → lxml.etree._Element

Build a DashML element.

Children passed in can be Elements or None natively. Nonetype objects will be filtered out from the final document (for easy conditional rendering). Non-string types will be cast to a string by calling their __str__() method.

Properties can be strings, booleans, or None natively. Booleans and Nontypes will function like HTML5 boolean attributes- True will leave the property in the document; False and None will leave no value present.

>>> from dashml import render
>>> from dashml.builder import Builder
>>> _ = Builder()
>>> render(_.build("input", type="checkbox", checked=True))
'<input type="checkbox" checked>'
Parameters:
  • tag_name (str) – The name of the HTML tag to build.
  • children (Child) – A list of strings, Elements, or None
Keyword Arguments:
 

properties (Prop) – A list of HTML5 properties.

Returns:

(Element) A DashML/lxml Element.

dashml.html module

Functions for exporting DashML to (or importing from) raw HTML.

The most notable function here is render(), which takes a DashML element as an argument and returns an HTML representation of it.

>>> from dashml import _, render
>>> render(_.p("Hello world!"))
'<p>Hello world!</p>'

Additionally, the method unsafe_from_string exists here to transform trusted HTML strings into DashML/lxml Element instances. Use this great unsafe power responsibly!

dashml.html.render(ele: lxml.etree._Element) → str

Render an Element to a string.

Parameters:ele (Element) – The element to render.
Returns:(str) Rendered utf-8 string of the element.
dashml.html.unsafe_from_string(unsafe_string: str) → lxml.etree._Element

UNSAFE: Create an element from a string, skipping escaping.

HTML will _not_ be escaped by this function, so using it with an untrusted string is a security vulnerability and could allow XSS attacks!

Parameters:unsafe_string (str) – A string to convert to an element.
Returns:(Element) The converted element

dashml.types module

Types defined for DashML.

dashml.types.Element

alias of lxml.etree._Element

Module contents

DashML - Create HTML with composable Python functions.

Usage:

>>> from dashml import render, _
>>> render(_.p("Hello world!"))
'<p>Hello world!</p>'
dashml.render(ele: lxml.etree._Element) → str

Render an Element to a string.

Parameters:ele (Element) – The element to render.
Returns:(str) Rendered utf-8 string of the element.
dashml.unsafe_from_string(unsafe_string: str) → lxml.etree._Element

UNSAFE: Create an element from a string, skipping escaping.

HTML will _not_ be escaped by this function, so using it with an untrusted string is a security vulnerability and could allow XSS attacks!

Parameters:unsafe_string (str) – A string to convert to an element.
Returns:(Element) The converted element