pyvis - 如何在浏览器中使节点 ID 可选(复制到缓冲区中)

问题描述 投票:0回答:1

此代码生成带有子图的 HTML 文件。问题是无法从 HTML 页面复制某些节点 ID 并将其粘贴到其他工具中。

G = nx.from_pandas_edgelist(df, source='fromKey',
                            target='toKey', 
                            create_using=nx.DiGraph) 
i = 0

for h in nx.weakly_connected_components(G):
    #Skip subgraphs with number of nodes < 10
    if len(list(h))<10:
        continue

    i += 1
    #Create subgraph
    g=nx.subgraph(G, h) 

    #----Draw using pyvis
    net = Network(height='1000px',
                width='1000px',
                directed=True#,
                #heading='Subgraph '+str(i)
                ) 

    net.from_nx(g) 

   
    net.repulsion(central_gravity=0.1) 
    net.set_edge_smooth('dynamic')

    net.show('subgraph_'+str(i)+'.html', notebook=False)       

        

图表看起来不错,但无法选择特定的节点 ID

比如说,它是某个 id=100040003003405 的节点 - 是否有任何属性允许使用鼠标光标复制 ID,以便能够将其用作即席查询的参数,而不是手动输入...

python networkx pyvis
1个回答
1
投票

一个简单的hack就是注入

<a>
标签(作为节点的标题),然后悬停复制链接地址

# add this before the loop
nx.set_node_attributes(
    G,
    dict(zip(G.nodes, map(lambda n: f'<a href="{n}">{n}</a>', G.nodes))),
    name="title",
)

enter image description here 使用过的(

G
):

import random
import networkx as nx

random.seed(0)

G = nx.Graph()

nodes = [random.getrandbits(64) for _ in range(10)]

while len(G.edges) < 15:
    n1, n2 = random.sample(nodes, 2)
    G.add_edge(n1, n2)
© www.soinside.com 2019 - 2024. All rights reserved.