forked from crearo/portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
101 lines (71 loc) · 2.99 KB
/
app.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
import io
import json
import os
from flask import Flask, render_template, request
app = Flask(__name__)
common = {
'first_name': 'Peter',
'last_name': 'Parker',
'alias': 'spiderman'
}
@app.route('/')
def index():
return render_template('home.html', common=common)
@app.route('/timeline')
def timeline():
timeline = get_static_json("static/files/timeline.json")
return render_template('timeline.html', common=common, timeline=timeline)
@app.route('/reading')
def reading():
data = get_static_json("static/files/reading.json")
return render_template('reading.html', common=common, data=data)
@app.route('/projects')
def projects():
data = get_static_json("static/projects/projects.json")['projects']
data.sort(key=order_projects_by_weight, reverse=True)
tag = request.args.get('tags')
if tag is not None:
data = [project for project in data if tag.lower() in [project_tag.lower() for project_tag in project['tags']]]
return render_template('projects.html', common=common, projects=data, tag=tag)
@app.route('/experiences')
def experiences():
experiences = get_static_json("static/experiences/experiences.json")['experiences']
experiences.sort(key=order_projects_by_weight, reverse=True)
return render_template('projects.html', common=common, projects=experiences, tag=None)
def order_projects_by_weight(projects):
try:
return int(projects['weight'])
except KeyError:
return 0
@app.route('/projects/<title>')
def project(title):
projects = get_static_json("static/projects/projects.json")['projects']
experiences = get_static_json("static/experiences/experiences.json")['experiences']
in_project = next((p for p in projects if p['link'] == title), None)
in_exp = next((p for p in experiences if p['link'] == title), None)
if in_project is None and in_exp is None:
return render_template('404.html'), 404
# fixme: choose the experience one for now, cuz I've done some shite hardcoding here.
elif in_project is not None and in_exp is not None:
selected = in_exp
elif in_project is not None:
selected = in_project
else:
selected = in_exp
# load html if the json file doesn't contain a description
if 'description' not in selected:
path = "experiences" if in_exp is not None else "projects"
selected['description'] = io.open(get_static_file(
'static/%s/%s/%s.html' % (path, selected['link'], selected['link'])), "r", encoding="utf-8").read()
return render_template('project.html', common=common, project=selected)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html', common=common), 404
def get_static_file(path):
site_root = os.path.realpath(os.path.dirname(__file__))
return os.path.join(site_root, path)
def get_static_json(path):
return json.load(open(get_static_file(path)))
if __name__ == "__main__":
print("running py app")
app.run(host="127.0.0.1", port=5000, debug=True)