-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDV0101EN-Final_Assign_Part_2_Questions.py
112 lines (92 loc) · 4.7 KB
/
DV0101EN-Final_Assign_Part_2_Questions.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
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# Load the data using pandas
data = pd.read_csv('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/historical_automobile_sales.csv')
# Initialize the Dash app
app = dash.Dash(__name__)
# Create the dropdown menu options
dropdown_options = [
{'label': 'Yearly Statistics', 'value': 'Yearly Statistics'},
{'label': 'Recession Period Statistics', 'value': 'Recession Period Statistics'}
]
# List of years
year_list = [i for i in range(1980, 2024, 1)]
# Create the layout of the app
app.layout = html.Div([
html.H1("Automobile Sales Statistics Dashboard", style={'color': '#503D36', 'fontSize': 24}),
html.Div([
html.Label("Select Statistics:"),
dcc.Dropdown(
id='dropdown-statistics',
options=dropdown_options,
value='Yearly Statistics',
placeholder='Select a report type.',
style={
'width': '80%',
'padding': 3,
'font-size': 20,
'text-align-last': 'center'
}
)
]),
html.Div([
dcc.Dropdown(
id='select-year',
options=[{'label': i, 'value': i} for i in year_list],
value='Yearly Statistics',
disabled=True
)
]),
html.Div(id='output-container', className='chart-grid', style={'display': 'flex'})
])
# Callback for disabling year dropdown when selecting 'Yearly Statistics'
@app.callback(
Output(component_id='select-year', component_property='disabled'),
Input(component_id='dropdown-statistics', component_property='value')
)
def update_input_container(value):
if value == 'Yearly Statistics':
return False
else:
return True
# Callback for updating output container based on selected statistics
@app.callback(
Output(component_id='output-container', component_property='children'),
[Input(component_id='select-year', component_property='value'), Input(component_id='dropdown-statistics', component_property='value')]
)
def update_output_container(input_year, selected_statistics):
if selected_statistics == 'Recession Period Statistics':
recession_data = data[data['Recession'] == 1]
yearly_rec = recession_data.groupby('Year')['Automobile_Sales'].mean().reset_index()
R_chart1 = dcc.Graph(figure=px.line(yearly_rec, x='Year', y='Automobile_Sales', title="Average Automobile Sales fluctuation over Recession Period"))
average_sales = recession_data.groupby('Vehicle_Type')['Automobile_Sales'].mean().reset_index()
R_chart2 = dcc.Graph(figure=px.bar(average_sales, x='Vehicle_Type', y='Automobile_Sales'))
exp_rec = recession_data.groupby('Vehicle_Type')['Advertising_Expenditure'].sum()
R_chart3 = dcc.Graph(figure=px.pie(exp_rec, names=exp_rec.index, values='Advertising_Expenditure'))
return [
html.Div(className='chart-item-2', children=[html.Div(children=R_chart1), html.Div(children=R_chart2)], style={'margin': '10px'}),
html.Div(className='chart-item-2', children=[html.Div(children=R_chart3)], style={'margin': '10px'})
]
elif input_year and selected_statistics == 'Yearly Statistics':
yearly_data = data[data['Year'] == input_year]
yas = data.groupby('Year')['Automobile_Sales'].mean().reset_index()
Y_chart1 = dcc.Graph(figure=px.line(yas, x='Year', y='Automobile_Sales', title='Yearly Automobile Sales'))
Y_chart2 = dcc.Graph(figure=px.line(yearly_data, x='Month', y='Automobile_Sales', title='Monthly Automobile Sales'))
avr_vdata = yearly_data.groupby('Vehicle_Type')['Automobile_Sales'].mean().reset_index()
Y_chart3 = dcc.Graph(figure=px.bar(avr_vdata, x='Vehicle_Type', y='Automobile_Sales', title='Average Vehicles Sold by Vehicle Type'))
exp_data = yearly_data.groupby('Vehicle_Type')['Advertising_Expenditure'].sum().reset_index()
Y_chart4 = dcc.Graph(figure=px.pie(exp_data, names='Vehicle_Type', values='Advertising_Expenditure', title='Total Advertisement Expenditure by Vehicle Type'))
return [
html.Div(className='chart-item', children=[html.Div(children=Y_chart1), html.Div(children=Y_chart2)], style={'margin': '10px'}),
html.Div(className='chart-item', children=[html.Div(children=Y_chart3), html.Div(children=Y_chart4)], style={'margin': '10px'})]
else:
return None
# Run the Dash app
if __name__ == '__main__':
app.run_server(debug=True)