Skip to content

pywry.templates

HTML template assembly functions for building complete documents.


Document Structure

pywry.templates.build_html

build_html(content: HtmlContent, config: WindowConfig, window_label: str, settings: PyWrySettings | None = None, loader: AssetLoader | None = None, enable_hot_reload: bool = False, toolbars: Sequence[Toolbar | dict[str, Any]] | None = None, modals: Sequence[Modal | dict[str, Any]] | None = None) -> str

Build the complete HTML document for a PyWry window.

PARAMETER DESCRIPTION
content

The HTML content to display.

TYPE: HtmlContent

config

The window configuration.

TYPE: WindowConfig

window_label

The label for this window.

TYPE: str

settings

PyWry settings for CSP, theme, etc.

TYPE: PyWrySettings or None DEFAULT: None

loader

Asset loader for custom CSS/JS files.

TYPE: AssetLoader or None DEFAULT: None

enable_hot_reload

Whether to include hot reload JavaScript.

TYPE: bool DEFAULT: False

toolbars

List of toolbar configurations. Each can be a Toolbar model or dict with: - position: "top", "bottom", "left", "right", "inside" - items: list of toolbar item configurations (Button, Select, etc.)

TYPE: list[Toolbar | dict] or None DEFAULT: None

modals

List of modal configurations. Each can be a Modal model or dict with: - title: Modal header title - items: list of input item configurations (Button, TextInput, etc.) - size: "sm", "md", "lg", "xl", "full"

TYPE: list[Modal | dict] or None DEFAULT: None

RETURNS DESCRIPTION
str

The complete HTML document as a string.

pywry.templates.build_csp_meta

build_csp_meta(settings: SecuritySettings | None = None) -> str

Build the Content Security Policy meta tag.

PARAMETER DESCRIPTION
settings

Security settings with CSP directives. Uses defaults if not provided.

TYPE: SecuritySettings or None DEFAULT: None

RETURNS DESCRIPTION
str

The CSP meta tag HTML string.

pywry.templates.build_theme_class

build_theme_class(theme: ThemeMode) -> str

Get the HTML class for the current theme.

PARAMETER DESCRIPTION
theme

The theme mode.

TYPE: ThemeMode

RETURNS DESCRIPTION
str

The CSS class name.

pywry.templates.build_base_styles

build_base_styles(settings: PyWrySettings | None = None) -> str

Build base CSS styles for the window from bundled pywry.css.

If a custom CSS file is specified in ThemeSettings, it will be loaded and appended after the base pywry.css styles.

PARAMETER DESCRIPTION
settings

Optional settings containing theme configuration with custom CSS file.

TYPE: PyWrySettings or None DEFAULT: None

RETURNS DESCRIPTION
str

The CSS styles wrapped in style tags.


Data & Script Injection

pywry.templates.build_json_data_script

build_json_data_script(json_data: dict[str, Any] | None) -> str

Build the script tag for injecting JSON data.

PARAMETER DESCRIPTION
json_data

The JSON data to inject.

TYPE: dict[str, Any] or None

RETURNS DESCRIPTION
str

The script tag HTML string.

pywry.templates.build_plotly_init_script

build_plotly_init_script(figure: dict[str, Any], chart_id: str | None = None, theme: ThemeMode = DARK) -> str

Build the Plotly initialization script and container.

Features: - Creates div with unique ID - Injects Plotly.newPlot call - Handles theme templating (dark/light) - Registers chart with PyWry (window.registerPyWryChart) - Sets up resize handler

PARAMETER DESCRIPTION
figure

The Plotly figure dictionary (data, layout, config).

TYPE: dict

chart_id

The unique chart ID. If None, one will be generated.

TYPE: str DEFAULT: None

theme

The window theme.

TYPE: ThemeMode DEFAULT: DARK

RETURNS DESCRIPTION
str

The HTML string containing the container div and initialization script.

pywry.templates.build_plotly_script

build_plotly_script(config: WindowConfig) -> str

Build the Plotly.js library injection with templates.

PARAMETER DESCRIPTION
config

The window configuration.

TYPE: WindowConfig

RETURNS DESCRIPTION
str

The script tags with Plotly.js content and templates.

pywry.templates.build_aggrid_script

build_aggrid_script(config: WindowConfig) -> str

Build the AG Grid library injection.

PARAMETER DESCRIPTION
config

The window configuration.

TYPE: WindowConfig

RETURNS DESCRIPTION
str

The script and style tags for AG Grid.

pywry.templates.build_content_update_script

build_content_update_script(html_content: str) -> str

Build a script to update window content without full reload.

PARAMETER DESCRIPTION
html_content

The new HTML content.

TYPE: str

RETURNS DESCRIPTION
str

JavaScript code to update the document content.


Custom CSS & Scripts

pywry.templates.build_custom_css

build_custom_css(content: HtmlContent, loader: AssetLoader | None = None) -> str

Build custom CSS from files and inline content.

PARAMETER DESCRIPTION
content

HTML content with CSS file references.

TYPE: HtmlContent

loader

Asset loader for reading files.

TYPE: AssetLoader or None DEFAULT: None

RETURNS DESCRIPTION
str

HTML style tags for custom CSS.

pywry.templates.build_custom_scripts

build_custom_scripts(content: HtmlContent, loader: AssetLoader | None = None) -> str

Build custom JavaScript from files.

PARAMETER DESCRIPTION
content

HTML content with script file references.

TYPE: HtmlContent

loader

Asset loader for reading files.

TYPE: AssetLoader or None DEFAULT: None

RETURNS DESCRIPTION
str

HTML script tags for custom scripts.

pywry.templates.build_global_css

build_global_css(settings: AssetSettings | None = None, loader: AssetLoader | None = None) -> str

Build global CSS from AssetSettings.css_files.

PARAMETER DESCRIPTION
settings

Asset settings with CSS file references.

TYPE: AssetSettings or None DEFAULT: None

loader

Asset loader for reading files.

TYPE: AssetLoader or None DEFAULT: None

RETURNS DESCRIPTION
str

HTML style tags for global CSS.

pywry.templates.build_global_scripts

build_global_scripts(settings: AssetSettings | None = None, loader: AssetLoader | None = None) -> str

Build global JavaScript from AssetSettings.script_files.

PARAMETER DESCRIPTION
settings

Asset settings with script file references.

TYPE: AssetSettings or None DEFAULT: None

loader

Asset loader for reading files.

TYPE: AssetLoader or None DEFAULT: None

RETURNS DESCRIPTION
str

HTML script tags for global scripts.


Theme Fixups

pywry.templates.fix_aggrid_theme_classes

fix_aggrid_theme_classes(content: str, theme: ThemeMode) -> str

Fix AG Grid theme classes in HTML to match the window theme.

This ensures that AG Grid theme classes (ag-theme-*) always match the window's dark/light mode. Users cannot accidentally have a dark window with a light grid or vice versa.

PARAMETER DESCRIPTION
content

The HTML content.

TYPE: str

theme

The window theme mode.

TYPE: ThemeMode

RETURNS DESCRIPTION
str

HTML with corrected AG Grid theme classes.

pywry.templates.fix_plotly_template

fix_plotly_template(content: str, theme: ThemeMode) -> str

Fix Plotly template references in HTML to match the window theme.

This ensures that Plotly templates always match the window's dark/light mode.

PARAMETER DESCRIPTION
content

The HTML content.

TYPE: str

theme

The window theme mode.

TYPE: ThemeMode

RETURNS DESCRIPTION
str

HTML with corrected Plotly template references.