Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Herbert committed Sep 11, 2017
0 parents commit 4535893
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions app.py
@@ -0,0 +1,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

0 comments on commit 4535893

Please sign in to comment.