Skip to content

Instantly share code, notes, and snippets.

@bpostlethwaite
Created September 25, 2018 20:03
Show Gist options
  • Save bpostlethwaite/db822baf14d5d6d311d1e865b0a953e5 to your computer and use it in GitHub Desktop.
Save bpostlethwaite/db822baf14d5d6d311d1e865b0a953e5 to your computer and use it in GitHub Desktop.
Sharing state between callbacks solution
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
import json
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"),
html.Div(id="intermediate-value", style={"display": "none"}),
]
)
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("intermediate-value", "children"), [Input("update-btn", "n_clicks")]
)
def update_btn(n_clicks):
new_data = jitter_data(get_data())
return json.dumps(new_data)
@app.callback(Output("graph-1", "figure"), [Input("intermediate-value", "children")])
def update_figure(str_data):
group_name = "Setosa"
new_data = json.loads(str_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("intermediate-value", "children")])
def update_figure(str_data):
group_name = "Virginica"
new_data = json.loads(str_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("intermediate-value", "children")])
def update_figure(str_data):
group_name = "Versicolor"
new_data = json.loads(str_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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment