Hello,
I’m new to dash.I’m trying to upload a file and do some analysis using that file in callback functions. In one callback function I’ve uploaded that file and returned the json format of that file to one particular id (i.e intermediate-value). But when I try to access the same json data in another callback,it is throwing an error (TypeError: the JSON object must be str, bytes or bytearray, not NoneType). Please provide me some help. Here is my code.
I’m getting error in my last callback function
import dash
import dash_core_components as dcc
import dash_html_components as html
import base64
import json
import io
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import time
from dash.dependencies import Input, Output
import dash_table as dt
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
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
),
html.Div(id='intermediate-value',style={'display':'none'}),
html.Div(id='output-data-upload'),
html.Div(id='conf_matrix')
])
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
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 uploaded an txt or tsv 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('intermediate-value','children'),
[
Input('upload-data', 'contents'),
Input('upload-data', 'filename')
])
def update_table(contents, filename):
#table = html.Div()
#print("flag 0")
if contents:
print("flag 0")
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
df=pd.DataFrame(df)
df =df.convert_dtypes()
return df.to_json(orient='split')
@app.callback(Output('output-data-upload','children'),
[
Input('upload-data', 'contents'),
Input('upload-data', 'filename')
])
def update_table(contents, filename):
#table = html.Div()
#print("flag 0")
if contents:
print("flag 0")
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
df=pd.DataFrame(df)
df =df.convert_dtypes()
def chkd(col):
if col.dtype== 'float64' or col.dtype == 'Int64':
return 'Continuous'
else:
return 'Categorical'
df_tab=pd.DataFrame(df.describe(include='all')).stack().unstack(0)
try:
df_tab['cnt_null'] = df.apply(lambda col: col.isnull().sum())
df_tab['null_%'] = df_tab.apply(lambda row: round((row['cnt_null']/(row['cnt_null']+row['count']))*100,2), axis=1)
df_tab['cnt_zeros'] = df.isin([0]).sum(axis=0)
df_tab['Data_Type'] = df.dtypes
df_tab['variance']=df.var(axis=0)
df_tab['mode'] = df.mode().iloc[0,:]
df_tab['1%'] = df.quantile(0.01)
df_tab['5%'] = df.quantile(0.05)
df_tab['10%'] = df.quantile(0.1)
df_tab['90%'] = df.quantile(0.9)
df_tab['95%'] = df.quantile(0.95)
df_tab['99%'] = df.quantile(0.99)
df_tab['skew']=df.skew(axis=0)
df_tab['kurtosis']=df.kurt(axis=0)
df_tab['Variable_Type'] = df.apply(lambda col: 'Categorical' if col.dtype=='object' else chkd(col))
except TypeError:
pass
df_tab=df_tab.reset_index(level=0)
df_tab.rename(columns={'50%':'median'},inplace=True)
df_tab.rename(columns={'count':'cnt_non_null'},inplace=True)
df_tab.rename(columns={'index':'Variable'},inplace=True)
df_tab=df_tab[['Variable','Variable_Type','Data_Type','cnt_non_null','cnt_null','null_%','cnt_zeros','mean','std','mode','min','1%','5%','10%','25%','median','75%','90%','95%','99%','max','variance','skew','kurtosis']]
#cols=[{'name': i, 'id': i} for i in df_tab.columns]
df_tab = df_tab.sort_values(by ='Variable_Type',ascending=False)
print("wait for 3 sec")
time.sleep(3)
df_tab.to_csv('univariate.csv')
new_df = pd.read_csv('univariate.csv')
table = html.Div([
html.H5(filename),
dt.DataTable(
data=new_df.to_dict('records'),
columns=[{'name': i, 'id': i} for i in df_tab.columns]
),
])
return table
#
@app.callback(Output('conf_matrix', 'children'), [Input('intermediate-value', 'children')])
def update_graph(jsonified_cleaned_data):
# more generally, this line would be
# json.loads(jsonified_cleaned_data)
dff =json.loads(jsonified_cleaned_data)
return str(dff)
if __name__ == '__main__':
app.run_server(debug=True)