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
1 change: 1 addition & 0 deletions 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
94 changes: 94 additions & 0 deletions dash_mantine_components/figure_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import dash_mantine_components as dmc
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
import plotly.graph_objects as go
import plotly.io as pio
import copy


def create_mantine_figure_templates():
"""
Create Mantine-styled Plotly templates for both light and dark modes.
registers templates with plotly.io.templates as "mantine_light" and "mantine_dark"
"""

colors = dmc.DEFAULT_THEME["colors"]
font_family = dmc.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")

return None


# Create and register templates
create_mantine_figure_templates()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little uneasy about automatically calling this - the convenience is great, but it's a bit magical. For example if you like these themes and want to use them without DMC, you have to know to import the package anyway and silence your linter.

Curious if perhaps @ndrezn you know whether any other packages add their own templates like this, or if you have thoughts on this pattern?

If we do automatically call this, there's no reason to import it by name in __init__.py - just import .figure_templates will do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started with requiring it to be imported in an app, but thought it was an unnecessary line of code when it could just be made available, but I see your point about it being too magical.

I can't imagine why anyone would use this outside of DMC though. It's not different enough from plotly_white and plotly_dark themes. It's just tweaked a little to make it look better with the color tones used in the default DMC theme.

Copy link

@ndrezn ndrezn Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is okay, but I also haven't seen it before. It's quite nice to be able to use the templates API natively but I agree it's not obvious what's going on.

Probably worth a specific documentation page that explains how to do this and maybe provides a reduced import if you want to use just the named themes but not the rest of Mantine (from dash_mantine_components import templates?)

Copy link
Collaborator Author

@AnnMarieW AnnMarieW Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's documented that when you use the dmc library, you will have access to the "mantine_light" and "mantine_dark" templates, shouldn't that be OK?, Why is that any more magical than having access to all the other built-in templates that come with the Plotly library? They are not set as defaults - you have to specify them in the figure definition otherwise you get the standard "plotly" template. I really don't think people would want to use these templates if they are not using DMC

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels somewhat magical to me because with the built-in templates you can go look for docs on the template arg and you immediately get to https://plotly.com/python/templates/#view-available-themes - which lists all the built-in templates, and you can go from there to learn about each of these templates. With these ones, an operation that normally has no side-effects (importing a library) results in extra options being available in a different library, so if you want to learn about these or if they disappeared and you want to know how to get them back, what do you do?

That said, given that the new options have the name mantine in them and that's a pretty distinctive name, it probably wouldn't be too hard for users to connect the dots and figure out that they came from dash_mantine_components - then if it's obvious how to find them in the docs we're good. Also we document exactly the pattern you're using 😅 https://plotly.com/python/templates/#saving-and-distributing-custom-themes

So OK, let's leave it as is. Then I'd still change the import in __init__.py to just import .figure_templates 😎

a reduced import if you want to use just the named themes but not the rest of Mantine

I don't think we can do this - importing a submodule will always import the parent - so unless we wanted to move the rest of DMC out of the top-level module we're always going to get it. Anyway I guess @AnnMarieW is right that this is similar enough to built-in themes that this is unlikely to come up much.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, OK, I get it now:

With these ones, an operation that normally has no side-effects (importing a library) results in extra options being available in a different library,

You're right - it's better to make this a function that is called in the app. Also, if it's a function, I could make it so you could set the default to one of these templates, which would be very convenient when not using a theme switcher.

Next trick would be to make it possible to redraw the figure when the theme changes without updating the entire figure in a callback, but I think that would need a custom Graph component based on dcc.Graph

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha we convinced each other to swap places 🔀

Being able to set the default is really nice though, so this is great. I do have a slight preference for shortening the function name - could it just be add_figure_templates? It doesn't need mantine in the name because it's already in the dash_mantine_components package, and to my mind the key verb is add, ie add them to the Plotly options.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I wasn’t a fan of the name, but couldn’t think of a better one. I like your suggestion 🙂

Loading