pg.Html

Accessible via pg.Html, pg.views.Html.

class Html(*content, style_files=None, styles=None, script_files=None, scripts=None)[source]

Bases: Content

HTML with consolidated CSS and Scripts.

Example:

.. code-block:: python
def foo() -> pg.Html:

s = pg.Html() s.add_style(‘div.foo { color: red; }’) s.add_script(‘function myFoo() { console.log(“foo”);}’) s.write(‘<div class=”foo”>Foo</div>’) return s

def bar() -> pg.Html:

s = pg.Html() s.add_style(‘div.bar { color: green; }’) s.add_script(‘function myBar() { console.log(“bar”);}’) s.write(‘<div class=”bar”>’) s.write(foo()) s.write(‘</div>’) return s

html = bar.html_str()

This will output:

<html>
<head>
<style>
div.bar { color: green; }
div.foo { color: red; }
</style>
<script>
function myBar() { console.log("bar");}
function myFoo() { console.log("foo");}
</script>
</head>
<body><div class="bar"><div class="foo">Foo</div></div></body></html>

Classes:

ScriptFiles(*parts)

Shared script files to link to in the HEAD section.

Scripts(*parts)

Shared script definitions in the HEAD section.

StyleFiles(*parts)

Shared style files to link to in the HEAD section.

Styles(*parts)

Shared style definitions in the HEAD section.

Methods:

add_script(*js)

Adds JavaScript scripts to the HTML.

add_script_file(*url)

Adds a script file to the HTML.

add_style(*css)

Adds CSS styles to the HTML.

add_style_file(*url)

Adds a style file to the HTML.

concate(nestable_str[, separator, dedup])

Concates the string nodes in a nestable object.

element(tag[, inner_html, options, ...])

Creates an HTML element.

escape(s[, javascript_str])

Escapes an HTML writable object.

from_value(value[, copy])

Returns a Content object or None from a writable type.

style_str(style)

Gets a string representing an inline CSS style.

to_str(*[, content_only])

Returns the HTML str.

Attributes:

body_section

Returns the body section.

head_section

Returns the head section.

script_files

Returns the script files to link to.

script_section

Returns the script section.

scripts

Returns the scripts to include in the HTML.

style_files

Returns the style files to link to.

style_section

Returns the style section.

styles

Returns the styles to include in the HTML.

class ScriptFiles(*parts)[source]

Bases: SharedParts

Shared script files to link to in the HEAD section.

Attributes:

content

Returns the content string representing the the shared parts.

property content: str

Returns the content string representing the the shared parts.

class Scripts(*parts)[source]

Bases: SharedParts

Shared script definitions in the HEAD section.

Attributes:

content

Returns the content string representing the the shared parts.

property content: str

Returns the content string representing the the shared parts.

class StyleFiles(*parts)[source]

Bases: SharedParts

Shared style files to link to in the HEAD section.

Attributes:

content

Returns the content string representing the the shared parts.

property content: str

Returns the content string representing the the shared parts.

class Styles(*parts)[source]

Bases: SharedParts

Shared style definitions in the HEAD section.

Attributes:

content

Returns the content string representing the the shared parts.

property content: str

Returns the content string representing the the shared parts.

add_script(*js)[source]

Adds JavaScript scripts to the HTML.

Return type:

pg.Html

add_script_file(*url)[source]

Adds a script file to the HTML.

Return type:

pg.Html

add_style(*css)[source]

Adds CSS styles to the HTML.

Return type:

pg.Html

add_style_file(*url)[source]

Adds a style file to the HTML.

Return type:

pg.Html

property body_section: str[source]

Returns the body section.

classmethod concate(nestable_str, separator=' ', dedup=True)[source]

Concates the string nodes in a nestable object.

Return type:

Optional[str]

classmethod element(tag, inner_html=None, *, options=None, css_classes=None, styles=None, **properties)[source]

Creates an HTML element.

Return type:

pg.Html

Parameters:
  • tag – The HTML tag name.

  • inner_html – The inner HTML of the element.

  • options – Positional options that will be added to the element. E.g. ‘open’ for <details open>.

  • css_classes – The CSS class name or a list of CSS class names.

  • styles – A single CSS style string or a dictionary of CSS properties.

  • **properties – Keyword arguments for HTML properties. For properties with underscore in the name, the underscore will be replaced by dash in the generated HTML. E.g. background_color will be converted to background-color.

Returns:

The opening tag of an HTML element.

classmethod escape(s, javascript_str=False)[source]

Escapes an HTML writable object.

Return type:

Any

classmethod from_value(value, copy=False)[source]

Returns a Content object or None from a writable type.

Return type:

Optional[pg.Html]

property head_section: str[source]

Returns the head section.

property script_files: ScriptFiles[source]

Returns the script files to link to.

property script_section: str[source]

Returns the script section.

property scripts: Scripts[source]

Returns the scripts to include in the HTML.

property style_files: StyleFiles[source]

Returns the style files to link to.

property style_section: str[source]

Returns the style section.

classmethod style_str(style)[source]

Gets a string representing an inline CSS style.

Return type:

Optional[str]

Parameters:

style – A single CSS style string, or a dictionary for CSS properties. When dictionary form is used, underscore in the key name will be replaced by dash in the generated CSS style string. For example, background_color will be converted to background-color.

Returns:

A CSS style string or None if no CSS property is provided.

property styles: Styles[source]

Returns the styles to include in the HTML.

to_str(*, content_only=False, **kwargs)[source]

Returns the HTML str.

Return type:

str

Parameters:
  • content_only – If True, only the content will be returned.

  • **kwargs – Additional keyword arguments passed from the user that will be ignored.

Returns:

The generated HTML str.