import dash_mantine_components as dmc
from dash import Dash, dcc, Input, Output, State, callback, _dash_renderer, Patch
import plotly.graph_objects as go
import pandas as pd
import plotly.io as pio
from dash_iconify import DashIconify
_dash_renderer._set_react_version("18.2.0")
dmc.add_figure_templates(default="mantine_dark")
def make_range_buttons():
# Load data
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df.columns = [col.replace("AAPL.", "") for col in df.columns]
# Create figure
fig = go.Figure()
fig.add_trace(
go.Scatter(x=list(df.Date), y=list(df.High)))
# Set title
fig.update_layout(
title_text="Time series with range slider and selectors"
)
# Add range slider
fig.update_layout(
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
label="1m",
step="month",
stepmode="backward"),
dict(count=6,
label="6m",
step="month",
stepmode="backward"),
dict(count=1,
label="YTD",
step="year",
stepmode="todate"),
dict(count=1,
label="1y",
step="year",
stepmode="backward"),
dict(step="all")
])
),
rangeslider=dict(
visible=True
),
type="date"
)
)
return fig
theme_toggle = dmc.ActionIcon(
[
dmc.Paper(DashIconify(icon="radix-icons:sun", width=25), darkHidden=True),
dmc.Paper(DashIconify(icon="radix-icons:moon", width=25), lightHidden=True),
],
variant="transparent",
color="yellow",
id="color-scheme-toggle",
size="lg",
)
layout = dmc.Container([
dmc.Group([
dmc.Title("Figure Template Demo", my="md"),
theme_toggle,
], justify="space-between"),
dcc.Graph(figure=make_range_buttons(), id="graph")
], fluid=True)
app= Dash(external_stylesheets=dmc.styles.ALL)
app.layout = dmc.MantineProvider(
layout,
forceColorScheme="dark",
id="mantine-provider"
)
@callback(
Output("mantine-provider", "forceColorScheme"),
Input("color-scheme-toggle", "n_clicks"),
State("mantine-provider", "forceColorScheme"),
prevent_initial_call=True,
)
def switch_theme(_, theme):
return "dark" if theme == "light" else "light"
@callback(
Output("graph", "figure"),
Input("mantine-provider", "forceColorScheme"),
)
def update_figure(theme):
# template must be template object rather than just the template string name
template = pio.templates["mantine_light"] if theme == "light" else pio.templates["mantine_dark"]
patched_fig = Patch()
patched_fig["layout"]["template"] = template
return patched_fig
if __name__ == "__main__":
app.run(debug=True)