from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
before_request() :
These connections are called before making a request and no arguments are passedafter_request() :
These connections are called after making a request and response is passed that will be sent to the client.teardown_request() :
These connections are called in a situation where an exception is raised and the response are not sure to get. They are also called after the construction of response. These are not allowed to change the request, and their values can be ignored.<a href=”{{ url_for(‘get_post_id’, post_id=post.id}}”>{{post.title}}<a>
@app.route(“/blog/post/<string:post_id>”)
def get_post_id(post_id):
return post_id
Flask.before_request
hook to log the request body in python flask. Here is an example :@app.before_request
def log_request_info():
app.logger.debug('Headers: %s', request.headers)
app.logger.debug('Body: %s', request.get_data())
<input type="checkbox" value="X" class="danger" name='my_checkbox'>
<input type="checkbox" value="Y" class="danger" name='my_checkbox'>
<input type="checkbox" value="Z" class="danger" name='my_checkbox'>
print(request.form.getlist('my_checkbox'))
from flask import g
@app.before_request
def load_user():
user = User.query.get(request.session.get("user_id"))
g.user = user
@app.route("/admin")
def admin():
if g.user is None or not g.user.is_admin:
return redirect(url_for("index"))
app = flask.Flask(__name__)
@app.route("/")
def index():
headers = flask.request.headers
return "Request headers:\n" + str(headers)
app.run(host="127.1.0.1", port=8080)​
<b> :
It adds a breakpoint<c> :
It resumes the execution<s> :
It debugs step by step<n> :
It moves to next line<l> :
It lists the source code<p> :
It prints an expression//referencing static files in the flask
url_for(‘static’, filename=’static_file_name’)
//example of getting a query string in the Flask
from flask import request //request method is used to work with the query string values
@app.route("/query")
def query():
args = request.args //here, we get the query string object in a variable called ‘args’ using
print(args)
<html>
<body>
<form action = "http://localhost:5000/uploader" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>
schema.sql
file to a sqlite3 command. So users need to install sqlite3 command if they want to create or start the database in Flask Python. //example of importing functions
from otherFile import function //importing function method from the file otherFile.py
@app.route("/")
@app.route("/index")
def index():
baz = function() //the function imported from your file otherFile.py
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask
app = Flask(__name__)
app.run(host='127.0.0.1', debug=True)
form = myForm(request.form, csrf_enabled=False)
__init__.py
file under the package folder where you declare all the global variables that you need to share between the classes. Then, you should add the code "app = Flask(__name__)
" in the __init__.py
file. Now you can share and use the app variables anywhere if you just import the package name. from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
$ export FLASK_APP=hello.py
$ flask run
* Running on http://127.0.0.1:5000/
current_user.get_id()
is used to get logged in user id in flask.from flask import g
if current_user.is_authenticated():
g.user = current_user.get_id()
Set app.debug
to True and add the below code in your function where you want to enable logging in Flask.import logging
from flask import Flask
app = Flask(__name__)
@app.route('/')
def foo():
app.logger.warning('A warning occurred')
app.logger.error('An error occurred')
app.logger.info('Info')
return "foo"
jsonify()
function to return json in flask.from flask import jsonify
@app.route('/users')
def users():
users= User.query.all()
return jsonify(users)