forked from truckyducky/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IceBreaker_KG.py
95 lines (68 loc) · 3.06 KB
/
IceBreaker_KG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import streamlit as st
import streamlit.components.v1 as components
import pandas as pd
import numpy as np
import networkx as nx
from collections import Counter
from pyvis.network import Network
from IPython.core.display import display, HTML
descriptors_data = pd.read_csv("HEAL Ice Breaker.csv")
df = pd.DataFrame(descriptors_data, columns = ["Text"])
array_of_descriptors = np.array(df)
list_of_ppl_descriptors = array_of_descriptors.tolist()
formatted_data = []
for i in range(0, len(list_of_ppl_descriptors), 6):
person_and_descriptors = list_of_ppl_descriptors[i:i+6]
person_and_descriptors = [item[0] if len(item) == 1 else item for item in person_and_descriptors]
formatted_data.append(person_and_descriptors)
# Print the formatted data
# for entry in formatted_data:
# print(entry)
net = Network(notebook=True, width="1000px", height="600px", cdn_resources='remote', font_color='white', bgcolor="black", select_menu = True, filter_menu = True)
st.title('Ice Breaker Activity')
def create_knowledge_graph(data):
# Flatten the data to get a list of all descriptors
all_descriptors = [descriptor for entry in data for descriptor in entry[1:]]
# Calculate the frequency of each descriptor
descriptor_frequency = Counter(all_descriptors)
# Scale the size of nodes based on frequency
max_frequency = max(descriptor_frequency.values())
scaling_factor = 20 # Adjust the scaling factor as needed
# Add nodes and edges to the network
for entry in data:
person = entry[0]
net.add_node(person, color='#e07c3e')
for descriptor in entry[1:]:
frequency = descriptor_frequency[descriptor]
node_size = scaling_factor * (frequency / max_frequency)
net.add_node(descriptor, color='#982568', size=node_size)
net.add_edge(person, descriptor)
# Save and show the network
return net.show('ice_breaker.html', local=False)
create_knowledge_graph(formatted_data)
try:
path = '/tmp'
html_path = f'{path}/ice_breaker.html'
net.save_graph(html_path)
#st.write(f"HTML file saved at {html_path}")
with open(html_path, 'r', encoding='utf-8') as HtmlFile:
html_content = HtmlFile.read()
components.html(html_content, height=1000, width=1000)
except FileNotFoundError:
st.warning(f"HTML file not found at {html_path}.")
except Exception as e:
st.error(f"An error occurred while reading the HTML file: {e}")
# # Save and read graph as HTML file (on Streamlit Sharing)
# try:
# path = '/tmp'
# net.save_graph(f'{path}/ice_breaker.html')
# HtmlFile = open(f'{path}/ice_breaker.html', 'r', encoding='utf-8')
# Save and read graph as HTML file (locally)
# except:
# path = '/html_files'
# net.save_graph(f'{path}/ice_breaker.html')
# HtmlFile = open(f'{path}/ice_breaker.html', 'r', encoding='utf-8')
# Load HTML file in HTML component for display on Streamlit page
components.html(HtmlFile.read(), height=1000, width = 1000)
# # Load HTML into HTML component for display on Streamlit
# components.html(HtmlFile.read(), height=800, width=800)