Skip to content

Instantly share code, notes, and snippets.

@bpostlethwaite
Last active September 25, 2018 19:18
Show Gist options
  • Save bpostlethwaite/96c37d411e4db3c221b34ca4ab21fe9d to your computer and use it in GitHub Desktop.
Save bpostlethwaite/96c37d411e4db3c221b34ca4ab21fe9d to your computer and use it in GitHub Desktop.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import csv
import random
def get_data():
with open("iris.csv") as csvfile:
reader = csv.reader(csvfile)
csv_data = [r for r in reader]
csv_data.pop(0)
# clean data - convert numbers to types
for i in range(len(csv_data)):
row = csv_data[i]
for j in range(len(row)):
try:
csv_data[i][j] = float(csv_data[i][j])
except:
pass
return csv_data
def jitter_data(csv_data):
for i in range(len(csv_data)):
row = csv_data[i]
for j in range(len(row)):
if type(csv_data[i][j]) == float:
csv_data[i][j] = csv_data[i][j] * random.random() * 2
return csv_data
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.Button(id="update-btn", children="update"),
dcc.Graph(id="graph-1"),
dcc.Graph(id="graph-2"),
dcc.Graph(id="graph-3"),
]
)
def create_groups(data):
groups = {}
for i in range(len(data)):
row = data[i]
species = row[4]
if species not in groups:
groups[species] = {"x": [], "y": []}
groups[species]["x"].append(row[0])
groups[species]["y"].append(row[1])
return groups
@app.callback(Output("graph-1", "figure"), [Input("update-btn", "n_clicks")])
def update_figure(n_clicks):
group_name = "Setosa"
new_data = jitter_data(get_data())
groups = create_groups(new_data)
traces = []
group = groups[group_name]
traces.append(
{
"type": "scatter",
"mode": "markers",
"name": group_name,
"x": group["x"],
"y": group["y"],
}
)
return {
"data": traces,
"layout": {
"title": group_name,
"xaxis": {"title": "Sepal.Length"},
"yaxis": {"title": "Sepal.Width"},
},
}
@app.callback(Output("graph-2", "figure"), [Input("update-btn", "n_clicks")])
def update_figure(n_clicks):
group_name = "Virginica"
new_data = jitter_data(get_data())
groups = create_groups(new_data)
traces = []
group = groups[group_name]
traces.append(
{
"type": "scatter",
"mode": "markers",
"name": group_name,
"x": group["x"],
"y": group["y"],
}
)
return {
"data": traces,
"layout": {
"title": group_name,
"xaxis": {"title": "Sepal.Length"},
"yaxis": {"title": "Sepal.Width"},
},
}
@app.callback(Output("graph-3", "figure"), [Input("update-btn", "n_clicks")])
def update_figure(n_clicks):
group_name = "Versicolor"
new_data = jitter_data(get_data())
groups = create_groups(new_data)
traces = []
group = groups[group_name]
traces.append(
{
"type": "scatter",
"mode": "markers",
"name": group_name,
"x": group["x"],
"y": group["y"],
}
)
return {
"data": traces,
"layout": {
"title": group_name,
"xaxis": {"title": "Sepal.Length"},
"yaxis": {"title": "Sepal.Width"},
},
}
if __name__ == "__main__":
app.run_server(debug=True)
dash
dash_core_components
dash_html_components
requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment