This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
179 lines (157 loc) · 6.16 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
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
import altair as alt
import vega_datasets
app = dash.Dash(__name__, assets_folder='assets')
app.config['suppress_callback_exceptions'] = True
server = app.server
app.title = 'Dash app with pure Altair HTML'
def make_plot(x_axis = 'Displacement',
y_axis = 'Cylinders'):
# Don't forget to include imports
def mds_special():
font = "Arial"
axisColor = "#000000"
gridColor = "#DEDDDD"
return {
"config": {
"title": {
"fontSize": 24,
"font": font,
"anchor": "start", # equivalent of left-aligned.
"fontColor": "#000000"
},
'view': {
"height": 300,
"width": 400
},
"axisX": {
"domain": True,
#"domainColor": axisColor,
"gridColor": gridColor,
"domainWidth": 1,
"grid": False,
"labelFont": font,
"labelFontSize": 12,
"labelAngle": 0,
"tickColor": axisColor,
"tickSize": 5, # default, including it just to show you can change it
"titleFont": font,
"titleFontSize": 16,
"titlePadding": 10, # guessing, not specified in styleguide
"title": "X Axis Title (units)",
},
"axisY": {
"domain": False,
"grid": True,
"gridColor": gridColor,
"gridWidth": 1,
"labelFont": font,
"labelFontSize": 14,
"labelAngle": 0,
#"ticks": False, # even if you don't have a "domain" you need to turn these off.
"titleFont": font,
"titleFontSize": 16,
"titlePadding": 10, # guessing, not specified in styleguide
"title": "Y Axis Title (units)",
# titles are by default vertical left of axis so we need to hack this
#"titleAngle": 0, # horizontal
#"titleY": -10, # move it up
#"titleX": 18, # move it to the right so it aligns with the labels
},
}
}
# register the custom theme under a chosen name
alt.themes.register('mds_special', mds_special)
# enable the newly registered theme
alt.themes.enable('mds_special')
#alt.themes.enable('none') # to return to default
typeDict = {'Displacement':'quantitative',
'Cylinders':'quantitative',
'Miles_per_Gallon':'quantitative'
}
# Create a plot from the cars dataset
chart = alt.Chart(vega_datasets.data.cars.url).mark_point(size=90).encode(
alt.X(x_axis,type='quantitative', title=x_axis),
alt.Y(y_axis, type='quantitative', title=y_axis),
tooltip = [{"type":typeDict[x_axis], "field":x_axis},
'Horsepower:Q',]
).properties(title='Horsepower vs. Displacement',
width=500, height=350).interactive()
return chart
app.layout = html.Div([
html.Div(
className="app-header",
children=[
html.Div('Plotly Dash', className="app-header--title")
]
),
### Add Tabs to the top of the page
dcc.Tabs(id='tabs', value='tab1', children=[
dcc.Tab(label='Lecture 1', value='tab-1'),
dcc.Tab(label='Lecture 2', value='tab-2'),
dcc.Tab(label='Lecture 3', value='tab-3'),
dcc.Tab(label='Lecture 4', value='tab-4'),
]),
### ADD CONTENT HERE like: html.H1('text'),
html.H3('Here is our first plot:'),
html.Iframe(
sandbox='allow-scripts',
id='plot',
height='500',
width='1000',
style={'border-width': '0'},
################ The magic happens here
srcDoc=make_plot().to_html()
################ The magic happens here
),
# Just to add some space
html.Iframe(height='200', width='10',style={'border-width': '0'}),
html.H3('Dropdown to control Altair Chart'),
dcc.Dropdown(
id='dd-chart',
options=[
{'label': 'Fuel efficiency', 'value': 'Miles_per_Gallon'},
{'label': 'Cylinders', 'value': 'Cylinders'},
{'label': 'Engine Displacement', 'value': 'Displacement'}
],
value='Displacement',
style=dict(width='45%',
verticalAlign="middle"
)
),
# Just to add some space
html.Iframe(height='200', width='10',style={'border-width': '0'}),
dcc.Dropdown(
id='dd-chart-y',
options=[
{'label': 'Fuel efficiency', 'value': 'Miles_per_Gallon'},
{'label': 'Cylinders', 'value': 'Cylinders'},
{'label': 'Engine Displacement', 'value': 'Displacement'}
],
value='Displacement',
style=dict(width='45%',
verticalAlign="middle"
)
),
# Just to add some space
html.Iframe(height='200', width='10',style={'border-width': '0'})
])
# This second callback tells Dash the output is the `plot` IFrame; srcDoc is a
# special property that takes in RAW html as an input and renders it
# As input we take in the values from second dropdown we created (dd-chart)
# then we run update_plot
@app.callback(
dash.dependencies.Output('plot', 'srcDoc'),
[dash.dependencies.Input('dd-chart', 'value'),
dash.dependencies.Input('dd-chart-y', 'value')])
def update_plot(xaxis_column_name,
yaxis_column_name):
updated_plot = make_plot(xaxis_column_name,
yaxis_column_name).to_html()
return updated_plot
if __name__ == '__main__':
app.run_server(debug=True)