Py.Cafe

dash.mantine.components/

dash-mantine-theme-builder

Dash Mantine Components Theme Builder

DocsPricing
  • components/
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import json
import dash_mantine_components as dmc
from dash_iconify import DashIconify
from dash import Dash, _dash_renderer, Input, Output, State, callback
from components import progress_card
from components import theme_switch
from components import figures
from components import authentication
from components import sample_components
from components import stepper
from components import date_picker

_dash_renderer._set_react_version("18.2.0")

app = Dash(external_stylesheets=dmc.styles.ALL)

colors = dmc.DEFAULT_THEME["colors"]
color_picker_value_mapping = {color: codes[5] for color, codes in colors.items() if color != "dark"}
theme_name_mapping = {codes[5]: color for color, codes in colors.items() if color != "dark"}
size_name_mapping = {1: "xs", 2: "sm", 3: "md", 4: "lg", 5: "xl"}


color_picker = dmc.Stack(
    [
        dmc.Text("Color", size="xs"),
        dmc.ColorPicker(
            id="color-picker",
            size="sm",
            withPicker=False,
            swatches=list(color_picker_value_mapping.values()),
            swatchesPerRow=7,
            value=color_picker_value_mapping["green"],
        ),
    ]
)


def make_slider(title, id):
    return dmc.Stack(
        [
            dmc.Text(title, size="sm"),
            dmc.Slider(
                min=1,
                max=5,
                value=2,
                id=id,
                updatemode="drag",
                styles={"markLabel": {"display": "none"}},
                marks=[
                    {"value": 1, "label": "xs"},
                    {"value": 2, "label": "sm"},
                    {"value": 3, "label": "md"},
                    {"value": 4, "label": "lg"},
                    {"value": 5, "label": "xl"},
                ],
            ),
        ],
        mt="md",
    )


customize_theme = dmc.Box(
    [
        dmc.Button("Customize Theme", id="modal-demo-button"),
        dmc.Modal(
            id="modal-customize",
            size="sm",
            children=[
                dmc.Box(
                    [
                        color_picker,
                        make_slider("Radius", "radius"),
                        make_slider("Shadow", "shadow"),
                        dmc.Group([theme_switch.theme_toggle, dmc.Text("light/dark color scheme", size="sm", pt="sm")])
                    ],
                    bd="1px solid gray.3",
                    p="sm",
                )
            ],
            zIndex=1000,
            withCloseButton=False,
            yOffset="0vh",
            styles={"overlay": {"background": "rgba(0, 0, 0, 0.3)"}},
        ),
    ]
)


see_code = dmc.Box(
    [
        dmc.Button("copy theme code", id="modal-code-button", variant="outline"),
        dmc.Modal(
            [
                dmc.CodeHighlight(id="json", code="", language="json", h=300),
                dmc.Text("dmc.MantineProvider(theme=theme)"),
            ],
            zIndex=1000,
            withCloseButton=False,
            yOffset="0vh",
            id="modal-code",
        ),
    ]
)

github_link =  dmc.Anchor(
    dmc.ActionIcon(
        DashIconify(icon= "radix-icons:github-logo", width=25), variant="transparent", size="lg"
    ),
    href="https://github.com/AnnMarieW/dmc-theme-builder",
    target="_blank",
    visibleFrom="xs",
)


sample_app = dmc.Box(
    [
        dmc.Group(
            [
                sample_components.card,
                progress_card.card,
                figures.card,
                date_picker.card,
                authentication.card,
                stepper.card,
            ],
            gap="lg",
            justify="center",
        ),
    ]
)

layout = dmc.Container(
    [
        dmc.Group([
            dmc.Title("Dash Mantine Components Theme Builder", order=1, mt="lg"),
            github_link
        ], justify="space-between"),
        dmc.Title(
            "Set default color, radius, shadow, and color scheme", order=3, mb="lg"
        ),
        dmc.Group([customize_theme, see_code]),
        dmc.Divider(size="md", mt="lg"),
        dmc.Space(h=60),
        sample_app,
    ],
   # fluid=True,
    mb="lg",
)

app.layout = dmc.MantineProvider(
    layout,
    theme={
        "primaryColor": "green",
        "defaultRadius": "sm",
        "components": {"Card": {"defaultProps": {"shadow": "sm"}}},
    },
    forceColorScheme="light",
    id="mantine-provider",
)


@callback(
    Output("mantine-provider", "theme"),
    Output("json", "code"),
    Input("color-picker", "value"),
    Input("radius", "value"),
    Input("shadow", "value"),
    State("mantine-provider", "theme"),
)
def update(color, radius, shadow, theme):
    theme["primaryColor"] = theme_name_mapping[color]
    theme["defaultRadius"] = size_name_mapping[radius]
    theme["components"]["Card"]["defaultProps"]["shadow"] = size_name_mapping[shadow]
    return theme, "theme=" + json.dumps(theme, indent=4)


@callback(
    Output("modal-customize", "opened"),
    Input("modal-demo-button", "n_clicks"),
    State("modal-customize", "opened"),
    prevent_initial_call=True,
)
def modal_demo(n, opened):
    return not opened


@callback(
    Output("modal-code", "opened"),
    Input("modal-code-button", "n_clicks"),
    State("modal-code", "opened"),
    prevent_initial_call=True,
)
def modal_demo(n, opened):
    return not opened


if __name__ == "__main__":
    app.run(debug=True)