htmltools methods
htmltools.HTMLDependency
HTMLDependency(self,
name,
version,*,
=None,
source=None,
script=None,
stylesheet=False,
all_files=None,
meta=None,
head )
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 thesrc
attribute (which should be file path relative to thesource
file location). stylesheet : Optional[
StylesheetItem
|list
[StylesheetItem
]] = None-
<link>
tags to include in the document’s<head>
. Each tag definition should include at least thehref
attribute (which should be file path relative to thesource
file location). all_files : bool = False
-
Whether all files under the
source
directory are dependency files. IfFalse
, 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(
="mypackage",
name="1.0",
version={
source"package": "mypackage",
"subdir": "lib/",
},={"src": "foo.js"},
script={"href": "css/foo.css"},
stylesheet )
>>> 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
='lib', include_version=True) HTMLDependency.as_dict(lib_prefix
Returns a dict of the dependency's attributes.
copy_to
=True) HTMLDependency.copy_to(path, include_version
Copy the dependency's files to the given path.
source_path_map
='lib', include_version=True) HTMLDependency.source_path_map(lib_prefix
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
='', **kwargs) css(collapse_
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 ``"
“or
None`` 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.