forked from ucl-dark/ucl-dark.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
291 lines (222 loc) · 6.87 KB
/
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
# pylint: disable=global-statement,redefined-outer-name
import argparse
import csv
import glob
import json
import os
import yaml
from flask import Flask, jsonify, redirect, render_template, send_from_directory
from flask_frozen import Freezer
from flaskext.markdown import Markdown
site_data = {}
by_uid = {}
def main(site_data_path):
global site_data, extra_files
extra_files = ["introduction.md"]
# Load all for your sitedata one time.
for f in glob.glob(site_data_path + "/*"):
extra_files.append(f)
name, typ = f.split("/")[-1].split(".")
if typ == "json":
site_data[name] = json.load(open(f))
elif typ in {"csv", "tsv"}:
site_data[name] = list(csv.DictReader(open(f)))
elif typ == "yml":
site_data[name] = yaml.load(open(f).read(), Loader=yaml.SafeLoader)
for typ in ["papers", "speakers", "workshops"]:
by_uid[typ] = {}
for p in site_data[typ]:
try:
by_uid[typ][p["UID"]] = p
except KeyError as e:
print(p)
print("Data Successfully Loaded")
return extra_files
# ------------- SERVER CODE -------------------->
app = Flask(__name__)
app.config.from_object(__name__)
freezer = Freezer(app)
markdown = Markdown(app)
# MAIN PAGES
def _data():
data = {}
data["config"] = site_data["config"]
return data
@app.route("/")
def index():
return redirect("/index.html")
# TOP LEVEL PAGES
@app.route("/index.html")
def home():
data = _data()
data["introduction"] = open("introduction.md").read()
data["news"] = site_data["news"]
for group_name, content in site_data["people"].items():
data[group_name] = content
return render_template("index.html", **data)
@app.route("/about.html")
def about():
data = _data()
data["FAQ"] = site_data["faq"]["FAQ"]
return render_template("about.html", **data)
@app.route("/papers.html")
def papers():
data = _data()
data["papers"] = site_data["papers"]
return render_template("papers.html", **data)
@app.route("/speakers.html")
def speakers():
data = _data()
data["speakers"] = site_data["speakers"]
return render_template("speakers.html", **data)
@app.route("/paper_vis.html")
def paper_vis():
data = _data()
return render_template("papers_vis.html", **data)
@app.route("/calendar.html")
def schedule():
data = _data()
data["day"] = {
"speakers": site_data["speakers"],
"highlighted": [
format_paper(by_uid["papers"][h["UID"]]) for h in site_data["highlighted"]
],
}
return render_template("schedule.html", **data)
def extract_list_field(v, key):
value = v.get(key, "")
if isinstance(value, list):
return value
elif isinstance(value, str):
return value.split("|")
else:
return value
def format_paper(v):
list_keys = ["authors", "keywords", "session", "proceedings", "year"]
list_fields = {}
for key in list_keys:
list_fields[key] = extract_list_field(v, key)
if os.path.isfile("./static/images/papers/{}.jpg".format(v["UID"])):
image_path = "./static/images/papers/{}.jpg".format(v["UID"])
else:
image_path = "./static/images/papers/{}.png".format(v["UID"])
if "blog" in v:
with open("./static/blogs/{}".format(v["blog"]), "r") as f:
blog = f.read()
else:
blog = ""
return {
"id": v["UID"],
"forum": v["UID"],
"image": image_path,
"content": {
"title": v["title"],
"authors": list_fields["authors"],
"keywords": list_fields["keywords"],
"abstract": v["abstract"],
"TLDR": v["abstract"],
"recs": [],
"session": list_fields["session"],
"proceedings": list_fields["proceedings"],
"year": list_fields["year"],
"url": v.get("url", ""),
"blog": blog,
},
}
def format_workshop(v):
list_keys = ["authors"]
list_fields = {}
for key in list_keys:
list_fields[key] = extract_list_field(v, key)
return {
"id": v["UID"],
"title": v["title"],
"organizers": list_fields["authors"],
"abstract": v["abstract"],
}
# ITEM PAGES
@app.route("/poster_<poster>.html")
def poster(poster):
uid = poster
v = by_uid["papers"][uid]
data = _data()
data["paper"] = format_paper(v)
return render_template("poster.html", **data)
@app.route("/speaker_<speaker>.html")
def speaker(speaker):
uid = speaker
v = by_uid["speakers"][uid]
data = _data()
data["speaker"] = v
return render_template("speaker.html", **data)
@app.route("/workshop_<workshop>.html")
def workshop(workshop):
uid = workshop
v = by_uid["workshops"][uid]
data = _data()
data["workshop"] = format_workshop(v)
return render_template("workshop.html", **data)
@app.route("/chat.html")
def chat():
data = _data()
return render_template("chat.html", **data)
# FRONT END SERVING
@app.route("/papers.json")
def paper_json():
json = []
for v in site_data["papers"]:
json.append(format_paper(v))
return jsonify(json)
@app.route("/speakers.json")
def speaker_json():
json = []
for v in site_data["speakers"]:
json.append(v)
return jsonify(json)
@app.route("/static/<path:path>")
def send_static(path):
return send_from_directory("static", path)
@app.route("/serve_<path>.json")
def serve(path):
return jsonify(site_data[path])
# --------------- DRIVER CODE -------------------------->
# Code to turn it all static
@freezer.register_generator
def generator():
for paper in site_data["papers"]:
yield "poster", {"poster": str(paper["UID"])}
for speaker in site_data["speakers"]:
yield "speaker", {"speaker": str(speaker["UID"])}
for workshop in site_data["workshops"]:
yield "workshop", {"workshop": str(workshop["UID"])}
for key in site_data:
yield "serve", {"path": key}
def parse_arguments():
parser = argparse.ArgumentParser(description="MiniConf Portal Command Line")
parser.add_argument(
"--build",
action="store_true",
default=False,
help="Convert the site to static assets",
)
parser.add_argument(
"-b",
action="store_true",
default=False,
dest="build",
help="Convert the site to static assets",
)
parser.add_argument("path", help="Pass the JSON data path and run the server")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_arguments()
site_data_path = args.path
extra_files = main(site_data_path)
if args.build:
freezer.freeze()
else:
debug_val = False
if os.getenv("FLASK_DEBUG") == "True":
debug_val = True
app.run(port=5000, debug=debug_val, extra_files=extra_files)