-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.py
90 lines (72 loc) · 1.95 KB
/
template.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
from flask import Flask
import os
import socket
import requests, json
from random import random
from flask import jsonify, request, redirect, url_for
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
##############
# Basic routes
##############
@app.route('/')
def hello():
return jsonify(
response='Hello World from host \"%s\".\n' % socket.gethostname()
)
@app.route('/query-example')
def getTriples():
r = requests.get("https://www.e-codices.ch/metadata/iiif/ubb-A-III-0021/manifest.json")
print(r.text)
return r.text
@app.route('/another-example')
def randomNumber():
return jsonify(
username='[email protected]',
email='[email protected]',
id=random()
)
################
# Variable parts
################
# String
@app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name.capitalize()
# Integer
@app.route('/item/<int:itemID>')
def show_item(itemID):
return 'Item number %d:' % itemID
# Float
@app.route('/version/<float:versionNo>')
def show_version(versionNo):
return 'Version number %f:' % versionNo
##################
# Named parameters
##################
@app.route('/hero')
def show_hero():
name = request.args.get('name', default = 'Nobody', type = str)
return '%s will save you!' % name.capitalize()
# /hero?name=batman
# --> Batman will save you!
# /hero
# --> Nobody will save you!
##############
# HTTP methods
##############
# POST & GET
# ...also introducing redirect and url_for
@app.route('/login', methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
name = request.form['name']
return redirect(url_for('hello_name', name = name))
else:
name = request.args.get('name')
return redirect(url_for('hello_name', name = name))
# POST: run template.py and open template.html in browser
# GET: /login?name=superman
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True)