-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
179 lines (167 loc) · 6.52 KB
/
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
import dash_bootstrap_components as dbc
from dash import Dash, Input, Output, State, dcc, html
from src.pages.graph import graph_page_layout
from src.pages.landing import landing_page_layout
from src.pages.table import table_page_layout
# Initialize the Dash app with Bootstrap theme and suppress callback exceptions
app = Dash(
__name__,
external_stylesheets=[
dbc.themes.COSMO,
dbc.icons.FONT_AWESOME,
"assets/css/style.css",
],
suppress_callback_exceptions=True,
)
app.title = "Superstore Dashboard"
# Define the callback for navigating between pages
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def display_page(pathname):
if pathname == "/table":
return table_page_layout
elif pathname == "/graph":
return graph_page_layout
elif pathname == "/landing":
return landing_page_layout
else:
return dcc.Location(id="redirect", href="/landing", refresh=True)
# Define the main app layout with a sidebar for navigation and a top navbar
app.layout = html.Div(
[
dcc.Location(id="url", refresh=False),
dbc.Navbar(
dbc.Container(
[
dbc.NavbarBrand(html.B("Superstore Dashboard"), href="/"),
dbc.NavbarToggler(id="navbar-toggler"),
dbc.Collapse(
dbc.Nav(
[
dbc.NavItem(
dbc.NavLink(
"Landing", href="/landing", active="exact"
)
),
dbc.NavItem(
dbc.NavLink("Table", href="/table", active="exact")
),
dbc.NavItem(
dbc.NavLink("Graph", href="/graph", active="exact")
),
],
className="me-auto",
navbar=True,
),
id="navbar-collapse",
is_open=False,
navbar=True,
),
],
fluid=True,
),
color="primary",
dark=True,
className="navbar-expand-lg w-100",
),
dbc.Container(
[
dbc.Row(
[
dbc.Col(
[
dbc.Nav(
[
dbc.NavLink(
[
html.I(
className="fas fa-home",
style={"margin-right": "10px"},
),
"Landing",
],
href="/landing",
active="exact",
),
dbc.NavLink(
[
html.I(
className="fas fa-table",
style={"margin-right": "10px"},
),
"Table",
],
href="/table",
active="exact",
),
dbc.NavLink(
[
html.I(
className="fas fa-chart-line",
style={"margin-right": "10px"},
),
"Graph",
],
href="/graph",
active="exact",
),
],
vertical=True,
pills=True,
className="sidebar",
),
],
width=2,
className="d-none d-md-block",
id="sidebar",
), # Hide sidebar on screens smaller than md
dbc.Col(
html.Div(id="page-content", className="page-content"),
width=10,
md=10,
),
# Take full width on small screens
],
className="mt-4",
),
],
fluid=True,
id="content",
),
html.Footer(
[
dbc.Container(
[
dbc.Row(
[
dbc.Col(
html.P("© 2024 Superstore Dashboard"),
className="text-center",
)
]
),
]
)
],
className="footer mt-auto py-3 bg-light",
),
],
style={"padding": "0"},
)
# Redirect callback
@app.callback(Output("redirect", "pathname"), [Input("url", "pathname")])
def redirect_to_landing(pathname):
if pathname in ["/", ""]:
return "/landing"
return pathname
# add callback for toggling the collapse on small screens
@app.callback(
Output("navbar-collapse", "is_open"),
[Input("navbar-toggler", "n_clicks")],
[State("navbar-collapse", "is_open")],
)
def toggle_navbar_collapse(n, is_open):
if n:
return not is_open
return is_open
if __name__ == "__main__":
app.run_server(debug=True, port=8000)