htmltools methods

htmltools.HTMLDependency

HTMLDependency(
    self,
    name,
    version,
    *,
    source=None,
    script=None,
    stylesheet=None,
    all_files=False,
    meta=None,
    head=None,
)

Define an HTML dependency.

Define an HTML dependency (i.e. CSS and/or JavaScript bundled in a directory). HTML dependencies make it possible to use libraries like jQuery, Bootstrap, and d3 in a more composable and portable way than simply using script, link, and style tags.

Parameters

name : str

Library name.

version : str | Version

Library version.

source : Optional[HTMLDependencySource | HTMLDependencyUrl] = None

A specification for the location of dependency files.

script : Optional[ScriptItem | list[ScriptItem]] = None

<script> tags to include in the document’s <head>. Each tag definition should include at least the src attribute (which should be file path relative to the source file location).

stylesheet : Optional[StylesheetItem | list[StylesheetItem]] = None

<link> tags to include in the document’s <head>. Each tag definition should include at least the href attribute (which should be file path relative to the source file location).

all_files : bool = False

Whether all files under the source directory are dependency files. If False, only the files specified in script and stylesheet are treated as dependency files.

meta : Optional[MetaItem | list[MetaItem]] = None

<meta> tags to include in the document’s <head>.

head : TagChild = None

Tags to include in the document’s <head>.

Examples

>>> dep = HTMLDependency(
        name="mypackage",
        version="1.0",
        source={
            "package": "mypackage",
            "subdir": "lib/",
        },
        script={"src": "foo.js"},
        stylesheet={"href": "css/foo.css"},
    )
>>> x = div("Hello", dep)
>>> x.render()

Methods

Name Description
as_dict Returns a dict of the dependency’s attributes.
as_html_tags Render the dependency as a TagList().
copy_to Copy the dependency’s files to the given path.
source_path_map Returns a dict of the absolute ‘source’ filepath and the ‘href’ path it will point to in the HTML (given the lib_prefix).

as_dict

HTMLDependency.as_dict(lib_prefix='lib', include_version=True)

Returns a dict of the dependency's attributes.

as_html_tags

HTMLDependency.as_html_tags(lib_prefix='lib', include_version=True)

Render the dependency as a TagList().

copy_to

HTMLDependency.copy_to(path, include_version=True)

Copy the dependency's files to the given path.

source_path_map

HTMLDependency.source_path_map(lib_prefix='lib', include_version=True)

Returns a dict of the absolute 'source' filepath and the 'href' path it will point to in the HTML (given the lib_prefix).

htmltools.css

css(collapse_='', **kwargs)
CSS string helper

Convenience function for building CSS style declarations (i.e. the string that goes
into a style attribute, or the parts that go inside curly braces in a full
stylesheet).

Parameters

collapse_
    String to use to collapse properties into a single string; likely ``""`` (the
    default) for style attributes, and either ``"

“orNone`` for style blocks. **kwargs Named style properties, where the name is the property name and the argument is the property value.

Returns

:
    A string of CSS style declarations, or ``None`` if no properties were given.

Example

>>> from htmltools import css
>>> css(font_size = "12px", backgroundColor = "red")
'font-size:12px;background-color:red;'

Note

CSS uses '-' (minus) as a separator character in property names, which isn't allowed
in Python's keyword arguments. This function allows you to use '_' (underscore) as a
separator and/or camelCase notation instead.