Skip to content

Instantly share code, notes, and snippets.

@scvnc
Created August 11, 2013 03:03
Show Gist options
  • Save scvnc/6203187 to your computer and use it in GitHub Desktop.
Save scvnc/6203187 to your computer and use it in GitHub Desktop.
conceptual tutorial
"""
Demonstrating POST data with Mitchuation (through Flask)
"""
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/')
def hello_world():
"""
This will return the raw HTML form that is configured to send the
data in the form to the url '/test'. This other route is defined
in another method below.
"""
return """
<form action="/test" method="POST">
How old are you?: <input name="age"/> (press enter when done)
</form>
"""
@app.route('/test', methods=['post'])
def test():
"""
Each HTTP request can have data sent from a form
This is typically called POST data (for reasons I won't get into right now)
Flask provides access to this data via the request object's property
called form. It's a dictionary of values recieved from the web browser
"""
their_age = request.form['age']
# The above establishes the variable 'their_name' as the form parameter 'name.'
# This allows you to see the data structure that flask
# provides to see the data the browser sent. (in the console.)
print request.form
return 'Hello, I see that you are ' + their_age + ' years old.'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
""" Play around with this; add a new field, maybe make an madlib """
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment