Document Rendering Module¶
WebEngine-based HTML document rendering system with theme support and automatic widget updates.
Quick Start¶
# At the very start of main.py (BEFORE QApplication)
from src.shared_services.rendering.documents.api import configure_chromium_flags
configure_chromium_flags()
# After QApplication is created
from src.shared_services.rendering.documents.api import (
configure_default_profile,
create_html_view,
)
# Configure profile once
configure_default_profile()
# Create views as needed
view = create_html_view(parent=self)
view.set_html("<h1>Willkommen</h1><p>Some content here.</p>")
When to Use What¶
| Use Case | What to Use |
|---|---|
| Theme-aware HTML display | create_html_view() |
| External/unthemed content | create_html_view_minimal() |
| Render with custom CSS | view.set_html(html, custom_css="...") |
| Raw HTML (no theme) | view.set_html_raw(html) |
| Reduce CPU when hidden | view.freeze() / view.unfreeze() |
HtmlView Widget¶
Theme-aware WebEngine view for displaying styled HTML content.
from src.shared_services.rendering.documents.api import create_html_view
# Basic usage
view = create_html_view(parent=self, object_name="NewsView")
view.set_html("<h1>Titel</h1><p>Absatz mit Text.</p>")
# With custom CSS
view.set_html(
"<table><tr><td>Zelle</td></tr></table>",
custom_css="table { margin: 20px; }"
)
# Raw HTML without theme styles
view.set_html_raw(complete_html_document)
# Lifecycle management
view.freeze() # Reduce CPU when hidden
view.unfreeze() # Restore when visible
Theme Integration¶
Views automatically update when the application theme changes.
# Register for automatic theme updates (default)
view.set_html(html, register_for_theme=True)
# Skip theme registration (for self-styled content)
view.set_html(html, register_for_theme=False)
# Manual theme control
from src.shared_services.rendering.documents.api import set_theme, get_theme
set_theme("dark")
current = get_theme() # "light" or "dark"
Profile Configuration¶
WebEngine profile settings for desktop app usage.
from src.shared_services.rendering.documents.api import (
configure_chromium_flags,
configure_default_profile,
)
# MUST be called before QApplication
configure_chromium_flags()
# Call once after QApplication
configure_default_profile()
Profile optimizations: - No persistent cookies - No HTTP cache - Spellcheck disabled - Chromium flags for reduced memory usage
View Settings¶
Fine-tune WebEngine settings for specific use cases.
from src.shared_services.rendering.documents.api import (
apply_base_optimizations,
apply_full_optimizations,
enable_javascript,
enable_scrollbars,
)
# Base optimizations (safe for all content)
apply_base_optimizations(view)
# Full optimizations (for simple, known content)
apply_full_optimizations(view)
# Toggle features
enable_javascript(view, enabled=False)
enable_scrollbars(view, enabled=True)
Fun Facts¶
Daily rotating facts injected at placeholder locations.
from src.shared_services.rendering.documents.fun_facts import inject_fun_fact
# HTML with placeholder
html = """
<h1>Welcome</h1>
<p>{{FUN_FACT}}</p>
"""
html = inject_fun_fact(html)
# {{FUN_FACT}} replaced with "Honey never spoils..."
Files¶
| File | Purpose |
|---|---|
api.py |
Public API exports |
html_view.py |
HtmlView and HtmlViewMinimal widgets |
profile_config.py |
Chromium flags and profile settings |
view_settings.py |
WebEngine optimization settings |
web_view_colors.py |
Theme-aware CSS generation |
web_view_registry.py |
Widget tracking for theme updates |
fun_facts.py |
Daily rotating facts |
constants/alert_types.py |
Alert type definitions |
constants/document_styles.py |
Typography constants |
Architecture¶
Application Startup
|
v
configure_chromium_flags() <-- BEFORE QApplication
|
v
QApplication created
|
v
configure_default_profile() <-- AFTER QApplication
|
v
create_html_view()
|
+---> ProfileManager.ensure_configured()
+---> HtmlView.__init__()
| |
| +--> apply_base_optimizations()
|
v
view.set_html(html, custom_css)
|
+---> WebViewRegistry.register()
| |
| +--> Store weak ref + content
| +--> Register theme callback (once)
|
+---> build_themed_html()
| |
| +--> generate_theme_stylesheet()
| +--> Inject CSS into HTML
|
+---> QWebEngineView.setHtml()
|
v
Rendered content
Theme Change Event
|
v
StylesheetManager.on_theme_change()
|
v
WebViewRegistry._on_theme_change()
|
+---> set_theme()
+---> refresh_all() -> Re-render all views
Performance Notes¶
- First WebEngine view takes ~600-800ms to initialize (Chromium warm-up)
- Subsequent views initialize in ~20-50ms
- Use
freeze()/unfreeze()for views that are temporarily hidden - Base RAM usage: ~80-120MB for first view, ~20-40MB per additional view
API Reference¶
src.shared_services.rendering.documents.api
¶
WebEngine public API for themed HTML display.
This module provides the public interface for creating optimized QWebEngineView widgets with automatic theme support.
IMPORTANT: Before using any WebEngine functionality, call configure_chromium_flags() at the very start of your application, BEFORE creating QApplication or importing other Qt modules.
USAGE::
# At the very start of main.py (before QApplication)
from src.shared_services.rendering.documents.web_engine.api import (
configure_chromium_flags,
)
configure_chromium_flags()
# Later, after QApplication is created
from src.shared_services.rendering.documents.web_engine.api import (
configure_default_profile,
create_html_view,
)
# Configure profile once
configure_default_profile()
# Create views as needed
view = create_html_view(parent=self)
view.set_html("<h1>Willkommen</h1>")
HtmlView
¶
Bases: QWebEngineView
Optimized WebEngine view for displaying themed HTML content.
This widget provides: - Automatic theme integration with the application style system - Optimized settings for local HTML display - Lifecycle management for resource efficiency - Simple API for setting HTML content
The view automatically updates when the application theme changes, re-rendering the content with appropriate colors.
Example::
view = HtmlView(parent=self)
view.set_html("<h1>Willkommen</h1><p>Dies ist ein Test.</p>")
# Or with custom CSS
view.set_html(
"<div class='card'>Inhalt</div>",
custom_css=".card { padding: 20px; border-radius: 8px; }"
)
Source code in src\shared_services\rendering\documents\html_view.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
__init__(parent=None, object_name='HtmlView', include_base_styles=True)
¶
Initialize the HTML view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
Optional[QWidget]
|
Parent widget. |
None
|
object_name
|
str
|
Object name for styling and identification. |
'HtmlView'
|
include_base_styles
|
bool
|
Whether to include base theme styles. |
True
|
Source code in src\shared_services\rendering\documents\html_view.py
closeEvent(event)
¶
freeze()
¶
Freeze the view to reduce CPU usage when hidden.
Call this when the view is not visible to save resources. The view will still display its last content but won't process JavaScript or animations.
Source code in src\shared_services\rendering\documents\html_view.py
get_html_content()
¶
Get the current HTML content (without theme wrapper).
Returns:
| Type | Description |
|---|---|
str
|
The HTML content that was set. |
hideEvent(event)
¶
refresh()
¶
Refresh the view with current content and theme.
Call this after modifying content dynamically.
Source code in src\shared_services\rendering\documents\html_view.py
setHtml(html, baseUrl=None)
¶
Override setHtml to sync background color before rendering.
This ensures the page background matches the theme, reducing flicker during resize operations.
Source code in src\shared_services\rendering\documents\html_view.py
set_custom_css(css)
¶
Set or update custom CSS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
css
|
str
|
Custom CSS rules. |
required |
set_html(html, custom_css=None, register_for_theme=True)
¶
Set the HTML content to display.
The content is automatically wrapped with theme-appropriate styles and will update when the application theme changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
HTML content (can be fragment or complete document). |
required |
custom_css
|
Optional[str]
|
Optional custom CSS to include. |
None
|
register_for_theme
|
bool
|
Whether to register for automatic theme updates. |
True
|
Example::
view.set_html("<h1>Titel</h1><p>Absatz mit Text.</p>")
# With custom styles
view.set_html(
"<table><tr><td>Zelle</td></tr></table>",
custom_css="table { margin: 20px; }"
)
Source code in src\shared_services\rendering\documents\html_view.py
set_html_raw(html)
¶
Set raw HTML without theme styles or registration.
Use this for content that manages its own styling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
Complete HTML document. |
required |
Source code in src\shared_services\rendering\documents\html_view.py
showEvent(event)
¶
Handle show event - unfreeze if needed.
Source code in src\shared_services\rendering\documents\html_view.py
unfreeze()
¶
Unfreeze the view when it becomes visible again.
Restores normal operation after a freeze() call.
Source code in src\shared_services\rendering\documents\html_view.py
HtmlViewMinimal
¶
Bases: QWebEngineView
Minimal WebEngine view without theme integration.
Use this for cases where you want full control over styling or when displaying external content that should not be themed.
This view applies performance optimizations but does not inject theme styles or register for theme updates.
Source code in src\shared_services\rendering\documents\html_view.py
__init__(parent=None, object_name='HtmlViewMinimal')
¶
Initialize the minimal HTML view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
Optional[QWidget]
|
Parent widget. |
None
|
object_name
|
str
|
Object name for identification. |
'HtmlViewMinimal'
|
Source code in src\shared_services\rendering\documents\html_view.py
set_background_color(color)
¶
Set the page background color.
Call this to reduce flicker during resize by matching the Chromium page background to your content color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
str
|
Hex color string (e.g., "#ffffff"). |
required |
Source code in src\shared_services\rendering\documents\html_view.py
ProfileManager
¶
Singleton manager for WebEngine profile configuration.
Ensures profile is configured exactly once and provides access to the configured profile.
Source code in src\shared_services\rendering\documents\profile_config.py
ensure_configured()
classmethod
¶
Ensure the default profile is configured.
Safe to call multiple times - configuration only happens once.
Source code in src\shared_services\rendering\documents\profile_config.py
get_profile()
classmethod
¶
Get the configured default profile.
Ensures configuration before returning.
Returns:
| Type | Description |
|---|---|
QWebEngineProfile
|
The configured QWebEngineProfile. |
Source code in src\shared_services\rendering\documents\profile_config.py
instance()
classmethod
¶
WebViewRegistry
¶
Registry for tracking WebEngine views for theme updates.
Uses weak references to avoid memory leaks. When a view widget is garbage collected, it is automatically removed from the registry.
The registry registers a callback with StylesheetManager to be notified when the theme changes, triggering re-render of all registered views.
Source code in src\shared_services\rendering\documents\web_view_registry.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
instance()
classmethod
¶
refresh_all()
¶
Refresh all registered views with current theme.
Called automatically when theme changes.
Source code in src\shared_services\rendering\documents\web_view_registry.py
refresh_view(view)
¶
Refresh a specific view with current theme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The QWebEngineView to refresh. |
required |
Source code in src\shared_services\rendering\documents\web_view_registry.py
register(view, html_content, include_base_styles=True, custom_css=None)
¶
Register a WebEngine view for automatic theme updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The QWebEngineView widget to track. |
required |
html_content
|
str
|
The HTML source content. |
required |
include_base_styles
|
bool
|
Whether to include base theme styles. |
True
|
custom_css
|
Optional[str]
|
Optional custom CSS to include. |
None
|
Source code in src\shared_services\rendering\documents\web_view_registry.py
unregister(view)
¶
Unregister a view from theme updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The QWebEngineView widget to stop tracking. |
required |
Source code in src\shared_services\rendering\documents\web_view_registry.py
update_content(view, html_content, include_base_styles=None, custom_css=None)
¶
Update the content of a registered view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The registered QWebEngineView. |
required |
html_content
|
str
|
New HTML source content. |
required |
include_base_styles
|
Optional[bool]
|
Update style inclusion (or keep existing). |
None
|
custom_css
|
Optional[str]
|
Update custom CSS (or keep existing). |
None
|
Source code in src\shared_services\rendering\documents\web_view_registry.py
apply_base_optimizations(view)
¶
Apply base optimizations to a WebEngineView.
These settings disable browser features not needed for local HTML display without any functionality loss for typical desktop app use cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The QWebEngineView to configure. |
required |
Source code in src\shared_services\rendering\documents\view_settings.py
apply_full_optimizations(view)
¶
Apply full optimizations including features that might affect some content.
Use this for views that display simple, known content where these features are confirmed unnecessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The QWebEngineView to configure. |
required |
Source code in src\shared_services\rendering\documents\view_settings.py
build_themed_html(content, include_base_styles=True, custom_css=None, theme=None)
¶
Build complete HTML document with theme styles.
If the content already contains or
tags, styles are injected into the existing structure. Otherwise, a complete HTML document wrapper is created.Scrollbar styles are always included regardless of include_base_styles to ensure consistent scrollbar appearance across all web views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
HTML content (can be fragment or complete document). |
required |
include_base_styles
|
bool
|
Whether to include base theme styles. |
True
|
custom_css
|
Optional[str]
|
Optional custom CSS to include. |
None
|
theme
|
Optional[str]
|
Theme to use (default: current theme). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Complete HTML document string with theme styles. |
Source code in src\shared_services\rendering\documents\web_view_registry.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
configure_chromium_flags()
¶
Configure Chromium flags for optimized resource usage.
IMPORTANT: This must be called BEFORE creating QApplication or any Qt imports that trigger Qt initialization.
The flags disable unnecessary browser features to reduce memory footprint and background activity.
Example::
# At the very start of main.py, before other imports
from src.shared_services.rendering.documents.web_engine.profile_config import (
configure_chromium_flags,
)
configure_chromium_flags()
# Then proceed with normal imports
from PySide6.QtWidgets import QApplication
Source code in src\shared_services\rendering\documents\profile_config.py
configure_default_profile()
¶
Configure the default WebEngine profile for desktop app usage.
Disables persistence features not needed in a local desktop application context (cookies, spellcheck, cache).
Call this once after QApplication is created, before creating any QWebEngineView instances.
Example::
app = QApplication(sys.argv)
configure_default_profile()
Source code in src\shared_services\rendering\documents\profile_config.py
create_html_view(parent=None, object_name='HtmlView', include_base_styles=True)
¶
Create a theme-aware HTML view widget.
The returned view automatically integrates with the application theme system and updates when the theme changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
Optional[QWidget]
|
Parent widget. |
None
|
object_name
|
str
|
Object name for styling and identification. |
'HtmlView'
|
include_base_styles
|
bool
|
Whether to include base theme styles. |
True
|
Returns:
| Type | Description |
|---|---|
HtmlView
|
Configured HtmlView instance. |
Example::
view = create_html_view(parent=self, object_name="NewsView")
view.set_html("<h1>Neuigkeiten</h1><p>Aktuelle Updates...</p>")
Source code in src\shared_services\rendering\documents\api.py
create_html_view_minimal(parent=None, object_name='HtmlViewMinimal')
¶
Create a minimal HTML view without theme integration.
Use this for content that manages its own styling or external content that should not be themed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
Optional[QWidget]
|
Parent widget. |
None
|
object_name
|
str
|
Object name for identification. |
'HtmlViewMinimal'
|
Returns:
| Type | Description |
|---|---|
HtmlViewMinimal
|
Configured HtmlViewMinimal instance. |
Source code in src\shared_services\rendering\documents\api.py
enable_javascript(view, enabled=True)
¶
Enable or disable JavaScript execution.
JavaScript is enabled by default. Use this to explicitly control JavaScript for specific views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The QWebEngineView to configure. |
required |
enabled
|
bool
|
Whether JavaScript should be enabled. |
True
|
Source code in src\shared_services\rendering\documents\view_settings.py
enable_scrollbars(view, enabled=True)
¶
Enable or disable scrollbars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
QWebEngineView
|
The QWebEngineView to configure. |
required |
enabled
|
bool
|
Whether scrollbars should be shown. |
True
|
Source code in src\shared_services\rendering\documents\view_settings.py
generate_base_styles(theme=None)
¶
Generate base CSS styles for HTML content.
Provides sensible defaults for common HTML elements using theme-appropriate colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
Optional[str]
|
Theme to generate for (default: current theme). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
CSS string with base element styles. |
Source code in src\shared_services\rendering\documents\web_view_colors.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
generate_css_variables(theme=None)
¶
Generate CSS custom properties (variables) for the theme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
Optional[str]
|
Theme to generate for (default: current theme). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
CSS string with :root variables. |
Source code in src\shared_services\rendering\documents\web_view_colors.py
generate_theme_stylesheet(theme=None)
¶
Generate complete theme stylesheet combining variables and base styles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
Optional[str]
|
Theme to generate for (default: current theme). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Complete CSS string ready for injection. |
Source code in src\shared_services\rendering\documents\web_view_colors.py
get_color(key, theme=None)
¶
Get a color by ColorSystem key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
ColorSystem palette key. |
required |
theme
|
Optional[str]
|
Optional theme override. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Hex color string. |
Source code in src\shared_services\rendering\documents\web_view_colors.py
get_configured_profile()
¶
Get the configured WebEngine profile.
Convenience function that ensures configuration and returns the default profile.
Returns:
| Type | Description |
|---|---|
QWebEngineProfile
|
The configured QWebEngineProfile. |
Source code in src\shared_services\rendering\documents\profile_config.py
get_theme()
¶
get_web_view_registry()
¶
Get the singleton WebViewRegistry instance.
Returns:
| Type | Description |
|---|---|
WebViewRegistry
|
The shared WebViewRegistry instance. |
refresh_all_views()
¶
Refresh all registered views with current theme.
Call this if you need to force a refresh of all web views. Normally not needed as views update automatically on theme change.
Source code in src\shared_services\rendering\documents\api.py
render_html(view, html, custom_css=None)
¶
Render HTML content to a view with theme styles.
Convenience function that sets HTML and registers for theme updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
HtmlView
|
The HtmlView to render to. |
required |
html
|
str
|
HTML content. |
required |
custom_css
|
Optional[str]
|
Optional custom CSS. |
None
|
Example::
render_html(view, "<p>Inhalt</p>", custom_css="p { margin: 10px; }")
Source code in src\shared_services\rendering\documents\api.py
set_theme(theme)
¶
Set the current theme for web view colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
str
|
Theme name ("light" or "dark"). |
required |