Dash_table: SyntaxError: positional argument follows keyword argument

Firstly I can see there is a question with a similar title here but I don’t think it answers my specific question.

I have this data frame (called final_net):

   Entrez Gene Interactor A  Entrez Gene Interactor B
0                      6840                      7431
1                      6640                      5217
2                       823                      7431
3                     57019                     57019

If I convert that data frame to this kind of list:

dash_elements = []

for index,i in final_net.iterrows():
    dict1 = {}
    dict2 = {}
    dict1['data'] = dict2
    dict2['id'] = str(i[0])
    dict2['label'] = str(i[0])
    
    dict3 = {}
    dict1['data'] = dict3
    dict3['id'] = str(i[1])
    dict3['label'] = str(i[1])
    
    final_dict2 = {}
    final_dict3 = {}
    final_dict2['data'] = dict2
    final_dict3['data'] = dict3
    
    dash_elements.append(final_dict2)
    dash_elements.append(final_dict3)
    
  
    dict4 = {}
    final_dict4 = {}
    final_dict4['data'] = dict4
    dict4['source'] = str(i[0])
    dict4['target'] = str(i[1])
    dash_elements.append(final_dict4)
    

print(dash_elements)

And then read the data into dash like this:

import dash
import dash_core_components as dcc
from dash import html
import dash_cytoscape as cyto
from dash.dependencies import Input, Output
import plotly.express as px

app = dash.Dash(__name__)

app.layout = html.Div([
    html.P("Dash Cytoscape:"),
    cyto.Cytoscape(
        id='cytoscape',
        elements = dash_elements,
        layout={'name': 'breadthfirst'},
        style={'width': '1000px', 'height': '1000px'}
    )
])

if __name__ == '__main__':
   app.run_server()

A network is generated as expected.

I’m wondering if this could be done more elegantly by reading the data frame directly into the network.

I wanted to implement the answer described in this link so I wrote:

from dash import dash_table



dt_col_param = []
for col in final_net.columns:
    dt_col_param.append({"name": str(col), "id": str(col)})


import dash
import dash_core_components as dcc
from dash import html
import dash_cytoscape as cyto
from dash.dependencies import Input, Output
import plotly.express as px

app = dash.Dash(__name__)

app.layout = html.Div([
    html.P("Dash Cytoscape:"),
    cyto.Cytoscape(
        id='cytoscape',
        
       dash_table.DataTable(
        columns=dt_col_param,
        data=final_net.to_dict()),

        layout={'name': 'breadthfirst'},
        style={'width': '1000px', 'height': '1000px'}
    )
])

if __name__ == '__main__':
   app.run_server()

But it returns the error:

 File "<ipython-input-93-2fb47236fbc6>", line 31
    dash_table.DataTable(
    ^
SyntaxError: positional argument follows keyword argument

And i don’t understand what I’m doing wrong. Could someone explain/show me how to correct this please?

I’m not too familiar with Cytoscapes, but I know you definitely can not give a DataTable as an argument to a Cytoscape. What you’re trying to do won’t ever work, sorry. DataTables are only for viewing data.

Using .to_dict(orient='records') does convert your dataframe into a list of dicts (as seen in the stackoverflow post)… but the dicts are not in the particular format Cytoscape requires. Again I think this is the wrong path to pursue, but someone can correct me. Cytoscape requires a list of dicts in a very particular format.

That said, here is a more elegant way to create your dash_elements list, if you’re interested:

dash_elements = []
for _,row in final_net.iterrows():
    dash_elements.append({'data':{'id':row[0], 'label':row[0]}})
    dash_elements.append({'data':{'id':row[1], 'label':row[1]}})
    dash_elements.append({'data':{'source':row[0], 'target':row[1]}})
1 Like

thanks so much for the help.

1 Like