Dash_table: SyntaxError: positional argument follows keyword argument
Dash_table: SyntaxError: positional argument follows keyword argument
我想用 dash 搭建一个网络。
我有这个数据框(称为final_net):
Entrez Gene Interactor A Entrez Gene Interactor B
0 6840 7431
1 6640 5217
2 823 7431
3 57019 57019
如果我将该数据框转换为这种列表:
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)
然后像这样将数据读入破折号:
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()
网络按预期生成。
我想知道是否可以通过将数据帧直接读入网络来更优雅地完成此操作。
我想实现答案,所以我写道:
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()
但是returns错误:
File "<ipython-input-93-2fb47236fbc6>", line 31
dash_table.DataTable(
^
SyntaxError: positional argument follows keyword argument
而且我不明白我做错了什么。请问有人 explain/show 我该如何纠正这个问题?
好吧,正如错误所说,您忘记了参数的关键字(即“元素”):
app.layout = html.Div(
[
html.P("Dash Cytoscape:"),
cyto.Cytoscape(
id="cytoscape",
elements=dash_table.DataTable(
columns=dt_col_param, data=final_net.to_dict()
),
layout={"name": "breadthfirst"},
style={"width": "1000px", "height": "1000px"},
),
]
)
但这也不太奏效,因为对于 cyto.Cytoscape
对象,这不是 elements
的有效输入 — 这需要
"A list of dictionaries representing the elements of the networks."
如果我有时间,我会回到这个问题并提供更完整的答案,但希望这可能足以帮助您了解如何实施....
我想用 dash 搭建一个网络。
我有这个数据框(称为final_net):
Entrez Gene Interactor A Entrez Gene Interactor B
0 6840 7431
1 6640 5217
2 823 7431
3 57019 57019
如果我将该数据框转换为这种列表:
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)
然后像这样将数据读入破折号:
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()
网络按预期生成。
我想知道是否可以通过将数据帧直接读入网络来更优雅地完成此操作。
我想实现答案
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()
但是returns错误:
File "<ipython-input-93-2fb47236fbc6>", line 31
dash_table.DataTable(
^
SyntaxError: positional argument follows keyword argument
而且我不明白我做错了什么。请问有人 explain/show 我该如何纠正这个问题?
好吧,正如错误所说,您忘记了参数的关键字(即“元素”):
app.layout = html.Div(
[
html.P("Dash Cytoscape:"),
cyto.Cytoscape(
id="cytoscape",
elements=dash_table.DataTable(
columns=dt_col_param, data=final_net.to_dict()
),
layout={"name": "breadthfirst"},
style={"width": "1000px", "height": "1000px"},
),
]
)
但这也不太奏效,因为对于 cyto.Cytoscape
对象,这不是 elements
的有效输入 — 这需要
"A list of dictionaries representing the elements of the networks."
如果我有时间,我会回到这个问题并提供更完整的答案,但希望这可能足以帮助您了解如何实施....