Flask: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "=Flask= Short introduction guide on Flask. http://flask.pocoo.org/ (Used for XPPL, so to see a more advanced use in connection with database, see XPPL) ==Basic Flask==...")
 
No edit summary
Line 31: Line 31:
$ FLASK_APP=hello.py flask run
$ FLASK_APP=hello.py flask run
</pre>
</pre>
== http methods ==
Methods like GET or POST (DELETE, PUT…) can be handled by flask
Therefore you need to add the wanted methods to the route definition like:
<pre>
from flask import Flask
app = Flask(__name__)
@app.route('/address_to_post', methods= ['POST','GET'])
def respond_to_post():
    answer = ""
    if request.method == 'GET':
        answer = "get"
    if request.method == 'POST':
        answer = "get"
    return answer
</pre>
with request.method you can determine the incoming kind of request.

Revision as of 19:20, 9 June 2018

Flask

Short introduction guide on Flask. http://flask.pocoo.org/ (Used for XPPL, so to see a more advanced use in connection with database, see XPPL)

Basic Flask

Flask is a microframework for python to create web applications. It basically connects the webserver with your python code.

Install

$ pip install Flask

Simple text serving

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"
  • with @app.route you can define the url flask respons to.
  • the function definition after is mandetory as well as the return.
  • everything that comes after return gets sent back to the browser (http GET request)

Run it with:

$ FLASK_APP=hello.py flask run

http methods

Methods like GET or POST (DELETE, PUT…) can be handled by flask

Therefore you need to add the wanted methods to the route definition like:

from flask import Flask
app = Flask(__name__)

@app.route('/address_to_post', methods= ['POST','GET'])
def respond_to_post():
    answer = ""
    if request.method == 'GET':
        answer = "get"
    if request.method == 'POST':
        answer = "get"
    return answer

with request.method you can determine the incoming kind of request.