Build a Flask Web App Quickly: From Setup to Blueprint Modularization
This guide walks you through installing Flask, structuring a project with webapp, templates, and static files, writing core code and routes, using Blueprint for modularization, rendering Jinja2 templates, and running the app, providing complete code snippets and configuration tips for rapid backend development.
Preparation
Install Flask: pip install flask. Quick start guide: http://docs.jinkan.org/docs/flask/quickstart.html#quickstart
Quick Build
Project layout under the root directory:
webapp package containing Flask code, with __init__.py.
templates directory for template files.
static directory for js, css, etc., with a sub‑directory for jquery and echarts.
app.py as the entry point.
Basic Components
Directory structure illustration:
Code Example – /webapp/__init__.py
# /webapp/__init__.py
from flask import Flask, jsonify
app = Flask("myweb")
@app.route("/")
def index():
return "hello flask"
@app.route("/json", methods=["GET"])
def getjson():
d = {"a":1, "b":2, "c":3}
return jsonify(d)
print(*filter(lambda x: not x[0].startswith("__") and x[1], app.__dict__.items()), sep="
")
print("-"*30)
print(app.url_map)
print(app.template_folder)
print(app.static_folder)
print("-"*30)Key concepts:
Application: the Flask instance that serves web requests (WSGI entry).
View function: returns the response content.
Route: created with @app.route decorator linking a URL path to a view.
Running the App
# /main.py
from webapp import app
if __name__ == "__main__":
app.run("127.0.0.1", port=8080, debug=True)Execute python main.py to start.
Blueprint (Modularization)
When many routes exist, organize them with Flask Blueprint.
# /webapp/books.py
from flask import Blueprint, jsonify, render_template
bpbooks = Blueprint("booksapp", __name__, url_prefix="/books")
@bpbooks.route("/", methods=["GET", "POST"])
def getall():
books = [
(1, "java", 20),
(2, "python", 40),
(3, "linux", 50)
]
return jsonify(books)Register the blueprint in /webapp/__init__.py:
from flask import Flask, jsonify
from .books import bpbooks
app = Flask("myweb")
# existing routes ...
app.register_blueprint(bpbooks, url_prefix="/bookss")Note: the url_prefix must start with “/”.
Blueprint Parameters
name : key used in app’s blueprint dictionary.
import_name : usually __name__, determines module path.
root_path : path of the blueprint module; defaults to calculated path.
template_folder : folder for blueprint templates.
url_prefix : URL prefix for the blueprint; can be overridden when registering.
Templates
Flask uses Jinja2. Place HTML files in templates directory, e.g., templates/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>xdd web</title>
</head>
<body>
<h2>欢迎使用flask框架</h2>
<hr>
{% for x in userlist %}
{{ x }}
{% endfor %}
</body>
</html>Update the index view to render this template:
from flask import Flask, jsonify, render_template
app = Flask("myweb")
@app.route("/index.html")
def index():
return render_template("index.html", userlist=[
(1, "tom", 20),
(1, "json", 30),
])Both the app and a blueprint use their own root_path combined with template_folder to locate templates, which can be inspected via app.jinja_loader.searchpath.
For further learning, see the linked cloud computing course (advertisement removed).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
