forked from yannikism/map-of-knowledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.2_main.py
299 lines (220 loc) · 8.39 KB
/
6.2_main.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import dash_cytoscape as cyto
from dash.exceptions import PreventUpdate
from markup import controls, graph, info_button, hover_text, stylesheet, placeholder, app, server
from WikipediaArticle import WikipediaArticle
from WikipediaArticle import suggest_article as getOptions
# pip install dash_bootstrap_components
"""
TODO:
- Skip print outs on heroku for better performance
- flag buttons for languages
- checkbox: set max_link_count to -1 (/ALL)?
- communicate when running (spinner), show when errors occur
- Beautify mouse over description
- Maybe use <iframe> ?
now:
- if node already exists, create just new edge but not a new node
- When Dropdown option is chosen entered text disappears
- When page is closen, server continues searches
- More detailed Info text
DONE:
dropdown suggestions in different languages
"""
#%% set variables
curr_depth = -2
topic = ""
depth = 0
lang = ""
max_link_count = 3
is_running = False
all_searchTerms = []
elements = []
next_id = 0
number_of_nodes = 0
terms_curr_stage = []
elements_curr_stage = []
def getArticle (search_term, language, number_branches):
'''
To return ALL links, set number_branches to -1.
'''
number_branches = int(number_branches)
article = WikipediaArticle(page_name = search_term, language = language)
article.get_links_in_summary()
if number_branches != -1:
article.filter(number_branches)
elif number_branches == -1: # -1 Represents: get all links
article.filtered_links_from_summary = article.links_from_summary
return article
def createElements (title, mother):
global all_searchTerms, elements, lang, next_id
global elements_curr_stage, terms_curr_stage # current stage/depth
article = getArticle(title, lang, max_link_count)
links = article.filtered_links_from_summary
for i in range(len(links)):
link = links[i]
article_already_exists = False
#Find out if article already exists
for element in elements:
for article in element:
#elements is nested like this: [[[article_data: {"data": {"label": "blub"}}]], [{...},{...},{...}]]
#Starting article is always elements[0]
if ("label" in article["data"].keys()):
#Has to be tested for to prevent errors
if article["data"]["label"] == link:
#Make edge to exististing article
#Edge means "connection"
source_id = str(curr_depth - 1) + "-" + str(mother)
target_id = article["data"]["id"]
new_edge = {'data': {'source': source_id, 'target': target_id}}
elements_curr_stage.append(new_edge)
article_already_exists = True
if article_already_exists == False:
terms_curr_stage.append(link)
# nodes
label = link
linked_article = getArticle(label, lang, max_link_count)
linked_article = linked_article.toJSON()
article_id = str(curr_depth) + "-" + str(next_id + i)
new_article = {'data': {'id': article_id, 'label': label, 'wiki_object': linked_article}}
elements_curr_stage.append(new_article)
#Make edge to newly generated article
#Edge means "connection"
source_id = str(curr_depth - 1) + "-" + str(mother)
target_id = article_id
new_edge = {'data': {'source': source_id, 'target': target_id}}
elements_curr_stage.append(new_edge)
next_id += len(links)
def generateNextStage (term, lang):
global elements, all_searchTerms
global next_id, number_of_nodes
global elements_curr_stage, terms_curr_stage
if all_searchTerms == []:
article = getArticle(term, lang, max_link_count)
article = article.toJSON()
elements.append([{'data': {'id': "0-0", 'label': term, 'wiki_object': article}}])
all_searchTerms.append([term])
number_of_nodes = 1
else:
next_id = 0
terms = all_searchTerms[-1]
for i in range(len(terms)):
term = terms[i]
createElements(term, i)
all_searchTerms.append(terms_curr_stage)
elements.append(elements_curr_stage)
terms_curr_stage = []
elements_curr_stage = []
#print("ALL SEARCH TERMS")
#print(all_searchTerms)
#print("ELEMENTS")
#print(elements)
# Start the search with a click on START, update search parameters
@app.callback(Output('elli', 'children'),
[Input('start-button', 'n_clicks')],
[State('search-dropdown', 'value'),
State('depth', 'value'),
State('max-count', 'value'),
State('language-dropdown', 'value')], prevent_initial_call=True)
def initialize_search (n_klicks, top, dep, number_branches, lng):
global topic, depth, max_link_count, lang, curr_depth
global elements, all_searchTerms
elements = []
all_searchTerms = []
curr_depth = -1
if not n_klicks == -1:
if top: topic = top
else: topic = placeholder
depth = int(dep)
max_link_count = number_branches
lang = lng
return ""
# starts the update_elements def if depth is not reached yet
@app.callback(Output('ello', 'children'),
Input('interval-component', 'n_intervals'))
def for_depth(v):
global curr_depth, depth
global is_running
if not is_running and -2 < curr_depth < depth:
is_running = True
#return v
#damit die Zahl nicht mehr erscheint und irritiert
else: raise PreventUpdate
# updates the elements of cytoscape graph
@app.callback(Output('cytoscape', 'elements'),
Output('depth', 'value'),
Output('status', 'children'),
Input('ello', 'children'),
Input('add-depth', 'n_clicks'), prevent_initial_call=True)
def update_elements(v, n_klicks):
global topic, lang, depth
global curr_depth, is_running, depth
ctx = dash.callback_context
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
if button_id == 'add-depth':
depth += 1
raise PreventUpdate
curr_depth += 1
generateNextStage(topic, lang)
list_of_elements = []
for el in elements:
for e in el:
list_of_elements.append(e)
is_running = False
#print("LIST OF ELEM")
#print(list_of_elements)
return list_of_elements, depth, str(curr_depth) + "/" + str(depth)
#%%
# Search suggestions
@app.callback(
dash.dependencies.Output("search-dropdown", "options"),
[dash.dependencies.Input("search-dropdown", "search_value")], prevent_initial_call=True)
def update_options(search_value):
dic_op = []
options = getOptions(search_value)
for option in options:
eintrag = {'label':option, 'value':option}
dic_op.append(eintrag)
return dic_op
@app.callback(
dash.dependencies.Output("search-dropdown", "placeholder"),
[dash.dependencies.Input("search-dropdown", "search_value")], prevent_initial_call=True)
def update_placeholder(search_value):
global placeholder
if search_value.strip() != "": # Checks if search value is empty
placeholder = search_value
return placeholder
#%%
# Weiterleitung
@app.callback(Output('start-button', 'n_clicks'),
Input('cytoscape', 'tapNodeData'), prevent_initial_call=True)
def displayTapNodeData(data):
global topic
topic = data['label']
return -1
# info_button
@app.callback(Output("info-text", "is_open"),
[Input("info-button", "n_clicks")],
[State("info-text", "is_open")])
def toggle_collapse(n_clicks, is_open):
if n_clicks:
return not is_open
return is_open
#summaries
@app.callback(Output('hover_text', 'children'),
Input('cytoscape', 'mouseoverNodeData'))
def displayHoverNodeData(data):
if data:
return data['wiki_object']['summary_text']
#%%
# Layout
@app.callback(Output('cytoscape', 'layout'),
[Input('layout-dropdown', 'value')])
def update_cytoscape_layout(layout):
return {'name': layout}
if __name__ == '__main__':
app.run_server(debug=False, port=8060)