首页 > 解决方案 > 如何在 python dash_cytoscape 中设置同心圆布局参数?

问题描述

我定义了我的元素,比如 level 指的是我想要那个节点的圆圈层。

 elements = [{'data': {'id': 'a', 'label': 'a', 'level': 1},
             {'data': {'id': 'b', 'label': 'b', 'level': 1},
             {'data': {'id': 'c', 'label': 'c', 'level': 2},
             .......]

我尝试了以下不起作用(错误加载布局)

app.layout = html.Div([
    cyto.Cytoscape(
        id='cytoscape',
        elements= elements,
        layout={'name': 'concentric', 'concentric': lambda x: x['data']['level']}
    )
])

javascript 文档在这里https://js.cytoscape.org/#layouts并指定

let options = {
  name: 'concentric',
  concentric: function( node ){ // returns numeric value for each node, placing higher nodes in levels towards the centre
  return node.degree();
  }

如何设置这个同心参数?

干杯

标签: pythonplotly-dashcytoscape

解决方案


不幸的是,无法将 python 函数传递layout给组件中的参数cyto.Cytoscape。这是因为该函数不会被转译为 JS 函数,而这正是concentric密钥在内部工作所需要的options

替代方案是:

  1. 使用 python 网络库(例如 networkX)基于同心算法分配 x/y 位置,然后使用 dash-cytoscape 中的自定义布局(而不是同心布局)返回确切位置。
  2. 修改您想要用于同心布局cyto.Cytoscape的 JS 的源代码和硬编码。function您需要按照贡献说明来构建和创建自定义安装。

推荐阅读