feat(composite): add show_name parameter to control header visibility.

This commit is contained in:
shay
2026-04-20 15:04:09 -04:00
parent b66f966adb
commit 96dc98142f
7 changed files with 235 additions and 98 deletions
+4 -3
View File
@@ -55,10 +55,11 @@ DATA_SOURCE_TYPE = os.getenv("DATA_SOURCE", "direct")
"""Data source type: 'direct' for immediate API calls, 'queued' for batch processing."""
# Widget settings
WIDGET_ORDER = ["grade", "impact", "streaks", "collaborators", "focus", "languages", "achievements"]
"""Default order of widgets. Customize by reordering this list."""
WIDGET_ORDER = ["name", "grade", "impact", "streaks", "collaborators", "focus", "languages", "achievements"]
"""Default order of widgets. "name" is the avatar+username header and is
always pinned first in the UI — it can be toggled off but not reordered."""
ENABLED_WIDGETS = ["grade", "impact", "streaks", "collaborators", "focus", "languages", "achievements"]
ENABLED_WIDGETS = ["name", "grade", "impact", "streaks", "collaborators", "focus", "languages", "achievements"]
"""Which widgets to display. Remove any you don't want to show."""
# Language filtering
+19 -10
View File
@@ -40,20 +40,29 @@ def compose_widget(
theme_name: str = "dark",
username: str = "",
avatar_b64: str = "",
show_name: bool = True,
) -> str:
"""
Takes rendered SVG strings for each widget type and composes them
into a single unified profile widget by inlining widget SVGs directly.
`show_name` controls whether the avatar + username header block is
drawn. It's the "name" pseudo-widget — pinned first in the UI, can be
toggled off but not reordered. When false, widgets start at the top
edge (minus the standard padding) so there's no orphan gap.
"""
t = THEMES[theme_name]
total_w = 420
header_h = 60
header_h = 60 if show_name else 0
padding = 20
gap = 16
# Extract inner content from each widget SVG
# Extract inner content from each widget SVG. "name" has no rendered
# widget — it maps to the header block and is consumed by show_name.
parts = []
for key in enabled:
if key == "name":
continue
if key in widgets and widgets[key]:
inner, h = _extract_inner(widgets[key], key)
parts.append((key, inner, h))
@@ -61,16 +70,16 @@ def compose_widget(
content_h = sum(h for _, _, h in parts) + gap * max(len(parts) - 1, 0)
total_h = header_h + content_h + padding * 2 + 10
# Header
avatar_svg = ""
if avatar_b64:
avatar_svg = f'''
# Header (avatar + username). Only drawn when "name" is enabled.
header = ""
if show_name:
if avatar_b64:
avatar_svg = f'''
<defs><clipPath id="mainAvatarClip"><circle cx="30" cy="30" r="16"/></clipPath></defs>
<image x="14" y="14" width="32" height="32" href="data:image/png;base64,{avatar_b64}" clip-path="url(#mainAvatarClip)"/>'''
else:
avatar_svg = f'<circle cx="30" cy="30" r="16" fill="{t["accent"]}" opacity="0.3"/>'
header = f'''
else:
avatar_svg = f'<circle cx="30" cy="30" r="16" fill="{t["accent"]}" opacity="0.3"/>'
header = f'''
{avatar_svg}
<text x="54" y="38" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="16" font-weight="700" fill="{t["text"]}">{escape(username or "Developer")}</text>
+32 -1
View File
@@ -67,10 +67,18 @@ def _render_widgets(username: str, payload: dict, settings: dict) -> dict[str, s
achievements=settings.get("achievements") or [],
stored_streak=stored_streak,
)
ordered = [w for w in order if w in enabled and w in widgets and widgets[w]]
enabled_set = set(enabled)
show_name = "name" in enabled_set
# Preserve "name" in the ordered list so the composite honors its
# user-ordered position (always 0 today, but future-proof).
ordered = [
w for w in order
if w in enabled_set and (w == "name" or (w in widgets and widgets[w]))
]
composite = compose_widget(
widgets=widgets, enabled=ordered, theme_name=theme,
username=username, avatar_b64=payload.get("avatar_b64", ""),
show_name=show_name,
)
out = {name: svg for name, svg in widgets.items() if svg}
out["composite"] = composite
@@ -113,6 +121,29 @@ def process_one() -> bool:
return True
def render_composite_adhoc(username: str, override_settings: dict) -> str | None:
"""Render the composite SVG for `username` using stored settings merged
with the per-request `override_settings`. Does NOT persist anything.
Used by the public `GET /api/<u>?...` endpoint so unauthenticated visitors
can preview/embed a widget configured purely via query string, without
touching the owner's stored settings.
Returns the SVG string, or None if the user isn't enrolled / the fetcher
has no data for them (caller should fall through to the cached path).
"""
settings_row = db.get_settings(username)
if settings_row is None:
return None
result = fetcher_client.get_data(username)
payload = result.get("data") or {}
if not payload or payload.get("error") == "not_found":
return None
effective = {**settings_row["settings"], **(override_settings or {})}
widgets = _render_widgets(username, payload, effective)
return widgets.get("composite")
def render_widgets_now(username: str) -> dict[str, str]:
"""Phase 2: synchronously render composite SVG and persist it.