-
Notifications
You must be signed in to change notification settings - Fork 56
/
app.py
86 lines (74 loc) · 2.61 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
######### Import your libraries #######
import dash
from dash import html
from dash import dcc
from dash.dependencies import Input, Output, State
import pandas as pd
import plotly as py
import plotly.graph_objs as go
###### Define your variables #####
tabtitle = 'Titanic!'
color1='#92A5E8'
color2='#8E44AD'
color3='#FFC300'
sourceurl = 'https://www.kaggle.com/c/titanic'
githublink = 'https://github.com/plotly-dash-apps/304-titanic-dropdown'
###### Import a dataframe #######
df = pd.read_csv("https://raw.githubusercontent.com/austinlasseter/plotly_dash_tutorial/master/00%20resources/titanic.csv")
df['Female']=df['Sex'].map({'male':0, 'female':1})
df['Cabin Class'] = df['Pclass'].map({1:'first', 2: 'second', 3:'third'})
variables_list=['Survived', 'Female', 'Fare', 'Age']
########### Initiate the app
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.title=tabtitle
####### Layout of the app ########
app.layout = html.Div([
html.H3('Choose a continuous variable for summary statistics:'),
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in variables_list],
value=variables_list[0]
),
html.Br(),
dcc.Graph(id='display-value'),
html.A('Code on Github', href=githublink),
html.Br(),
html.A("Data Source", href=sourceurl),
])
######### Interactive callbacks go here #########
@app.callback(Output('display-value', 'figure'),
[Input('dropdown', 'value')])
def display_value(continuous_var):
grouped_mean=df.groupby(['Cabin Class', 'Embarked'])[continuous_var].mean()
results=pd.DataFrame(grouped_mean)
# Create a grouped bar chart
mydata1 = go.Bar(
x=results.loc['first'].index,
y=results.loc['first'][continuous_var],
name='First Class',
marker=dict(color=color1)
)
mydata2 = go.Bar(
x=results.loc['second'].index,
y=results.loc['second'][continuous_var],
name='Second Class',
marker=dict(color=color2)
)
mydata3 = go.Bar(
x=results.loc['third'].index,
y=results.loc['third'][continuous_var],
name='Third Class',
marker=dict(color=color3)
)
mylayout = go.Layout(
title='Grouped bar chart',
xaxis = dict(title = 'Port of Embarkation'), # x-axis label
yaxis = dict(title = str(continuous_var)), # y-axis label
)
fig = go.Figure(data=[mydata1, mydata2, mydata3], layout=mylayout)
return fig
######### Run the app #########
if __name__ == '__main__':
app.run_server(debug=True)