i am trying to upload two files simultaneously, put them in two variables (df and dp) and plot a graph using update_graph from the values from the df and dp. But i cant seem to do it. it doesnt show any graph. Here is the code below. I am just learning dash.
import base64
import datetime
import io
import plotly.graph_objects as go
import cufflinks as cf
from datetime import datetime
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]
#dp= pd.read_csv(‘formatted.csv’)
app = dash.Dash(name, external_stylesheets=external_stylesheets)
server = app.server
colors = {
“graphBackground”: “#F5F5F5”,
“background”: “#ffffff”,
“text”: “#000000”
}
app.layout = html.Div([
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’
},
# Allow multiple files to be uploaded
multiple=True
),
dcc.Graph(id=‘Mygraph’),
html.Div(id=‘output-data-upload’)
])
def parse_data(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 or TXT file
if filename == "all.csv":
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
if filename == "formatted.csv":
dp = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
#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))
elif 'txt' or 'tsv' in filename:
# Assume that the user upl, delimiter = r'\s+'oaded an excel file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')), delimiter = r'\s+')
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
return df
@app.callback(Output(‘Mygraph’, ‘figure’),
[
Input(‘upload-data’, ‘contents’),
Input(‘upload-data’, ‘filename’)
])
def update_graph(contents, filename):
“”“fig = {
‘layout’: go.Layout(
plot_bgcolor=colors[“graphBackground”],
paper_bgcolor=colors[“graphBackground”])
}”""
if contents:
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
df = df.set_index(df.columns[0])
i = df['Date']
f = []
for a in i:
f.append(datetime.strptime(a,'%d.%m.%Y').strftime('%Y-%m-%d'))
figure = {'data':[{'x':f,
'y':df.Opening,
'name':'Open'},
{'x':f,
'y':df.Close,
'name':'Close'},
go.Scatter(
x=dp['Dates'],
y=dp['Price'],
name='transactions')],
'layout':{'title':'Visual'}
}
#if filename == "all.csv":
#figure= df.iplot(asFigure=True, x='Date', y=['Close', 'Opening'], xTitle='Dates', yTitle='Currency(USD)')
return (figure)
@app.callback(Output(‘output-data-upload’, ‘children’),
[
Input(‘upload-data’, ‘contents’),
Input(‘upload-data’, ‘filename’)
])
def update_table(contents, filename):
table = html.Div()
if contents:
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
table = html.Div([
html.H5(filename),
dash_table.DataTable(
data=df.to_dict('rows'),
columns=[{'name': i, 'id': i} for i in df.columns]
),
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents[0:200] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])
return table
if name == ‘main’:
app.run_server(debug=True)