Template Generation
Overview
The template generation area is the source for Plotly's built-in figure templates and the generated JSON artifacts shipped with the package. Its source definitions live under templategen/**; its generated package data lives under plotly/package_data/templates/**.
templategen/definitions.py organizes each built-in template as a builder function that returns a plotly.graph_objs.layout.Template. Each builder is registered in the module-level builders dict immediately after definition, and templategen/__main__.py uses that registry as the generation entrypoint. Running the module imports builders, calls every builder, serializes the returned Template with plotly.io.json.to_json_plotly, and writes one JSON file per builder to plotly/…/<template_name>.json.
The main theme builders in templategen/definitions.py are ggplot2, simple_white, seaborn, plotly, plotly_white, and plotly_dark. They define colorways, colorscales, colorbar defaults, axis defaults, annotation defaults, and shape defaults, then call templategen/…/__init__.py's initialize_template helper to populate common layout and trace defaults. After that common initialization, each builder applies theme-specific overrides such as title alignment, 3D axis grid widths, ternary colors, map style, zero-line styling, scatter outlines, histogram bin outlines, and pie automargin defaults. presentation, xgridoff, ygridoff, and gridon are smaller modifier templates built directly from a blank Template; they set only the presentation sizing or cartesian grid defaults they are meant to layer with other templates.
templategen/…/__init__.py centralizes the shared template shape. initialize_template creates a go.layout.Template, sets global layout defaults (autotypenumbers="strict", colorway, font color, hover behavior, paper/plot/polar/ternary backgrounds), applies colorbar defaults to trace types listed in colorscale_parent_paths, configures layout colorscales and trace colorscales for heatmap-like traces, and builds defaults for cartesian, 3D, ternary, polar, carpet, geo, table, scatter, bar, barpolar, and histogram traces. Its set_all_colorbars helper ensures that each trace type with a possible colorbar has a default trace object before assigning colorbar settings when the target property exists. templategen/…/colors.py is a large constant table of named RGB strings exposed through the colors dict; the definition builders use entries such as gray14, gray85, and gray93 rather than recomputing those strings.
The generated artifacts in plotly/…/*.json are single-line JSON serializations of the Template objects. The current generated set mirrors the registered builders: ggplot2.json, seaborn.json, simple_white.json, plotly.json, plotly_white.json, plotly_dark.json, presentation.json, xgridoff.json, ygridoff.json, and gridon.json. These files are not dynamically regenerated at import time; they are package data consumed by Plotly runtime code.
Plotly's runtime template loader in plotly/…/_templates.py is the direct consumer of these generated files. TemplatesConfig.__init__ registers the built-in names as lazy entries, including all generated template names plus the special none template. When code accesses plotly.io.templates["plotly"] or a composed key such as "plotly_white+presentation", TemplatesConfig.__getitem__ loads package_data/…/<template_name>.json with pkgutil.get_data("plotly", path), parses it with json.loads, creates a Template(template_dict, _validate=False), caches it, and then returns the merged template result. The special none template does not have a JSON file; it is created in code as a mostly empty Template(data_scatter=[{}]).
Coverage / routing
This doc owns:
templategen/**— source definitions, generation entrypoint, shared initialization helpers, and color constants for built-in templates.plotly/package_data/templates/**— generated JSON artifacts loaded as package data byplotly.io.
Coupling
- If a builder is added, removed, or renamed in templategen/definitions.py, also update the generated files under
plotly/package_data/templates/**and thedefault_templateslist in plotly/…/_templates.py; otherwise a generated file may be orphaned, orplotly.io.templatesmay advertise a built-in name whose JSON package data is missing. - If templategen/…/__init__.py changes
initialize_template's common defaults, regenerate and review every JSON artifact underplotly/package_data/templates/**, because all full theme builders call this helper and the runtime loader reads only the serialized package data. - If
colorscale_parent_pathsin templategen/…/__init__.py is changed, verify the paths against Plotly trace schemas before regenerating templates;set_all_colorbarsindexes into generated trace defaults by path and silently creates default trace entries for each listed trace type. - If plotly/…/*.json is edited directly, reconcile the same behavior back into templategen/definitions.py or
templategen/utils/**; otherwise the next run of templategen/__main__.py will overwrite the direct JSON edit. - If template serialization behavior changes in
plotly.io.json.to_json_plotly, check the generated files inplotly/package_data/templates/**and runtime loading in plotly/…/_templates.py; the generator writes whateverto_json_plotlyemits, and the loader expects valid JSON dictionaries forTemplate(..., _validate=False).
Risks
- templategen/__main__.py writes to plotly/…/<name>.json using a relative path from the current working directory. Running it outside the repository root can fail to update the package artifacts or create output in an unintended location.
- templategen/…/__init__.py mutates dictionaries passed into
initialize_template: it popsstandofffrom the copied 3D axis title dict and popstitlefromaxis_commonbefore assigning polar axes. Builders in templategen/definitions.py currently pass fresh dictionaries per call; reusing an axis defaults dict across calls could lose title settings after the first initialization.