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

Conversation

AnnMarieW
Copy link
Collaborator

@AnnMarieW AnnMarieW commented Nov 23, 2024

This PR adds 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.

The templates can be used in an app just like any built-in plotly template, for example:

px.histogram(dff, x='lifeExp', title='2007 Distribution of Life Expectancy', template="mantine_dark")

Here is a sample app hosted on PyCafe: https://py.cafe/amward/mantine-themed-plotly-figure-templates2

image

image

@AnnMarieW
Copy link
Collaborator Author

Here's a demo app for the docs: https://py.cafe/amward/mantine-plotly-figure-templates



# 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 🙂

Copy link
Collaborator

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

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

💃 Just one small remaining suggestion but nonblocking.

Repository owner deleted a comment from github-actions bot Nov 27, 2024
@AnnMarieW
Copy link
Collaborator Author

@alexcjohnson Do you have any comments on the style of the new figure templates? Anything that should be changed? Anything missing for other chart types?

Copy link

Generated link: snehilvj/dash-mantine-components-431

Repository owner deleted a comment from github-actions bot Nov 27, 2024
@AnnMarieW AnnMarieW merged commit 3124e27 into snehilvj:master Dec 3, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants