Skip to content

Commit 3124e27

Browse files
authored
Merge pull request snehilvj#431 from AnnMarieW/add-plotly-figure-templates
add mantine_light and mantine_dark plotly figure templates
2 parents 092cdf7 + d6f42eb commit 3124e27

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
### Added
55

66
- Added `disabled` prop to `Radio` #425 by @namakshenas
7+
8+
- Added the `add_figure_templates()` function which creates Mantine-styled Plotly figure templates for
9+
both light and dark modes using the default Mantine theme. It registers the templates with plotly.io.templates as
10+
"mantine_light" and "mantine_dark", and optionally sets one of the themes as a default. #431 by @AnnMarie
11+
712
- Added various props: `acceptValueOnBlur` to `TagsInput`, `gradient` to `LineChart`, `hideWithOnePage` to `Pagination`, `name` to `Avatar`, and `disabled` to `NumberInput`. #440 by @AnnMarieW
13+
814
- Added `SemiCircleProgress` component #434 by @AnnMarieW
915

16+
1017
### Fixed
1118
- Fixed closing of Popovers when clicking outside. #423 by @magicmq
1219

dash_mantine_components/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ._imports_ import *
1313
from ._imports_ import __all__
1414
from .theme import DEFAULT_THEME
15+
from .figure_templates import add_figure_templates
1516

1617
if not hasattr(_dash, "__plotly_dash") and not hasattr(_dash, "development"):
1718
print(
@@ -64,4 +65,4 @@
6465
setattr(locals()[_component], "_css_dist", _css_dist)
6566

6667

67-
__all__ += [DEFAULT_THEME, styles]
68+
__all__ += [DEFAULT_THEME, styles, add_figure_templates]
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from .theme import DEFAULT_THEME
2+
import plotly.graph_objects as go
3+
import plotly.io as pio
4+
import copy
5+
6+
7+
def add_figure_templates(default=None):
8+
9+
"""
10+
Create and register Plotly figure templates styled to match the Mantine default theme.
11+
12+
This function generates two custom Plotly templates:
13+
- "mantine_light" for light mode
14+
- "mantine_dark" for dark mode
15+
16+
Templates are registered with `plotly.io.templates`, allowing you to apply them to Plotly figures
17+
using the template names "mantine_light" or "mantine_dark". These templates include Mantine-inspired
18+
color palettes, background colors, and other layout customizations.
19+
20+
Parameters:
21+
- default (str): The default template to apply globally. Must be either "mantine_light" or "mantine_dark".
22+
If not set, the default Plotly template remains unchanged.
23+
24+
Returns:
25+
- None: The templates are registered and optionally set as the default, but no value is returned.
26+
27+
"""
28+
29+
colors = DEFAULT_THEME["colors"]
30+
font_family = DEFAULT_THEME["fontFamily"]
31+
# pallet generated from https://www.learnui.design/tools/data-color-picker.html#palette
32+
custom_colorscale = [
33+
"#1864ab", # blue[9]
34+
"#7065b9",
35+
"#af61b7",
36+
"#e35ea5",
37+
"#ff6587",
38+
"#ff7c63",
39+
"#ff9e3d",
40+
"#fcc419", # yellow[5]
41+
]
42+
43+
# Default theme configurations
44+
default_themes = {
45+
"light": {
46+
"colorway": [
47+
colors[color][6]
48+
for color in ["blue", "red", "green", "violet", "orange", "cyan", "pink", "yellow"]
49+
],
50+
"paper_bgcolor": "#ffffff", # mantine background color
51+
"plot_bgcolor": "#ffffff",
52+
"gridcolor": "#dee2e6",
53+
},
54+
"dark": {
55+
"colorway": [
56+
colors[color][8]
57+
for color in ["blue", "red", "green", "violet", "orange", "cyan", "pink", "yellow"]
58+
],
59+
"paper_bgcolor": colors["dark"][7], # mantine background color
60+
"plot_bgcolor": colors["dark"][7],
61+
"gridcolor": "#343a40",
62+
}
63+
}
64+
65+
def make_template(name):
66+
#Start with either a light or dark Plotly template
67+
base = "plotly_white" if name == "light" else "plotly_dark"
68+
template = copy.deepcopy(pio.templates[base])
69+
70+
layout = template.layout
71+
theme_config = default_themes[name]
72+
73+
# Apply theme settings
74+
layout.colorway = theme_config["colorway"]
75+
layout.colorscale.sequential = custom_colorscale
76+
layout.piecolorway = theme_config["colorway"]
77+
layout.paper_bgcolor = theme_config["paper_bgcolor"]
78+
layout.plot_bgcolor = theme_config["plot_bgcolor"]
79+
layout.font.family = font_family
80+
81+
# Grid settings
82+
for axis in (layout.xaxis, layout.yaxis):
83+
axis.gridcolor = theme_config["gridcolor"]
84+
axis.gridwidth = 0.5
85+
axis.zerolinecolor = theme_config["gridcolor"]
86+
87+
# Geo settings
88+
layout.geo.bgcolor = theme_config["plot_bgcolor"]
89+
layout.geo.lakecolor = theme_config["plot_bgcolor"]
90+
layout.geo.landcolor = theme_config["plot_bgcolor"]
91+
92+
# Hover label settings
93+
layout.hoverlabel.font.family = font_family
94+
95+
# Scatter plot settings
96+
template.data.scatter = (go.Scatter(marker_line_color=theme_config["plot_bgcolor"]),)
97+
template.data.scattergl = (go.Scattergl(marker_line_color=theme_config["plot_bgcolor"]),)
98+
99+
return template
100+
101+
102+
# #register templates
103+
pio.templates["mantine_light"] = make_template("light")
104+
pio.templates["mantine_dark"] = make_template("dark")
105+
106+
# set the default
107+
if default in ["mantine_light", "mantine_dark"]:
108+
pio.templates.default = default
109+
elif default:
110+
raise ValueError(f"unrecognized {default=}, allowed values are 'mantine_light' and 'mantine_dark'")
111+
112+
return None

0 commit comments

Comments
 (0)