Flask¶
lightweight Python web framework
sample¶
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/login")
def login():
return render_template('login.html')
@app.route("/some_internal_api", methods=['POST'])
def do_something():
print(request.json)
For some reason request.get_json()
only works in POST
request
routing¶
route with decorator
from flask import Flask
app = Flask(__name__)
@app.route("/testing", methods=['POST'])
def something():
# /testing with type = POST will go to this function
# do something
print(request.json)
url_for('<function_name>')
will return the route leading to that function
e.g. url_for('something')
will return '/testing'
can use this with redirect('target_url')
/static/path/to/file
fallback route¶
CORS¶
To allow request from another origins, use flask_cors
. Note that browsers will still block localhost
. See https://stackoverflow.com/questions/10883211.
Install flask-cors
Apply the Access-Control-Allow-Origin
header
views / templates¶
Put all your views (html files) in /templates
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
select another folder for template & static files¶
By default, template_folder
= ./templates
and static_folder
= ./static
. To change them, specify them in Flask()
.
https://stackoverflow.com/a/55615550/15493213
passing variables¶
app.py
haha,html
if¶
{% if request.path != '/' %}
<div class="navbar">
<a href="{{ url_for('index') }}">Dashboard</a>
</div>
{% endif %}
static files¶
put static files in /static
use url_for
to reference it
e.g. you have /static/style.css
to link it, do
Relational Database ORM¶
use SQLAlchemy