I’m trying to create an app here I can upload a csv file and that returns a scatter graph. On this scatter graph there will be a dropdown indicating No_of_Presyn and No_of_Postsyn, accompanied by Radio buttons to determine whether the x axis is linear of log. Finally, there will also be a slider button indicating the index (time) when this data was created.
So far I have:
import dash_html_components as html
import dash_core_components as dcc
import dash
import plotly
#import dash_table_experiments as dte
from dash.dependencies import Input, Output, State
import pandas as pd
import numpy as np
import json
import datetime
import operator
import os
import base64
import io
node_list = ['137de', '305b5', '8f6ee', '5c549', '948a3', 'a21aa', 'b98b4', 'c765c', '41583', 'a0dfa', '3c281', '36b02']
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config['suppress_callback_exceptions'] = True
colors = {
'background' : '#111111',
'text' : '#7FDBFF'
}
app.layout = html.Div([
html.H5('Upload Files'),
dcc.Upload(
id = 'upload-data',
children = html.Div([
'Drag and Drop or', html.A('Select Files')
]),
style = {
'width' : '100%',
'height' : '60px',
'lineHeight' : '60px',
'borderWidth' : '1px',
'borderStyle' : 'dashed',
'borderRadius' : '5px',
'textAlign' : 'center',
'margin' : '10px'
},
multiple = True),
html.Br(),
dcc.RadioItems(
id = 'xaxis-type',
options = [{'label' : i, 'value' : i} for i in ['Linear', 'Log']],
value = 'Linear',
labelStyle = {'display' : 'inline-block'}),
dcc.Dropdown(id = 'yaxis-column',
options = [{'label' : i, 'value' : i} for i in ['No_of_Presyn', 'No_of_Postsyn']],
value = 'No_of_Presyn',
placeholder = 'Filter Column'),
html.Br(),
#html.Div(dcc.Graph(id = 'sv_scatter')),
html.Div(id= 'output-data-upload'),
dcc.Slider(
id = 'node_slider',
min = 0,
max = 10,
value = 0,
marks = {str(i):str(i) for i in range(0,11)},
step = None
)
])
# Functions
# file upload function
def parse_contents(contents, filename):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return None
return df
# callback figure creation
@app.callback(Output('output-data-upload', ''),
[Input('upload-data', 'contents'),
#Input('upload-data', 'filename'),
Input('xaxis-type', 'value'),
Input('yaxis-column','value'),
Input('node_slider', 'value')],
[State('upload-data', 'filename')])
def update_output(list_of_contents, list_of_names, xaxis_type, yaxis_column, node_index):
if list_of_contents is not None:
df = parse_contents(list_of_contents, list_of_names)
children = [
parse_contents(c, n) for c, n in
zip(list_of_contents, list_of_names)]
if children is not None:
#print(children)
dff = df[df.node_index == node_index]
return{
'data' : [go.Scatter(
x = dff.supervoxel_sizes.values.tolist(),
y = dff.No_of_Presyn if 'Presyn' in yaxis_column else dff.No_of_Postsyn,
mode = 'markers',
marker = {
'size' : 15,
'opacity' : 0.5,
'line' : {'width' : 0.5, 'color' : 'white'}
}
)],
'layout' : go.Layout(
xaxis = {
'title' : 'SV_sizes',
'type' : 'linear' if xaxis_type == 'Linear' else 'log'
},
yaxis = {
'title' : yaxis_column
},
)
}
else:
return [{}]
else:
return [{}]
if __name__ == '__main__':
app.run_server(debug=True)
This code returns the upload button, dropdown, radio buttons and slider however when I try to upload data, I receive an error:
Traceback (most recent call last):
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/dash/dash.py", line 1152, in dispatch
response.set_data(self.callback_map[output]['callback'](*args))
File "/Users/markuswilliampleijzier/anaconda/lib/python3.6/site-packages/dash/dash.py", line 1038, in add_context
output_value = func(*args, **kwargs)
File "/Users/markuswilliampleijzier/Desktop/DASH_TIME_MACHINE/test_upload_v3.py", line 122, in update_output
df = parse_contents(list_of_contents, list_of_names)
File "/Users/markuswilliampleijzier/Desktop/DASH_TIME_MACHINE/test_upload_v3.py", line 90, in parse_contents
content_type, content_string = contents.split(',')
AttributeError: 'list' object has no attribute 'split'
Thanks for any help in advance!!!
M