|
1 | 1 | import dash
|
2 |
| -from dash import dcc, html, Output, Input, State |
| 2 | +from dash import callback, Output, Input, State |
3 | 3 | import dash_mantine_components as dmc
|
4 | 4 | from dash_iconify import DashIconify
|
5 | 5 |
|
| 6 | +# Set "mantine_light" as the default plotly figure template |
| 7 | +# styles Plotly figures with Mantine theme https://www.dash-mantine-components.com/components/figure-templates |
| 8 | +dmc.add_figure_templates(default="mantine_light") |
6 | 9 |
|
7 |
| -app = dash.Dash(__name__, use_pages=True) |
| 10 | +app = dash.Dash(__name__, use_pages=True, external_stylesheets=dmc.styles.ALL) |
8 | 11 |
|
9 | 12 |
|
10 | 13 | def create_nav_link(icon, label, href):
|
11 |
| - return dcc.Link( |
12 |
| - dmc.Group( |
13 |
| - [ |
14 |
| - dmc.ThemeIcon( |
15 |
| - DashIconify(icon=icon, width=18), |
16 |
| - size=30, |
17 |
| - radius=30, |
18 |
| - variant="light", |
19 |
| - ), |
20 |
| - dmc.Text(label, size="sm", color="gray"), |
21 |
| - ] |
22 |
| - ), |
| 14 | + return dmc.NavLink( |
| 15 | + label=label, |
23 | 16 | href=href,
|
24 |
| - style={"textDecoration": "none"}, |
25 |
| - ) |
| 17 | + active="exact", |
| 18 | + leftSection=DashIconify(icon=icon) |
26 | 19 |
|
| 20 | + ) |
27 | 21 |
|
28 |
| -sidebar = dmc.Navbar( |
29 |
| - fixed=True, |
30 |
| - width={"base": 300}, |
31 |
| - position={"top": 80}, |
32 |
| - height=300, |
33 |
| - children=[ |
34 |
| - dmc.ScrollArea( |
35 |
| - offsetScrollbars=True, |
36 |
| - type="scroll", |
37 |
| - children=[ |
38 |
| - dmc.Stack( |
39 |
| - children=[ |
40 |
| - create_nav_link( |
41 |
| - icon="radix-icons:rocket", |
42 |
| - label="Home", |
43 |
| - href="/", |
44 |
| - ), |
45 |
| - ], |
46 |
| - ), |
47 |
| - dmc.Divider( |
48 |
| - label="Chapter 1", style={"marginBottom": 20, "marginTop": 20} |
49 |
| - ), |
50 |
| - dmc.Stack( |
51 |
| - children=[ |
52 |
| - create_nav_link( |
53 |
| - icon=page["icon"], label=page["name"], href=page["path"] |
54 |
| - ) |
55 |
| - for page in dash.page_registry.values() |
56 |
| - if page["path"].startswith("/chapter1") |
57 |
| - ], |
58 |
| - ), |
59 |
| - dmc.Divider( |
60 |
| - label="Chapter 2", style={"marginBottom": 20, "marginTop": 20} |
61 |
| - ), |
62 |
| - dmc.Stack( |
63 |
| - children=[ |
64 |
| - create_nav_link( |
65 |
| - icon=page["icon"], label=page["name"], href=page["path"] |
66 |
| - ) |
67 |
| - for page in dash.page_registry.values() |
68 |
| - if page["path"].startswith("/chapter2") |
69 |
| - ], |
| 22 | +navbar_links =dmc.Box( |
| 23 | + [ |
| 24 | + create_nav_link( |
| 25 | + icon="radix-icons:rocket", |
| 26 | + label="Home", |
| 27 | + href="/", |
70 | 28 | ),
|
| 29 | + dmc.Divider( |
| 30 | + label="Chapter 1", style={"marginBottom": 20, "marginTop": 20} |
| 31 | + ), |
| 32 | + dmc.Stack( |
| 33 | + [ |
| 34 | + create_nav_link( |
| 35 | + icon=page["icon"], label=page["name"], href=page["path"] |
| 36 | + ) |
| 37 | + for page in dash.page_registry.values() |
| 38 | + if page["path"].startswith("/chapter1") |
| 39 | + ], |
| 40 | + ), |
| 41 | + dmc.Divider( |
| 42 | + label="Chapter 2", style={"marginBottom": 20, "marginTop": 20} |
| 43 | + ), |
| 44 | + dmc.Stack( |
| 45 | + [ |
| 46 | + create_nav_link( |
| 47 | + icon=page["icon"], label=page["name"], href=page["path"] |
| 48 | + ) |
| 49 | + for page in dash.page_registry.values() |
| 50 | + if page["path"].startswith("/chapter2") |
71 | 51 | ],
|
72 |
| - ) |
| 52 | + ), |
73 | 53 | ],
|
74 | 54 | )
|
75 | 55 |
|
76 |
| -app.layout = dmc.Container( |
| 56 | +layout = dmc.AppShell( |
77 | 57 | [
|
78 |
| - dmc.Header( |
79 |
| - height=70, |
80 |
| - children=[dmc.Text("Company Logo")], |
81 |
| - style={"backgroundColor": "#228be6"}, |
| 58 | + dmc.AppShellHeader( |
| 59 | + dmc.Group( |
| 60 | + [ |
| 61 | + dmc.Burger(id="burger", size="sm", hiddenFrom="sm", opened=False), |
| 62 | + dmc.Title("Demo App", c="blue"), |
| 63 | + ], |
| 64 | + h="100%", |
| 65 | + px="md", |
| 66 | + ) |
82 | 67 | ),
|
83 |
| - sidebar, |
84 |
| - dmc.Container( |
85 |
| - dash.page_container, |
86 |
| - size="lg", |
87 |
| - pt=20, |
88 |
| - style={"marginLeft": 300}, |
| 68 | + dmc.AppShellNavbar( |
| 69 | + id="navbar", |
| 70 | + children=navbar_links, |
| 71 | + p="md", |
89 | 72 | ),
|
| 73 | + dmc.AppShellMain(dash.page_container), |
90 | 74 | ],
|
91 |
| - fluid=True, |
| 75 | + header={"height": 60}, |
| 76 | + padding="md", |
| 77 | + navbar={ |
| 78 | + "width": 300, |
| 79 | + "breakpoint": "sm", |
| 80 | + "collapsed": {"mobile": True}, |
| 81 | + }, |
| 82 | + id="appshell", |
| 83 | +) |
| 84 | + |
| 85 | + |
| 86 | +app.layout = dmc.MantineProvider(layout) |
| 87 | + |
| 88 | + |
| 89 | +@callback( |
| 90 | + Output("appshell", "navbar"), |
| 91 | + Input("burger", "opened"), |
| 92 | + State("appshell", "navbar"), |
92 | 93 | )
|
| 94 | +def navbar_is_open(opened, navbar): |
| 95 | + navbar["collapsed"] = {"mobile": not opened} |
| 96 | + return navbar |
93 | 97 |
|
94 | 98 |
|
95 | 99 | if __name__ == "__main__":
|
|
0 commit comments