-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
195 lines (161 loc) · 5.53 KB
/
app.py
File metadata and controls
195 lines (161 loc) · 5.53 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import streamlit as st
import pandas as pd
from PIL import Image
from urllib.parse import urljoin, quote
from src.utils import (
search_text_to_graphistry,
get_graphistry_from_search,
get_graphistry_from_milieu_search,
setup_logger,
contribution,
)
logger = setup_logger(__name__)
src, dst, node_col = "to_node", "from_node", "Node"
good_node_cols = ["Node", "link", "Blurb", "Summary", "Website"]
good_node_tags = ["Types", "Revenue", "Aliases", "pagerank"]
@st.cache(suppress_st_warning=True)
def get_data(dummy_variable=True):
logger.info("Loading Data")
edges = pd.read_csv("data/edges.csv", index_col=0)
edges = edges.fillna("")
st.write(f"Number of total relationships {len(edges):,}")
nodes = pd.read_csv("data/nodes.csv", index_col=0)
nodes = nodes.fillna("")
st.write(f"Number of total entities {len(nodes):,}")
return edges, nodes
def icon(icon_name):
st.markdown(f'<i class="material-icons">{icon_name}</i>', unsafe_allow_html=True)
def display_graph(g):
if len(g._nodes) > 0:
url = g.plot(render=False)
st.markdown(
f'<iframe width=1000 height=800 src="{url}"></iframe>',
unsafe_allow_html=True,
)
else:
st.write("No results found, try another search term")
@st.cache
def simple_search(search_term):
g = get_graphistry_from_search(search_term, src, dst, node_col, edf, ndf)
return g
@st.cache
def milieu_search(search_term, both=False):
g = get_graphistry_from_milieu_search(
search_term, src, dst, node_col, edf, ndf, both=both
)
return g
@st.cache
def text_search(search_term):
g = search_text_to_graphistry(search_term, src, dst, node_col, edf, ndf)
return g
def tag_boxes(search: str, tags: list) -> str:
"""HTML scripts to render tag boxes.
st.write(tag_boxes(search, results['sorted_tags'][:10], ''),
unsafe_allow_html=True)
"""
html = ""
search = quote(search)
for tag in tags:
html += f"""
<a id="tags" href="?search={search}&tags={tag}">
{tag.replace('-', ' ')}
</a>
"""
html += "<br><br>"
return html
def pretty_pandas(i, node, url, blurb, summary, website):
littlesis = "https://littlesis.org/"
url = urljoin(littlesis, url)
if website != "":
res = f"""
<div style="font-size:122%;">
{i + 1}.
<a href="{url}">
{node}
</a>
</div>
<div style="font-size:95%;">
<div style="color:grey;font-size:85%;">
<a href="{website}">
{website[:90]}
</a>
</div>
<div style="font-size:112%;float:left;font-style:italic;">
{blurb} ·
</div>
<div style="float:left;font-style:normal;">
{summary} ·
</div>
</div>
"""
else:
res = f"""
<div style="font-size:122%;">
{i + 1}.
<a href="{url}">
{node}
</a>
</div>
<div style="font-size:95%;">
<div style="font-size:112%;float:left;font-style:italic;">
{blurb} ·
</div>
<div style="float:left;font-style:normal;">
{summary} ·
</div>
</div>
"""
return res
def print_results(search, ndf, topN=100):
tdf = ndf.drop_duplicates()
tdf = tdf.sort_values(by="pagerank", ascending=False)
for i, (_, row) in enumerate(tdf.iterrows()):
if i >= topN:
break
st.write(
pretty_pandas(i, row.Node, row.link, row.Blurb, row.Summary, row.Website),
unsafe_allow_html=True,
)
# tags = row.Types.split(',')
# tags = [t.strip() for t in tags]
# st.write(tag_boxes(search, tags), unsafe_allow_html=True)
etdf, ndf = get_data(True)
edf = etdf
# search bar
st.sidebar.header("Influence & Power Networks")
st.sidebar.subheader("Search Network")
drop_contributors = st.sidebar.checkbox("Drop Contributors", value=True)
if drop_contributors:
edf = etdf[etdf.relationship != contribution]
# st.write(f'Number of total relationships after removing "{contribution}" data is {len(edf):,}')
rand = st.sidebar.checkbox("Get Random Entity")
if rand:
entity_options = ndf.Node
entity = entity_options.sample(1).values[0]
else:
entity = "BlackRock"
options = ["Search Text", "Milieu", "Nearest"]
option = st.sidebar.selectbox("Search Type", options)
if rand: # for random entity need Milieu or Local, since they search Nodes
option = options[1]
search_term = st.sidebar.text_input("", entity, key="search")
# get the graph
if option == options[0]:
st.sidebar.write(f"Searching Summary and Blurb for {search_term}")
g = text_search(search_term)
if option == options[1]:
st.sidebar.write(f"Searching node entities for milieu graph of {search_term}")
g = milieu_search(search_term)
if option == options[2]:
st.sidebar.write(
f"Searching node entities for nearest connections to {search_term}"
)
g = simple_search(search_term)
# The main frontend calls
this_df = g._nodes
display_graph(g)
print_results(search_term, this_df)
# logo
st.sidebar.write("-" * 40)
image = Image.open("./tensorml.png")
st.sidebar.image(image, use_column_width=True)