Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mantine_light and mantine_dark plotly figure templates #431

Merged
merged 8 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
### Added

- Added `disabled` prop to `Radio` #425 by @namakshenas
- Added the `create_mantine_figure_templates()` function which creates Mantine-styled Plotly figure templates for
both light and dark modes using the default Mantine theme. It registers the templates with plotly.io.templates as
"mantine_light" and "mantine_dark", and optionally sets one of the themes as a default. #431 by @AnnMarie


### Fixed
- Fixed closing of Popovers when clicking outside. #423 by @magicmq
Expand Down
3 changes: 2 additions & 1 deletion dash_mantine_components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ._imports_ import *
from ._imports_ import __all__
from .theme import DEFAULT_THEME
from .figure_templates import create_mantine_figure_templates

if not hasattr(_dash, "__plotly_dash") and not hasattr(_dash, "development"):
print(
Expand Down Expand Up @@ -64,4 +65,4 @@
setattr(locals()[_component], "_css_dist", _css_dist)


__all__ += [DEFAULT_THEME, styles]
__all__ += [DEFAULT_THEME, styles, create_mantine_figure_templates]
110 changes: 110 additions & 0 deletions dash_mantine_components/figure_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from .theme import DEFAULT_THEME
import plotly.graph_objects as go
import plotly.io as pio
import copy


def create_mantine_figure_templates(default=None):

"""
Create and register Plotly figure templates styled to match the Mantine default theme.

This function generates two custom Plotly templates:
- "mantine_light" for light mode
- "mantine_dark" for dark mode

Templates are registered with `plotly.io.templates`, allowing you to apply them to Plotly figures
using the template names "mantine_light" or "mantine_dark". These templates include Mantine-inspired
color palettes, background colors, and other layout customizations.

Parameters:
- default (str): The default template to apply globally. Must be either "mantine_light" or "mantine_dark".
If not set, the default Plotly template remains unchanged.

Returns:
- None: The templates are registered and optionally set as the default, but no value is returned.

"""

colors = DEFAULT_THEME["colors"]
font_family = DEFAULT_THEME["fontFamily"]
# pallet generated from https://www.learnui.design/tools/data-color-picker.html#palette
custom_colorscale = [
"#1864ab", # blue[9]
"#7065b9",
"#af61b7",
"#e35ea5",
"#ff6587",
"#ff7c63",
"#ff9e3d",
"#fcc419", # yellow[5]
]

# Default theme configurations
default_themes = {
"light": {
"colorway": [
colors[color][6]
for color in ["blue", "red", "green", "violet", "orange", "cyan", "pink", "yellow"]
],
"paper_bgcolor": "#f8f9fa",
"plot_bgcolor": "#ffffff",
"gridcolor": "#dee2e6",
},
"dark": {
"colorway": [
colors[color][8]
for color in ["blue", "red", "green", "violet", "orange", "cyan", "pink", "yellow"]
],
"paper_bgcolor": "#1a1b1e",
"plot_bgcolor": "#25262b",
"gridcolor": "#343a40",
}
}

def make_template(name):
#Start with either a light or dark Plotly template
base = "plotly_white" if name == "light" else "plotly_dark"
template = copy.deepcopy(pio.templates[base])

layout = template.layout
theme_config = default_themes[name]

# Apply theme settings
layout.colorway = theme_config["colorway"]
layout.colorscale.sequential = custom_colorscale
layout.piecolorway = theme_config["colorway"]
layout.paper_bgcolor = theme_config["paper_bgcolor"]
layout.plot_bgcolor = theme_config["plot_bgcolor"]
layout.font.family = font_family

# Grid settings
for axis in (layout.xaxis, layout.yaxis):
axis.gridcolor = theme_config["gridcolor"]
axis.gridwidth = 0.5
axis.zerolinecolor = theme_config["gridcolor"]

# Geo settings
layout.geo.bgcolor = theme_config["plot_bgcolor"]
layout.geo.lakecolor = theme_config["plot_bgcolor"]
layout.geo.landcolor = theme_config["plot_bgcolor"]

# Hover label settings
layout.hoverlabel.font.family = font_family

# Scatter plot settings
template.data.scatter = (go.Scatter(marker_line_color=theme_config["plot_bgcolor"]),)
template.data.scattergl = (go.Scattergl(marker_line_color=theme_config["plot_bgcolor"]),)

return template


# #register templates
pio.templates["mantine_light"] = make_template("light")
pio.templates["mantine_dark"] = make_template("dark")

# set the default
if default in ["mantine_light", "mantine_dark"]:
pio.templates.default = default
AnnMarieW marked this conversation as resolved.
Show resolved Hide resolved

return None