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.

Parameters:

Name Type Description Default
content HtmlContent

The HTML content to display.

required
config WindowConfig

The window configuration.

required
window_label str

The label for this window.

required
settings PyWrySettings or None

PyWry settings for CSP, theme, etc.

None
loader AssetLoader or None

Asset loader for custom CSS/JS files.

None
enable_hot_reload bool

Whether to include hot reload JavaScript.

False
toolbars list[Toolbar | dict] or None

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.)

None
modals list[Modal | dict] or None

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"

None

Returns:

Type 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.

Parameters:

Name Type Description Default
settings SecuritySettings or None

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

None

Returns:

Type 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.

Parameters:

Name Type Description Default
theme ThemeMode

The theme mode.

required

Returns:

Type 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.

Parameters:

Name Type Description Default
settings PyWrySettings or None

Optional settings containing theme configuration with custom CSS file.

None

Returns:

Type 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.

Parameters:

Name Type Description Default
json_data dict[str, Any] or None

The JSON data to inject.

required

Returns:

Type 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

Parameters:

Name Type Description Default
figure dict

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

required
chart_id str

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

None
theme ThemeMode

The window theme.

DARK

Returns:

Type 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.

Parameters:

Name Type Description Default
config WindowConfig

The window configuration.

required

Returns:

Type 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.

Parameters:

Name Type Description Default
config WindowConfig

The window configuration.

required

Returns:

Type 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.

Parameters:

Name Type Description Default
html_content str

The new HTML content.

required

Returns:

Type 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.

Parameters:

Name Type Description Default
content HtmlContent

HTML content with CSS file references.

required
loader AssetLoader or None

Asset loader for reading files.

None

Returns:

Type 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.

Parameters:

Name Type Description Default
content HtmlContent

HTML content with script file references.

required
loader AssetLoader or None

Asset loader for reading files.

None

Returns:

Type 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.

Parameters:

Name Type Description Default
settings AssetSettings or None

Asset settings with CSS file references.

None
loader AssetLoader or None

Asset loader for reading files.

None

Returns:

Type 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.

Parameters:

Name Type Description Default
settings AssetSettings or None

Asset settings with script file references.

None
loader AssetLoader or None

Asset loader for reading files.

None

Returns:

Type 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.

Parameters:

Name Type Description Default
content str

The HTML content.

required
theme ThemeMode

The window theme mode.

required

Returns:

Type 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.

Parameters:

Name Type Description Default
content str

The HTML content.

required
theme ThemeMode

The window theme mode.

required

Returns:

Type Description
str

HTML with corrected Plotly template references.