-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
234 lines (171 loc) · 5.31 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
# 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 = ["README.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]:
by_uid[typ][p["UID"]] = 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["readme"] = open("README.md").read()
data["committee"] = site_data["committee"]["committee"]
data["config"]["sponsors"] = data["config"]["sponsors"]
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("/paper_vis.html")
def paperVis():
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)
@app.route("/workshops.html")
def workshops():
data = _data()
data["workshops"] = site_data["workshops"]
return render_template("workshops.html", **data)
def format_paper(v):
return {
"id": v["UID"],
"forum": v["UID"],
"content": {
"title": v["title"],
"authors": v["authors"].split("|"),
"keywords": v["keywords"].split("|"),
"abstract": v["abstract"],
"TLDR": v["abstract"],
"recs": [],
"session": v.get("session", "").split("|"),
},
}
# 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"] = v
return render_template("workshop.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("/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)