Hello I’m trying to make an app to which it provides a csv file and treats the data to be able to visualize them, at first that treatment of the data was doing within the function to graph (display_graph), but since I want to perform some interactive actions with the calculations, I tried to get the function to graph to later weigh to the function “display_graph” only the data to do, but loading dash reproduce me this error.
This is my code:
@app.callback(Output('datatable-upload-matrix', 'data'),
[Input('calculate-button', 'n_clicks'),
Input('datatable-upload-vectors', 'data')],
[State('table', 'data'),
State('graphics-algo-selector', 'value'),
State('vector-norm', 'value'),
State('vector-choose', 'value'),
State('dropdown_table_filterColumn','value')])
def calc_matrix(n_clicks, data, V_r, graphics_algo, vector_norm, vector_choose, selected):
if n_clicks < 1:
P=[]
print("Didn't calculate anything")
return P
else:
df = pd.DataFrame(data)
def get_index(name):
'''
return the index of a column name
'''
for column in df.columns:
# print(f"{selected2} + {df.columns.get_loc(selected2)}")
if column == name:
index = df.columns.get_loc(column)
# print(index)
return index
result=[]
for i in range(len(selected)):
X = get_index(selected[i])
result.append(X)
df = df[df.columns[result]]
x = df.values
x = (x).astype(float)
n_components = len(selected)
k = get_index(vector_choose)
W = np.identity(n_components)
#Estandarizamos los datos y se calcula la matriz traspuesta.
X_std = stats.zscore(x)
if graphics_algo == "RadViz":
P = mapping('RadViz', X_std, V_r, W, 1, k)
elif graphics_algo == "SC":
P = mapping('SC', X_std, V_r, W, 1, k)
elif graphics_algo == "Adaptable":
if vector_norm == 1:
P = mapping('Adaptable', X_std, V_r, W, 1, k)
elif vector_norm == 'Inf':
P = mapping('Adaptable', X_std, V_r, W, 'Inf', k)
elif graphics_algo == "Adaptable exact":
if vector_norm == 1:
P = mapping('Adaptable exact', X_std, V_r, W, 1, k)
elif vector_norm == 2:
P = mapping('Adaptable exact', X_std, V_r, W, 2, k)
elif vector_norm == 'Inf':
P = mapping('Adaptable exact', X_std, V_r, W, 'Inf', k)
elif graphics_algo == "Adaptable ordered":
if vector_norm == 1:
P = mapping('Adaptable ordered', X_std, V_r, W, 1, k)
elif vector_norm == 2:
P = mapping('Adaptable ordered', X_std, V_r, W, 2, k)
elif vector_norm == 'Inf':
P = mapping('Adaptable ordered', X_std, V_r, W, 'Inf', k)
return P
@app.callback(Output('datatable-upload-graph', 'figure'),
[Input('calculate-button', 'n_clicks')],
[State('datatable-upload-vectors', 'data'),
State('datatable-upload-matrix', 'data'),
State('table', 'data')])
def display_graph(n_clicks, V_r, P):
if n_clicks < 1:
print("Didn't calculate anything")
return {
'data': [
go.Scatter(
x=[],
y=[],
mode='markers',
)]
}
else:
#Convertimos los datos adecuadamente para poder representarlos.
r = sorted(abs(np.squeeze(np.asarray(P[:,0])))) #Pasamos las columnas a un array
theta = (np.squeeze(np.asarray(P[:,1]))) #Convertimos los radianes a grados sexagesimales.
V_r[:,0] = abs(V_r[:,0])
V_r[:,1] = V_r[:,1]
x_graph = []
y_graph = []
for i in range(len(r)):
x = r[i]*math.cos(theta[i])
y = r[i]*math.sin(theta[i])
x_graph.append(x)
y_graph.append(y)
V_xgraph = []
V_ygraph = []
for i in range(len(V_r)):
x = V_r[i,0]*math.cos(V_r[i,1])
y = V_r[i,0]*math.sin(V_r[i,1])
V_xgraph.append(x)
V_ygraph.append(y)
if P==[]:
raise dash.exceptions.PreventUpdate()
else:
trace1 = go.Scatter(
x = x_graph,
y = y_graph,
mode='markers',
marker=dict(
size=12,
opacity=1
)
)
vectors=[]
for i in range(len(V_r)):
layout = {
'type': 'line',
#"xref": "x",
#"yref": "y",
'x0': 0,
'x1': V_xgraph[i],
'y0': 0,
'y1': V_ygraph[i],
'line': {
'width': 4,
'color': 'rgb(30, 30, 30)'
}
}
vectors.append(layout)
return {
'data': [trace1],
'layout': {"shapes": vectors}
}
This error is only reproduced if the display_graph function is passed on the calc_matrix data.