Spaces:
Runtime error
Runtime error
Upload got.py
Browse files
got.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
from pyvis.network import Network
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def got_func(physics):
|
| 9 |
+
got_net = Network(height="600px", width="100%", font_color="black",heading='Game of Thrones Graph')
|
| 10 |
+
|
| 11 |
+
# set the physics layout of the network
|
| 12 |
+
got_net.barnes_hut()
|
| 13 |
+
got_data = pd.read_csv("stormofswords.csv")
|
| 14 |
+
#got_data = pd.read_csv("stormofswords.csv")
|
| 15 |
+
#got_data.rename(index={0: "Source", 1: "Target", 2: "Weight"})
|
| 16 |
+
sources = got_data['Source']
|
| 17 |
+
targets = got_data['Target']
|
| 18 |
+
weights = got_data['Weight']
|
| 19 |
+
|
| 20 |
+
edge_data = zip(sources, targets, weights)
|
| 21 |
+
|
| 22 |
+
for e in edge_data:
|
| 23 |
+
src = e[0]
|
| 24 |
+
dst = e[1]
|
| 25 |
+
w = e[2]
|
| 26 |
+
|
| 27 |
+
got_net.add_node(src, src, title=src)
|
| 28 |
+
got_net.add_node(dst, dst, title=dst)
|
| 29 |
+
got_net.add_edge(src, dst, value=w)
|
| 30 |
+
|
| 31 |
+
neighbor_map = got_net.get_adj_list()
|
| 32 |
+
|
| 33 |
+
# add neighbor data to node hover data
|
| 34 |
+
for node in got_net.nodes:
|
| 35 |
+
node["title"] += " Neighbors:<br>" + "<br>".join(neighbor_map[node["id"]])
|
| 36 |
+
node["value"] = len(neighbor_map[node["id"]])
|
| 37 |
+
if physics:
|
| 38 |
+
got_net.show_buttons(filter_=['physics'])
|
| 39 |
+
got_net.show("gameofthrones.html")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def simple_func(physics):
|
| 43 |
+
nx_graph = nx.cycle_graph(10)
|
| 44 |
+
nx_graph.nodes[1]['title'] = 'Number 1'
|
| 45 |
+
nx_graph.nodes[1]['group'] = 1
|
| 46 |
+
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
|
| 47 |
+
nx_graph.nodes[3]['group'] = 10
|
| 48 |
+
nx_graph.add_node(20, size=20, title='couple', group=2)
|
| 49 |
+
nx_graph.add_node(21, size=15, title='couple', group=2)
|
| 50 |
+
nx_graph.add_edge(20, 21, weight=5)
|
| 51 |
+
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
nt = Network("500px", "500px",notebook=True,heading='')
|
| 55 |
+
nt.from_nx(nx_graph)
|
| 56 |
+
#physics=st.sidebar.checkbox('add physics interactivity?')
|
| 57 |
+
if physics:
|
| 58 |
+
nt.show_buttons(filter_=['physics'])
|
| 59 |
+
nt.show('test.html')
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def karate_func(physics):
|
| 63 |
+
G = nx.karate_club_graph()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
nt = Network("500px", "500px",notebook=True,heading='Zachary’s Karate Club graph')
|
| 67 |
+
nt.from_nx(G)
|
| 68 |
+
#physics=st.sidebar.checkbox('add physics interactivity?')
|
| 69 |
+
if physics:
|
| 70 |
+
nt.show_buttons(filter_=['physics'])
|
| 71 |
+
nt.show('karate.html')
|