-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
111 lines (92 loc) · 2.75 KB
/
app.py
File metadata and controls
111 lines (92 loc) · 2.75 KB
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
from random import random
from time import sleep
from bottle import Bottle, request, run, static_file, template
app = application = Bottle()
RECORDS = [
{
"id": 1,
"name": "Tonsai",
"description": "A view over Tonsai beach.",
"filename": "landscape01.jpg",
"latitude": "8.01025",
"longitude": "98.8380777778",
},
{
"id": 2,
"name": "Railay",
"description": "A view over Railay.",
"filename": "landscape02.jpg",
"latitude": "8.0098916667",
"longitude": "98.841075",
},
{
"id": 3,
"name": "Phrang Beach",
"description": "Phrang Beach at sunset.",
"filename": "landscape03.jpg",
"latitude": "8.0099777778",
"longitude": "98.8381055556",
},
{
"id": 4,
"name": "Thaiwand Wall",
"description": "The view from Thaiwand Wall towards Tonsai.",
"filename": "landscape04.jpg",
"latitude": "8.0127777778",
"longitude": "98.8361027778",
},
{
"id": 5,
"name": "Long Boats",
"description": "Long boats moored at the jetty.",
"filename": "landscape05.jpg",
"latitude": "8.0093777778",
"longitude": "98.8441305556",
},
]
def find_record_by_id(record_id: int):
"""Helper function to get requested record."""
return next((r for r in RECORDS if r["id"] == record_id), None)
def search_records(query: str):
"""Helper function to query data."""
q = query.strip().lower()
if not q:
return []
def hit(r):
return (
q in str(r["id"]).lower()
or q in r["name"].lower()
or q in r["description"].lower()
)
sleep(random()) # Random delay
return [r for r in RECORDS if hit(r)]
@app.get("/")
def index():
"""
Front page:
- Shows search form
- If ?q= is present, shows results
"""
q = request.query.q or ""
results = search_records(q) if q else []
sleep(random()) # Random delay
return template("index", q=q, results=results)
@app.get("/record/<record_id:int>")
def record_detail(record_id):
"""
Shows a single record's details
"""
record = find_record_by_id(record_id)
sleep(random()) # Random delay
if not record:
return template("detail", record=None, record_id=record_id)
return template("detail", record=record, record_id=record_id)
@app.get("/static/<filepath:path>")
def server_static(filepath):
return static_file(filepath, root="./static")
@app.get("/favicon.ico")
def favicon():
return static_file("favicon.ico", root="static", mimetype="image/x-icon")
if __name__ == "__main__":
# Debug server
run(app, host="0.0.0.0", port=8080, debug=True, reloader=True)