有向图箭头消失的 ipycytoscape 布局

layout of ipycytoscape for directed graphs arrow disapearing

我有以下代码用 ipycytoscape 生成和绘制图表:

from ipycytoscape import CytoscapeWidget
import networkx as nx
cyto = CytoscapeWidget()
nodes = [1,2,3,4,5,6,7,8]
children = [[2],[3,4],[5,6],[6,7],[8],[],[8],[]]
G2 = nx.Graph()
G2.add_nodes_from(nodes)
for i,child in enumerate(children):
    for c in child:
        G2.add_edge(i+1, c)

cyto.graph.add_graph_from_networkx(G2, directed=True)
cyto.set_layout(name='dagre', nodeSpacing=10, edgeLengthVal=10)
display(cyto)

到目前为止一切顺利:

现在,如果我添加一个布局,不管它是什么

cyto_style = [{ 'selector' : 'node',
                'style': {'font-family': 'arial',
                         'font-size': '10px',
                         'label': 'data(id)'}}]
cyto.set_style(cyto_style)
display(cyto)

箭头消失了!!!没有方向性绘图。 我应该怎么做才能使箭头保持在添加样式之前的状态?

发生这种情况是因为cytoscape.js,ipycytoscape 绘制的库需要具有以下样式配置才能显示箭头:

            {
                "selector": "edge.directed",
                "style": {
                    "curve-style": "bezier",
                    "target-arrow-shape": "triangle",
                    "target-arrow-color": "#9dbaea",
                },
            },

(当然你可以改变箭头的形状和颜色,但是需要指定这三个属性才能看到)。

这里的问题是,由于 traitlets 实现(Jupyter 实验室依赖于将后端与前端属性同步的库)不提供对列表或字典等深层对象的支持。换句话说,它只是自动同步像整数和字符串这样的简单对象。 幸运的是,我能够使用名为 spectate 的库在 ipycytoscape 中解决这个问题。但是,spectate 似乎无法同步嵌套的深层对象,也就是我们在创建 styles.

时所拥有的字典中的字典。

我今天看了一会儿,但真的想不出如何解决它。我明天再试试。请考虑关注 the issue,因为我在堆栈溢出中不活跃。 另外,如果您准备尝试修复它,请告诉我。我是来回答问题和讨论想法的。 干杯。