def make_graph(self, inp): nx_graph = nx.MultiDiGraph() nodes = [] inp = ast.literal_eval(str(inp)) inp.sort(key=lambda x: x['value'], reverse=True) for x in inp: print(x) if x["_from"] not in nodes: nodes.append(x["_from"]) nx_graph.add_node(x["_from"], title=x['_from'], color="#557BEA") if x["_to"] not in nodes: nodes.append(x["_to"]) nx_graph.add_node(x["_to"], title=x["_to"], color="#557BEA") if (float(x["value"]) == 1): nx_graph.add_edge(x["_to"], x["_from"], label=round(x["value"], 5), size=120, arrowStrikethrough=True, width=x["value"] * 10, color="#BB62FF") else: if not (nx_graph.has_edge(x["_from"], x["_to"], key=None) or nx_graph.has_edge(x["_to"], x["_from"], key=None)): nx_graph.add_edge(x["_from"], x["_to"], label=round(x["value"], 5), arrowStrikethrough=False, width=x["value"] * 10, color="#4FCFFF") nx_graph.add_edge(x["_to"], x["_from"], arrowStrikethrough=False, width=x["value"] * 10, color="#4FCFFF") g = Network(height=950, width=1730, notebook=True, directed=True, bgcolor='#171a23', font_color='white') g.toggle_hide_edges_on_drag(True) g.get_network_data() g.barnes_hut() g.from_nx(nx_graph, default_edge_weight=True, default_node_size=70) g.show("test.html") self.get_html(0)
def make_html_string(g: Network) -> str: use_link_template = False for n in g.nodes: title = n.get("title", None) if title: if "href" in title: """ this tells the template to override default hover mechanic, as the tooltip would move with the mouse cursor which made interacting with hover data useless. """ use_link_template = True break with open(g.path) as html: content = html.read() template = Template(content) nodes, edges, height, width, options = g.get_network_data() # check if physics is enabled if isinstance(g.options, dict): if 'physics' in g.options and 'enabled' in g.options['physics']: physics_enabled = g.options['physics']['enabled'] else: physics_enabled = True else: physics_enabled = g.options.physics.enabled html_string = template.render(height=height, width=width, nodes=nodes, edges=edges, options=options, physics_enabled=physics_enabled, use_DOT=g.use_DOT, dot_lang=g.dot_lang, widget=g.widget, bgcolor=g.bgcolor, conf=g.conf, tooltip_link=use_link_template) return html_string
def nx2vis_dict(graph: nx.Graph) -> Dict[str, object]: """[summary] Args: graph (nx.Graph): [description] Returns: Dict[str, object]: [description] """ # nt = Network(height='400px', width='50%', bgcolor='#2222', font_color='white') nt = Network(height='400px', width='100%') # nt = Network(height='100%', width='100%') nt.from_nx(graph) vis_nodes, vis_edges, vis_heading, vis_height, vis_width, vis_options = nt.get_network_data( ) d = { "nodes": vis_nodes, "edges": vis_edges, "heading": vis_heading, "height": vis_height, "width": vis_width, "options": vis_options, } return d