Skip to content

Commit

Permalink
Merge pull request #63 from nocluebutalotofit/master
Browse files Browse the repository at this point in the history
fixes issues: #45 and #60
  • Loading branch information
Giancarlo Perrone committed Jun 4, 2020
2 parents 84441ac + 004a9a5 commit 2755fd5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 155 deletions.
200 changes: 53 additions & 147 deletions notebooks/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -18,31 +18,9 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"500px\"\n",
" height=\"500px\"\n",
" src=\"example.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f0e3ec32b0>"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"g = Network(notebook=True)\n",
"g.add_nodes(range(5))\n",
Expand All @@ -66,7 +44,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -75,33 +53,11 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"500px\"\n",
" height=\"500px\"\n",
" src=\"example.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f0e3edc198>"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"nxg = nx.random_tree(20)\n",
"g.from_nx(nxg)\n",
Expand All @@ -117,31 +73,9 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"500px\"\n",
" height=\"500px\"\n",
" src=\"example.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f0e3e9d588>"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"g.toggle_physics(False)\n",
"g.show(\"example.html\")"
Expand All @@ -156,31 +90,9 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"500px\"\n",
" height=\"500px\"\n",
" src=\"example.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f0e4f15320>"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"g = Network(notebook=True)\n",
"g.add_nodes([1,2,3],\n",
Expand All @@ -201,31 +113,9 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"100%\"\n",
" height=\"750px\"\n",
" src=\"example.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f0e4ec3dd8>"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
Expand Down Expand Up @@ -270,35 +160,51 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"100%\"\n",
" height=\"750px\"\n",
" src=\"example.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x1f0e541e390>"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"got_net.show_buttons(filter_=\"physics\")\n",
"got_net.show(\"example.html\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import networkx as nx"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"nx_graph = nx.cycle_graph(10)\n",
"nx_graph.nodes[1]['title'] = 'Number 1'\n",
"nx_graph.nodes[1]['group'] = 1\n",
"nx_graph.nodes[3]['title'] = 'I belong to a different group!'\n",
"nx_graph.nodes[3]['group'] = 10\n",
"nx_graph.add_node(20, size=20, title='couple', group=2)\n",
"nx_graph.add_node(21, size=15, title='couple', group=2)\n",
"nx_graph.add_edge(20, 21, weight=5)\n",
"nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)\n",
"\n",
"nt = Network(notebook=True, height=\"750px\", width=\"100%\")\n",
"\n",
"nt.from_nx(nx_graph)\n",
"nt.show(\"nx.html\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -317,7 +223,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.7.7"
}
},
"nbformat": 4,
Expand Down
45 changes: 37 additions & 8 deletions pyvis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,30 +561,59 @@ def neighbors(self, node):
assert(node in self.node_ids), "error: %s node not in network" % node
return self.get_adj_list()[node]

def from_nx(self, nx_graph):
def from_nx(self, nx_graph, node_size_transf=(lambda x: x), edge_weight_transf=(lambda x: x),
default_node_size=10, default_edge_weight=1):
"""
This method takes an exisitng Networkx graph and translates
it to a PyVis graph format that can be accepted by the VisJs
API in the Jinja2 template. This operation is done in place.
:param nx_graph: The Networkx graph object that is to be translated.
:type nx_graph: networkx.Graph instance
>>> nx_graph = Networkx.cycle_graph()
:param node_size_transf: function to transform the node size for plotting
:type node_size_transf: func
:param edge_weight_transf: function to transform the edge weight for plotting
:type edge_weight_transf: func
:param default_node_size: default node size if not specified
:param default_edge_weight: default edge weight if not specified
>>> nx_graph = nx.cycle_graph(10)
>>> nx_graph.nodes[1]['title'] = 'Number 1'
>>> nx_graph.nodes[1]['group'] = 1
>>> nx_graph.nodes[3]['title'] = 'I belong to a different group!'
>>> nx_graph.nodes[3]['group'] = 10
>>> nx_graph.add_node(20, size=20, title='couple', group=2)
>>> nx_graph.add_node(21, size=15, title='couple', group=2)
>>> nx_graph.add_edge(20, 21, weight=5)
>>> nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)
>>> nt = Network("500px", "500px")
# populates the nodes and edges data structures
>>> nt.from_nx(nx_graph)
>>> nt.show("nx.html")
"""
assert(isinstance(nx_graph, nx.Graph))
edges = nx_graph.edges(data=True)
nodes = nx_graph.nodes()
nodes = nx_graph.nodes(data=True)

if len(edges) > 0:
for e in edges:
self.add_node(e[0], e[0], title=str(e[0]))
self.add_node(e[1], e[1], title=str(e[1]))
self.add_edge(e[0], e[1])
else:
self.add_nodes(nodes)
if 'size' not in nodes[e[0]].keys():
nodes[e[0]]['size'] = default_node_size
nodes[e[0]]['size'] = int(node_size_transf(nodes[e[0]]['size']))
if 'size' not in nodes[e[1]].keys():
nodes[e[1]]['size'] = default_node_size
nodes[e[1]]['size'] = int(node_size_transf(nodes[e[1]]['size']))
self.add_node(e[0], **nodes[e[0]])
self.add_node(e[1], **nodes[e[1]])

if 'weight' not in e[2].keys():
e[2]['weight'] = default_edge_weight
e[2]['weight'] = edge_weight_transf(e[2]['weight'])
self.add_edge(e[0], e[1], **e[2])

for node in nx.isolates(nx_graph):
if 'size' not in nodes[node].keys():
nodes[node]['size']=default_node_size
self.add_node(node, **nodes[node])

def get_nodes(self):
"""
Expand Down

0 comments on commit 2755fd5

Please sign in to comment.