-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
378 lines (334 loc) · 12.1 KB
/
app.py
File metadata and controls
378 lines (334 loc) · 12.1 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import dash
from dash import dcc, html, Input, Output
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# Initialize the Dash app
app = dash.Dash(__name__)
server = app.server
# Define CSS styles for light and dark themes
LIGHT_THEME = {
'backgroundColor': '#FFFFFF',
'color': '#000000',
'cardBackground': '#F8F9FA',
'borderColor': '#E9ECEF'
}
DARK_THEME = {
'backgroundColor': '#1E1E1E',
'color': '#FFFFFF',
'cardBackground': '#2D2D2D',
'borderColor': '#404040'
}
# Load the data
def load_data():
try:
# Try to load the actual CSV file
df = pd.read_csv('animals_info.csv')
return df
except FileNotFoundError:
# Create sample data if file doesn't exist (for demonstration)
print("animals_info.csv not found. Creating sample data...")
# Sample data structure based on typical animal datasets
sample_data = {
'name': ['Lion', 'Tiger', 'Eagle', 'Shark', 'Elephant', 'Penguin', 'Snake', 'Frog',
'Butterfly', 'Whale', 'Bear', 'Deer', 'Owl', 'Turtle', 'Kangaroo', 'Dolphin',
'Wolf', 'Rabbit', 'Hawk', 'Octopus', 'Gorilla', 'Zebra', 'Flamingo', 'Crocodile'],
'class': ['Mammalia', 'Mammalia', 'Aves', 'Chondrichthyes', 'Mammalia', 'Aves', 'Reptilia', 'Amphibia',
'Insecta', 'Mammalia', 'Mammalia', 'Mammalia', 'Aves', 'Reptilia', 'Mammalia', 'Mammalia',
'Mammalia', 'Mammalia', 'Aves', 'Mollusca', 'Mammalia', 'Mammalia', 'Aves', 'Reptilia'],
'habitat': ['Grassland', 'Forest', 'Mountains', 'Ocean', 'Savanna', 'Arctic', 'Desert', 'Wetland',
'Garden', 'Ocean', 'Forest', 'Forest', 'Forest', 'Wetland', 'Grassland', 'Ocean',
'Forest', 'Grassland', 'Mountains', 'Ocean', 'Forest', 'Savanna', 'Wetland', 'Wetland'],
'diet': ['Carnivore', 'Carnivore', 'Carnivore', 'Carnivore', 'Herbivore', 'Carnivore', 'Carnivore', 'Carnivore',
'Herbivore', 'Carnivore', 'Omnivore', 'Herbivore', 'Carnivore', 'Omnivore', 'Herbivore', 'Carnivore',
'Carnivore', 'Herbivore', 'Carnivore', 'Carnivore', 'Omnivore', 'Herbivore', 'Omnivore', 'Carnivore'],
'order': ['Carnivora', 'Carnivora', 'Accipitriformes', 'Carcharhiniformes', 'Proboscidea', 'Sphenisciformes',
'Squamata', 'Anura', 'Lepidoptera', 'Cetacea', 'Carnivora', 'Artiodactyla', 'Strigiformes',
'Testudines', 'Diprotodontia', 'Cetacea', 'Carnivora', 'Lagomorpha', 'Accipitriformes',
'Octopoda', 'Primates', 'Perissodactyla', 'Phoenicopteriformes', 'Crocodilia'],
'population': [20000, 3900, 500000, 100000, 415000, 12000000, 3000000, 2000000,
200000000, 50000, 600000, 30000000, 4000000, 300000, 50000000, 600000,
300000, 40000000, 2000000, 300000000, 1000000, 750000, 3200000, 250000]
}
df = pd.DataFrame(sample_data)
return df
# Load the data
df = load_data()
# Define the app layout
app.layout = html.Div([
# Header with toggle button
html.Div([
html.H1("Animal Planet Dashboard",
id='main-title',
style={'text-align': 'center', 'color': '#2E8B57', 'margin-bottom': '30px', 'flex': '1'}),
# Theme toggle button in top right
html.Div([
html.Button(
"🌙",
id='theme-toggle',
style={
'background': 'none',
'border': '2px solid #2E8B57',
'border-radius': '50%',
'width': '50px',
'height': '50px',
'font-size': '20px',
'cursor': 'pointer',
'color': '#2E8B57'
}
)
], style={'position': 'absolute', 'top': '20px', 'right': '20px'})
], style={'position': 'relative', 'display': 'flex', 'align-items': 'center'}),
html.Div([
html.P(f"Total Animals in Dataset: {len(df)}",
id='total-count',
style={'text-align': 'center', 'font-size': '18px', 'margin-bottom': '20px'})
]),
# First row with bar charts
html.Div([
html.Div([
dcc.Graph(id='class-bar-chart')
], className='six columns'),
html.Div([
dcc.Graph(id='habitat-bar-chart')
], className='six columns'),
], className='row', style={'margin-bottom': '20px'}),
# Second row with diet chart and population chart
html.Div([
html.Div([
dcc.Graph(id='diet-bar-chart')
], className='six columns'),
html.Div([
html.H3("Population by Order", style={'text-align': 'center'}),
dcc.RadioItems(
id='chart-type-selector',
options=[
{'label': 'Pie Chart', 'value': 'pie'},
{'label': 'Line Chart', 'value': 'line'}
],
value='pie',
style={'text-align': 'center', 'margin-bottom': '10px'}
),
dcc.Graph(id='population-chart')
], className='six columns'),
], className='row'),
], id='main-container', style={'margin': '20px', 'transition': 'all 0.3s ease'})
# Callback for theme toggle
@app.callback(
[Output('main-container', 'style'),
Output('main-title', 'style'),
Output('total-count', 'style'),
Output('theme-toggle', 'children'),
Output('theme-toggle', 'style')],
[Input('theme-toggle', 'n_clicks')],
prevent_initial_call=False
)
def toggle_theme(n_clicks):
if n_clicks is None:
n_clicks = 0
is_dark = n_clicks % 2 == 1
if is_dark:
theme = DARK_THEME
toggle_icon = "☀️"
container_style = {
'margin': '20px',
'transition': 'all 0.3s ease',
'backgroundColor': theme['backgroundColor'],
'color': theme['color'],
'min-height': '100vh',
'padding': '10px'
}
title_style = {
'text-align': 'center',
'color': '#4CAF50',
'margin-bottom': '30px',
'flex': '1'
}
count_style = {
'text-align': 'center',
'font-size': '18px',
'margin-bottom': '20px',
'color': theme['color']
}
button_style = {
'background': 'none',
'border': '2px solid #4CAF50',
'border-radius': '50%',
'width': '50px',
'height': '50px',
'font-size': '20px',
'cursor': 'pointer',
'color': '#4CAF50'
}
else:
theme = LIGHT_THEME
toggle_icon = "🌙"
container_style = {
'margin': '20px',
'transition': 'all 0.3s ease',
'backgroundColor': theme['backgroundColor'],
'color': theme['color']
}
title_style = {
'text-align': 'center',
'color': '#2E8B57',
'margin-bottom': '30px',
'flex': '1'
}
count_style = {
'text-align': 'center',
'font-size': '18px',
'margin-bottom': '20px',
'color': theme['color']
}
button_style = {
'background': 'none',
'border': '2px solid #2E8B57',
'border-radius': '50%',
'width': '50px',
'height': '50px',
'font-size': '20px',
'cursor': 'pointer',
'color': '#2E8B57'
}
return container_style, title_style, count_style, toggle_icon, button_style
# Callback for class bar chart
@app.callback(
Output('class-bar-chart', 'figure'),
[Input('class-bar-chart', 'id'),
Input('theme-toggle', 'n_clicks')]
)
def update_class_chart(_, n_clicks):
if n_clicks is None:
n_clicks = 0
is_dark = n_clicks % 2 == 1
theme = DARK_THEME if is_dark else LIGHT_THEME
class_counts = df['class'].value_counts()
fig = px.bar(
x=class_counts.index,
y=class_counts.values,
title='Animals by Class',
labels={'x': 'Class', 'y': 'Count'},
color=class_counts.values,
color_continuous_scale='Viridis'
)
fig.update_layout(
title_x=0.5,
xaxis_tickangle=-45,
height=400,
paper_bgcolor=theme['backgroundColor'],
plot_bgcolor=theme['cardBackground'],
font={'color': theme['color']},
title_font={'color': theme['color']}
)
return fig
# Callback for habitat bar chart
@app.callback(
Output('habitat-bar-chart', 'figure'),
[Input('habitat-bar-chart', 'id'),
Input('theme-toggle', 'n_clicks')]
)
def update_habitat_chart(_, n_clicks):
if n_clicks is None:
n_clicks = 0
is_dark = n_clicks % 2 == 1
theme = DARK_THEME if is_dark else LIGHT_THEME
habitat_counts = df['habitat'].value_counts()
fig = px.bar(
x=habitat_counts.index,
y=habitat_counts.values,
title='Animals by Habitat',
labels={'x': 'Habitat', 'y': 'Count'},
color=habitat_counts.values,
color_continuous_scale='Plasma'
)
fig.update_layout(
title_x=0.5,
xaxis_tickangle=-45,
height=400,
paper_bgcolor=theme['backgroundColor'],
plot_bgcolor=theme['cardBackground'],
font={'color': theme['color']},
title_font={'color': theme['color']}
)
return fig
# Callback for diet bar chart
@app.callback(
Output('diet-bar-chart', 'figure'),
[Input('diet-bar-chart', 'id'),
Input('theme-toggle', 'n_clicks')]
)
def update_diet_chart(_, n_clicks):
if n_clicks is None:
n_clicks = 0
is_dark = n_clicks % 2 == 1
theme = DARK_THEME if is_dark else LIGHT_THEME
diet_counts = df['diet'].value_counts()
fig = px.bar(
x=diet_counts.index,
y=diet_counts.values,
title='Animals by Diet',
labels={'x': 'Diet', 'y': 'Count'},
color=diet_counts.values,
color_continuous_scale='Cividis'
)
fig.update_layout(
title_x=0.5,
height=400,
paper_bgcolor=theme['backgroundColor'],
plot_bgcolor=theme['cardBackground'],
font={'color': theme['color']},
title_font={'color': theme['color']}
)
return fig
# Callback for population chart (pie or line)
@app.callback(
Output('population-chart', 'figure'),
[Input('chart-type-selector', 'value'),
Input('theme-toggle', 'n_clicks')]
)
def update_population_chart(chart_type, n_clicks):
if n_clicks is None:
n_clicks = 0
is_dark = n_clicks % 2 == 1
theme = DARK_THEME if is_dark else LIGHT_THEME
# Group by order and sum population
order_population = df.groupby('order')['population'].sum().sort_values(ascending=False)
if chart_type == 'pie':
fig = px.pie(
values=order_population.values,
names=order_population.index,
title='Population Distribution by Order'
)
fig.update_traces(textposition='inside', textinfo='percent+label')
else: # line chart
fig = px.line(
x=range(len(order_population)),
y=order_population.values,
title='Population by Order (Line Chart)',
markers=True
)
fig.update_xaxes(
tickmode='array',
tickvals=list(range(len(order_population))),
ticktext=order_population.index,
tickangle=-45
)
fig.update_traces(mode='lines+markers')
fig.update_layout(
xaxis_title='Order',
yaxis_title='Population'
)
fig.update_layout(
title_x=0.5,
height=400,
paper_bgcolor=theme['backgroundColor'],
plot_bgcolor=theme['cardBackground'],
font={'color': theme['color']},
title_font={'color': theme['color']}
)
return fig
# Run the app
if __name__ == '__main__':
app.run(debug=True)