-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
118 lines (88 loc) · 3.04 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from flask import Flask, url_for, redirect, render_template, request, abort
app = Flask(__name__)
#######################
# Basic routing/views #
#######################
# Simple route/view
@app.route('/') # index route
def hello_world():
return "hello, world!"
# Route to variable URL
# E.g. request to /user/john would resolve to page containing Hello, john.
@app.route('/user/<username>/')
def user(username):
return 'Hello, %s' % username
# Optional converter, only accepts integers as varible part of URL
# can also use float or path (path is same as default but accepts /s)
@app.route('/post/<int:post_id>/')
def show_post(post_id):
return 'This is post %d' % post_id
# Redirect to another view function
@app.route('/old-posts/')
def old_posts():
return redirect(url_for('show_post', post_id=12345))
# Multiple routes to same view function
# you can tell which route is being used with request.url_rule
@app.route('/url/')
@app.route('/url2/')
def url():
url = request.url_rule
return 'route being used is: %s' % url
# Route to a number of pages if matching varible portion of route or cause 404
@app.route('/pages/<variable_part>')
def var_page(variable_part):
variable_parts = ['123', '456', 'hello-world', 'readifying-austlii']
if variable_part in variable_parts:
return "your page is: %s" % variable_part
else:
abort(404)
# Custom 404 handling
@app.errorhandler(404)
def not_found(error):
return '404: Page Not Found :(', 404
#######################
# Rendering templates #
#######################
# Simple template render
# variable content is passed to the template as render_template() kwarg
@app.route('/simple-template/')
def templated():
h2_content = "This content is rendered via a template"
return render_template('simple-template.html', h2_content=h2_content)
# Inherited template
# See base.html and extends-base.html templates for explanation
@app.route('/extended-base-template/')
def extends_base():
return render_template('extends-base.html')
######################
# The request object #
######################
# Display all request headers
# request.headers is dictionary-like object so standard Dict methods apply
# E.g. request.headers.get('some_key')
@app.route('/headers/')
def headers():
headers = request.headers
output = ""
for item in headers:
item = str(item) + "<br>"
output += item
return output
# Display single request header (when known)
@app.route('/browser/')
def browser():
user_agent = request.headers.get('User-Agent')
return 'Browser is: %s' % user_agent
# Get url params
# e.g. browse to http://localhost:5000/params?some_param=hello, world!
# request.args is a Werkzeug MultiDict containing all params as k,v pairs
# See http://werkzeug.pocoo.org/docs/0.11/datastructures/
@app.route('/params/')
def params():
param = request.args.get('some_param')
if param:
return param
else:
return "bad request :(", 400 # handle missing param as 400 error
if __name__ == '__main__':
app.run(debug=True)