plotly_dynamo.py 7.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
#  SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#  SPDX-License-Identifier: Apache-2.0
"""Dynamo Dark theme for Plotly charts.

Loads design tokens from design_tokens.yaml and produces a reusable
plotly.graph_objects.layout.Template aligned with the Dynamo visual identity.

Typography:
    - Titles, headings, body: Arial (sans-serif)
    - Tick labels, code-like text: Roboto Mono (monospace fallback)

Usage:
    from plotly_dynamo import dynamo_template, load_tokens

    fig = go.Figure(data=[...], layout=go.Layout(template=dynamo_template))
    fig.write_image("chart.svg")
    fig.write_image("chart.png", scale=2)

Or apply globally:
    import plotly.io as pio
    pio.templates.default = dynamo_template
"""

from __future__ import annotations

from pathlib import Path
from typing import Any

import plotly.graph_objects as go
import yaml


def load_tokens(path: str | Path | None = None) -> dict[str, Any]:
    """Load design tokens from YAML file.

    Args:
        path: Path to design_tokens.yaml. Defaults to the file
              next to this module.

    Returns:
        Parsed token dictionary.
    """
    if path is None:
        path = Path(__file__).parent / "design_tokens.yaml"
    with open(path) as f:
        return yaml.safe_load(f)


def build_template(tokens: dict[str, Any] | None = None) -> go.layout.Template:
    """Build a Plotly Template from design tokens.

    Args:
        tokens: Parsed design_tokens dict. Loaded from default path if None.

    Returns:
        A fully configured plotly.graph_objects.layout.Template.
    """
    if tokens is None:
        tokens = load_tokens()

    colors = tokens["colors"]
    typo = tokens["typography"]

    bg_primary = colors["background"]["primary"]
    text_primary = colors["text"]["primary"]
    text_secondary = colors["text"]["secondary"]
    border_subtle = colors["border"]["subtle"]
    series = colors["chart_series"]

    font_sans = typo["font_family"]
    font_mono = typo["font_family_mono"]  # Roboto Mono for ticks/code

    layout = go.Layout(
        # -- Background --
        paper_bgcolor=bg_primary,
        plot_bgcolor=bg_primary,
        # -- Typography --
        font=dict(
            family=font_sans,
            color=text_primary,
            size=typo["label"]["size"],
        ),
        title=dict(
            font=dict(
                family=font_sans,
                size=typo["title"]["size"],
                color=text_primary,
            ),
            x=0.02,
            xanchor="left",
            y=0.98,
            yanchor="top",
        ),
        # -- Color palette --
        colorway=series,
        # -- X axis (monospace tick labels) --
        xaxis=dict(
            gridcolor=border_subtle,
            gridwidth=0.5,
            zeroline=False,
            linecolor=border_subtle,
            linewidth=0.5,
            tickfont=dict(
                family=font_mono,
                size=typo["annotation"]["size"],
                color=text_secondary,
            ),
            title=dict(
                font=dict(
                    family=font_sans,
                    size=typo["label"]["size"],
                    color=text_secondary,
                ),
            ),
        ),
        # -- Y axis (monospace tick labels for alignment) --
        yaxis=dict(
            gridcolor=border_subtle,
            gridwidth=0.5,
            zeroline=False,
            linecolor=border_subtle,
            linewidth=0.5,
            tickfont=dict(
                family=font_mono,
                size=typo["label"]["size"],
                color=text_primary,
            ),
            title=dict(
                font=dict(
                    family=font_sans,
                    size=typo["label"]["size"],
                    color=text_secondary,
                ),
            ),
            automargin=True,
        ),
        # -- Legend --
        legend=dict(
            bgcolor="rgba(0,0,0,0)",
            font=dict(
                family=font_sans,
                size=typo["annotation"]["size"],
                color=text_secondary,
            ),
            bordercolor=border_subtle,
            borderwidth=0,
        ),
        # -- Margins --
        margin=dict(l=20, r=20, t=60, b=20),
        # -- Subtle outer frame (muted green, thin) --
        shapes=[
            dict(
                type="rect",
                xref="paper",
                yref="paper",
                x0=-0.01,
                y0=-0.02,
                x1=1.01,
                y1=1.04,
                line=dict(
                    color=border_subtle,
                    width=0.5,
                ),
                fillcolor="rgba(0,0,0,0)",
                layer="above",
            ),
        ],
    )

    # -- Data defaults --
    template = go.layout.Template(layout=layout)

    # Bar chart defaults
    template.data.bar = [
        go.Bar(
            marker=dict(
                line=dict(width=0),
                opacity=0.92,
            ),
            textfont=dict(
                family=font_mono,
                size=typo["annotation"]["size"],
                color=text_primary,
            ),
            textposition="inside",
            insidetextanchor="start",
            error_x=dict(
                color=text_primary,
                thickness=1.5,
                width=4,
            ),
            error_y=dict(
                color=text_primary,
                thickness=1.5,
                width=4,
            ),
        ),
    ]

    # Scatter / line chart defaults
    template.data.scatter = [
        go.Scatter(
            marker=dict(size=8, line=dict(width=1, color=bg_primary)),
            line=dict(width=2),
        ),
    ]

    return template


# Module-level singleton -- import this directly
dynamo_template = build_template()


def apply_outer_frame(fig: go.Figure) -> go.Figure:
    """Add the Dynamo green outer frame to an existing figure.

    Useful when creating a figure without the template, or when
    the frame was lost during updates.
    """
    tokens = load_tokens()
    border_frame = tokens["colors"]["border"]["frame"]
    frame_width = tokens["borders"]["frame_width"]

    fig.add_shape(
        type="rect",
        xref="paper",
        yref="paper",
        x0=-0.01,
        y0=-0.02,
        x1=1.01,
        y1=1.04,
        line=dict(color=border_frame, width=frame_width),
        fillcolor="rgba(0,0,0,0)",
        layer="above",
    )
    return fig


def section_annotation(
    fig: go.Figure,
    text: str,
    y: float,
    x: float = 0.0,
) -> go.Figure:
    """Add a section header annotation (e.g., 'DISPATCH (us)').

    Uses the Dynamo theme font for section headers.

    Args:
        fig: The Plotly figure.
        text: Section header text (pre-formatted; not auto-uppercased).
        y: Y position in paper coordinates (0-1).
        x: X position in paper coordinates (0-1).
    """
    tokens = load_tokens()
    typo = tokens["typography"]
    text_color = tokens["colors"]["text"]["secondary"]

    fig.add_annotation(
        text=text,
        xref="paper",
        yref="paper",
        x=x,
        y=y,
        showarrow=False,
        font=dict(
            family=typo["font_family"],
            size=typo["heading"]["size"],
            color=text_color,
        ),
        xanchor="left",
        yanchor="bottom",
    )
    return fig