有向图箭头消失的 ipycytoscape 布局

问题描述

我有以下代码用 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],[],[]]
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)

到目前为止一切顺利:

enter image description here

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

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

enter image description here

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

解决方法

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

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

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

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

我今天花了一段时间查看它,但无法真正弄清楚如何修复它。我明天再试试。请考虑关注 the issue,因为我在堆栈溢出中不活跃。 另外,如果您准备尝试修复它,请告诉我。我在附近回答问题和讨论想法。 干杯。