I have a list of edges between several nodes of a graph as a .csv
file. I am reading the mentioned file and storing it in a Dash Store
component like below:
dataset = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
del dataset[dataset.columns[0]]
return html.Div(className="mx-auto text-center", children=[
dcc.Store(id="approach-1-dataset", data=dataset.to_dict('records'))]
Then using NetworkX
the graph is created after the user clicks on a button in the view like below:
@app.callback(Output('visualization-container', 'children'),
Input('visualize-button', 'n_clicks'),
State('dataset', 'data'))
def visualize_graph(n1,dataset):
if n1:
main_dataset = pd.DataFrame.from_dict(dataset)
pd.set_option('precision',10)
G = nx.from_pandas_edgelist(main_dataset, 'member1', 'member2', create_using = nx.Graph())
nodes = G.nodes()
degree = G.degree()
colors = [degree[n] for n in nodes]
size = [(degree[n]) for n in nodes]
pos = nx.kamada_kawai_layout(G)
pos = nx.spring_layout(G, k = 0.2)
cmap = plt.cm.viridis_r
cmap = plt.cm.Greys
fig = plt.figure(figsize = (15,9), dpi=100)
nx.draw(G,pos,alpha = 0.8, nodelist = nodes, node_color = colors, node_size = size , with_labels= False,font_size = 6, width = 0.2, cmap = cmap, edge_color ='yellow')
fig.set_facecolor('#0B243B')
return dcc.Graph(figure = fig)
return ""
Using this code I get the following error in my view:
Callback error updating visualization-container.children
dash.exceptions.InvalidCallbackReturnValue: The callback for
<Output
visualization-container.children>
returned a value having typeGraph
which is not JSON serializable. The value in question is either the only value returned, or is in the top level of the returned list, and has string representationGraph(figure=<Figure size 1500x900 with 1 Axes>)
In general, Dash properties can only be dash components, strings, dictionaries, numbers, None, or lists of those.
And this error in my console:
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
It is worth mentioning that the same code works pretty fine when I run it directly in a Jupyter notebook but when I try to run it in a dash call back and return the result as a dcc.Graph
component I get the errors.
How can I solve this issue?
My .csv
file looks like below:
member1,member2,weight
200114206,3949436,1
217350178,8539046,1
.
.
.
193986670,8539046,2