Flask is a simple web framework written in Python.
In this article, we’ll look at how to develop simple Python web apps with Flask.
Blueprints
We can use Blueprints to modularize our apps with Flask.
For example, we can write:
app.py
from flask import Flask, request
from simple_page import simple_page
app = Flask(__name__)
app.register_blueprint(simple_page)
simple_page.py
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
simple_page = Blueprint('simple_page', __name__,
template_folder='templates')
@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
try:
return render_template('pages/%s.html' % page)
except TemplateNotFound:
abort(404)
templates/pages/users.html
<p>users</p>
In the simple_pages.py
file, we create the blueprint.
We create it with the Blueprint
class.
The first argument is the name.
The 3rd argument is the template folder location.
Then to create a route, we call route
on the simple_page
blueprint.
The defaults
parametrer has a dict that has the default value for the page
URL parameter.
Then in the function, we call render_template
to render the template with the given file name.
In app.py
, we call app.register_blueprint
to register the blueprint.
When we go to http://127.0.0.1:5000/users, we see ‘users’ displayed.
We can add a URL prefix for the blueprint.
For example, we can write:
app.py
from flask import Flask, request
from simple_page import simple_page
app = Flask(__name__)
app.register_blueprint(simple_page, url_prefix='/pages')
simple_page.py
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
simple_page = Blueprint('simple_page', __name__,
template_folder='templates')
@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
print(page)
try:
return render_template('pages/%s.html' % page)
except TemplateNotFound:
abort(404)
templates/pages/users.html
<p>users</p>
We add the url_prefix
parameter into the app.register_blueprint
to add the URL prefix.
When we go to http://127.0.0.1:5000/pages/users, we see ‘users’ displayed.
Static Files
We can add a static files folder with Blueprints.
To do that, we pass in the static_folder
parameter.
To do that, we write:
app.py
from flask import Flask, request
from simple_page import simple_page
app = Flask(__name__)
app.register_blueprint(simple_page, url_prefix='/pages')
simple_page.py
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
simple_page = Blueprint('simple_page', __name__,
template_folder='templates',
static_folder='static')
@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
print(page)
try:
return render_template('pages/%s.html' % page)
except TemplateNotFound:
abort(404)
static/styles.css
body {
font-weight: bold;
}
templates/pages/users.html
<link rel="stylesheet" href="{{ url_for('simple_page.static', filename='styles.css')
}}">
<p>users</p>
In simple_page.py
, we add the static_folder
parameter to set the static folder location.
Then we put the styles.css
in the static
folder and set the styles we want.
In users.html
, we have:
url_for('simple_page.static', filename='styles.css')
to get the path to the styles.css
file.
Command Line Interface
We can add commands into our Flask app.
For example, we can write:
from flask import Flask
import click
app = Flask(__name__)
@app.cli.command("create-user")
@click.argument("name")
def create_user(name):
print('create %s' % name)
@app.route('/')
def hello_world():
return 'hello world'
We add the create-user
command with the @app.cli.command
decorator.
The name
parameter is obtained from the name
command-line argument.
The command-line argument is specified by the @click.argument
decorator.
So when we run:
flask create-user admin
in the command line, we see ‘create admin’ displayed.
Also, we can put commands in groups.
For example, we can write:
from flask import Flask
import click
from flask.cli import AppGroup
app = Flask(__name__)
user_cli = AppGroup('user')
@user_cli.command('create')
@click.argument('name')
def create_user(name):
print('create %s' % name)
app.cli.add_command(user_cli)
@app.route('/')
def hello_world():
return 'hello world'
to add the 'user'
command group.
Then we specify our commands in the group with the @user_cli.command
decorator.
And we register the command with the app.cli.add_command
method.
Now when we run:
flask user create demo
we see ‘create demo’ displayed.
Conclusion
We can organize our Flask apps with blueprints and add commands to our Flask app.