-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.py
50 lines (39 loc) · 2.1 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
#app.py
from flask import Flask, request #import main Flask class and request object
app = Flask(__name__) #create the Flask app
@app.route('/query-example')
def query_example():
language = request.args.get('language') #if key doesn't exist, returns None
framework = request.args['framework'] #if key doesn't exist, returns a 400, bad request error
website = request.args.get('website')
return '''<h1>The language value is: {}</h1>
<h1>The framework value is: {}</h1>
<h1>The website value is: {}'''.format(language, framework, website)
@app.route('/form-example', methods=['GET', 'POST']) #allow both GET and POST requests
def form_example():
if request.method == 'POST': #this block is only entered when the form is submitted
language = request.form.get('language')
framework = request.form['framework']
return '''<h1>The language value is: {}</h1>
<h1>The framework value is: {}</h1>'''.format(language, framework)
return '''<form method="POST">
Language: <input type="text" name="language"><br>
Framework: <input type="text" name="framework"><br>
<input type="submit" value="Submit"><br>
</form>'''
@app.route('/json-example', methods=['POST']) #GET requests will be blocked
def json_example():
req_data = request.get_json()
language = req_data['language']
framework = req_data['framework']
python_version = req_data['version_info']['python'] #two keys are needed because of the nested object
example = req_data['examples'][0] #an index is needed because of the array
boolean_test = req_data['boolean_test']
return '''
The language value is: {}
The framework value is: {}
The Python version is: {}
The item at index 0 in the example list is: {}
The boolean value is: {}'''.format(language, framework, python_version, example, boolean_test)
if __name__ == '__main__':
app.run(debug=True, port=5000) #run app in debug mode on port 5000