pg.Html¶
Accessible via pg.Html, pg.views.Html.
- class Html(*content, style_files=None, styles=None, script_files=None, scripts=None)[source]¶
Bases:
ContentHTML 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:
Returns the body section.
Returns the head section.
Returns the script files to link to.
Returns the script section.
Returns the scripts to include in the HTML.
Returns the style files to link to.
Returns the style section.
Returns the styles to include in the HTML.
- class ScriptFiles(*parts)[source]¶
Bases:
SharedPartsShared script files to link to in the HEAD section.
Attributes:
contentReturns the content string representing the the shared parts.
- class Scripts(*parts)[source]¶
Bases:
SharedPartsShared script definitions in the HEAD section.
Attributes:
contentReturns the content string representing the the shared parts.
- class StyleFiles(*parts)[source]¶
Bases:
SharedPartsShared style files to link to in the HEAD section.
Attributes:
contentReturns the content string representing the the shared parts.
- class Styles(*parts)[source]¶
Bases:
SharedPartsShared style definitions in the HEAD section.
Attributes:
contentReturns the content string representing the the shared parts.
- classmethod concate(nestable_str, separator=' ', dedup=True)[source]¶
Concates the string nodes in a nestable object.
- classmethod element(tag, inner_html=None, *, options=None, css_classes=None, styles=None, **properties)[source]¶
Creates an HTML element.
- Return type:
- 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 from_value(value, copy=False)[source]¶
Returns a Content object or None from a writable type.
- property script_files: ScriptFiles[source]¶
Returns the script files to link to.
- property style_files: StyleFiles[source]¶
Returns the style files to link to.
- classmethod style_str(style)[source]¶
Gets a string representing an inline CSS style.
- Return type:
- 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.