Skip to main content

Style an HTML widget for both themes

An HTML widget shows the document that its endpoint returns. The document is in a frame, and the frame gets nothing from the app. Thus the document cannot see the theme of the app.

FINOS Workspace sends the theme to the endpoint in the query parameter theme. The value is dark or light. The endpoint returns the styles for that theme.

1. Add the widget to widgets.json​

Do not add a parameter with the name theme. The name is reserved. FINOS Workspace adds the parameter to each request of an HTML widget.

widgets.json
{
"report": {
"name": "Report",
"description": "A report that follows the theme",
"type": "html",
"endpoint": "report"
}
}

2. Choose the colors from the theme​

In the endpoint, read theme and choose the colors for that theme:

main.py
from fastapi import FastAPI, Query
from fastapi.responses import HTMLResponse

app = FastAPI()

PALETTES = {
"dark": {"background": "#0b2432", "text": "#e7f2f8", "border": "#355867", "accent": "#00b5e2"},
"light": {"background": "#ffffff", "text": "#1a3248", "border": "#b7cdd8", "accent": "#005e8a"},
}


@app.get("/report", response_class=HTMLResponse)
def report(theme: str = Query("dark")) -> str:
colors = PALETTES.get(theme, PALETTES["dark"])
return f"""<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
body {{
margin: 0;
padding: 16px;
background: {colors["background"]};
color: {colors["text"]};
font: 14px/1.5 system-ui, sans-serif;
}}
table {{ border-collapse: collapse; width: 100%; }}
th, td {{ border: 1px solid {colors["border"]}; padding: 4px 8px; text-align: left; }}
a {{ color: {colors["accent"]}; }}
</style>
</head>
<body>
<table><tr><th>Quarter</th><th>Revenue</th></tr><tr><td>Q1</td><td>120</td></tr></table>
</body>
</html>"""

3. Use the colors of the app​

Use the values of the app for the colors that must agree with the app. Semantic properties gives the values for the two themes.

  • In the example, background, text and border are the values of --general-bg-primary, --foreground and --border.
  • The accent colors give good contrast on these backgrounds.
  • The link color of the app, --link-color, comes from the brand color.

Limits​

  • The document cannot read the parameter theme itself, because the document comes through srcdoc.
  • The document cannot read the custom properties of the app.

HTML documents tells what the frame adds and what it does not add. The widget types page shows a complete HTML widget of the reference backend.

What follows from the frame​

  • The document draws its own background, text and fonts. The app adds no background color and no font to it.
  • Nothing in the frame can read --btn-primary-bg or any other property of the app. To match the app, use the values on Color palette and Semantic properties in the document's own CSS.
  • The theme reaches an HTML widget or an iframe widget only as the theme query parameter. An HTML widget's document arrives through srcdoc, which has no query string, so the backend that serves it chooses the light or the dark styles. An iframe widget's page receives the parameter in its URL.
  • In an artifact, styles must be inline in the document: style-src allows inline styles only, img-src and font-src allow data: URLs only, and nothing loads from the network.
  • theme is a reserved query parameter name for markdown, html, iframe and plugin widget types (see the widgets.json schema).

HTML widgets and artifacts lists what the app does to each frame and document.