forked from CSCfi/rahti-flask-hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (81 loc) · 1.86 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
'''
Simple Flask app
'''
import json
import os
from pathlib import Path
import flask
# Templates
# In a proper Flask application all these templates should be in indepent files
STYLE = """
body {
background-color: beige;
font-family: "Helvetica Neue",Helvetica,"Liberation Sans",Arial,sans-serif;
font-size: 14px;
padding: 10%;
}
img {
width: 90%;
}
"""
PAGE = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>{{ student }}</title>
<style>""" + STYLE + """</style>
</head>
<body>
<h1>This is the photo gallery from {{ student }}</h1>
<ul>{% for kitten in kittens %}
<li><img src='{{ kitten }}'/> {{ kitten }}</li>
{% endfor %}</ul>
</body>
</html>
"""
# Default configuration
defaults = {
"student": "??????",
"debug": False}
config = {}
# Flask app object
app = flask.Flask(__name__,
static_url_path='/static',
static_folder='/static')
# Routes
@app.route("/", methods=['GET'])
def home():
'''
Hello page, shows photos in the /static folder
'''
kittens = Path('/static/').rglob('*.jpg')
return flask.render_template_string(
PAGE,
student=config["student"],
kittens=kittens)
# Entry function
def main():
'''
Main entry function
'''
# Load student name from file
global config
try:
with open('/etc/flask/config.json') as custom_config_file:
config = json.load(custom_config_file)
except FileNotFoundError:
config = defaults
try:
if os.environ['DEBUG']:
config["debug"] = True
except KeyError:
pass
print('Configuration:')
print(json.dumps(config))
app.run(debug=config["debug"],
port=8080,
host='0.0.0.0')
if __name__ == "__main__":
main()