Flask Framework Overview
What is Flask and why use it?
• Lightweight Python web framework: Flask is a “micro” framework for building websites and web
apps in Python. It only includes the core tools (like handling web requests and routing) to get you
started quickly 1 . Because of this simplicity, Flask is easy to learn and lets beginners get a basic
app up and running in just a few lines of code 1 2 .
• Microframework (high flexibility): Flask is called a micro framework because it does not bundle
extra features like databases or user login by default 3 . You add only what you need via
extensions. This minimal design gives you full control: you decide which libraries or tools to include.
As one guide notes, “Flask is a lightweight web framework… classified as a microframework… [letting you]
build applications with custom components” 2 3 .
• Quick setup and scaling: Setting up Flask is fast. You can start with a single Python file and grow
the app later. Flask’s good documentation and simple defaults mean beginners can “get started
quick and easy” 1 2 . You use a virtual environment, install Flask with pip , and write just a few
lines to see a working web page.
Key features of Flask
• URL Routing (mapping): Flask lets you map web addresses (URLs) to Python functions using the
@[Link]() decorator. For example:
@[Link]("/")
def home():
return "Welcome to my homepage!"
This means when someone visits "/" , Flask runs the home() function. As the docs explain, “To tie
a function to a URL, use the route() decorator” 4 . Routing makes it simple to add multiple pages
or endpoints (just use more @[Link] lines for different URLs).
• Templating with Jinja2: Flask automatically uses the Jinja2 template engine to generate HTML
pages. Instead of writing raw HTML in your Python code, you create HTML files with placeholders
(like {{ name }} ) in a templates/ folder. In your route you call render_template . For
example:
@[Link]("/hello")
def hello():
return render_template("[Link]", name="Alice")
This fills in the {{ name }} in [Link] with “Alice”. Flask’s integration with Jinja2 is built-in
5 , so you get features like template inheritance and automatic escaping of unsafe input.
1
• Built-in development server & debugger: Flask comes with a simple web server for development.
You run your app with flask run and Flask listens on [Link] by default. In
debug mode, the server auto-reloads if you change your code and shows detailed error pages in the
browser if something goes wrong 6 . This makes testing and fixing errors fast. In short, Flask’s
built-in server and debugger help you see changes instantly without extra setup 6 .
• Handling requests and HTTP methods: Flask provides easy access to request data and different
HTTP methods. By default, routes respond to GET requests. You can allow other methods (like
POST ) with an argument. For example:
@[Link]('/submit', methods=['GET', 'POST'])
def submit():
if [Link] == 'POST':
username = [Link]['username']
return f"Hello, {username}!"
else:
return render_template('[Link]')
Here the route shows a form on a GET request and processes the form data on POST. The official
documentation uses a similar example for a login form 7 . This makes building RESTful APIs or
form-handling pages straightforward.
• Extensions and modular code: Flask’s core is minimal, but there is a rich ecosystem of extensions.
Popular ones include Flask-SQLAlchemy (databases), Flask-Login (authentication), Flask-
WTF (forms) and many others. These plug into Flask seamlessly as if they were built-in 3 . Flask also
supports Blueprints – a way to split a large app into modules or components. You can organize
routes and logic into separate files (blueprints) and register them on the main app 8 . This helps
keep bigger projects organized.
• Built-in testing support: Flask is designed to be easy to test. It provides a test_client() that
lets you simulate requests to your application in code. This makes it easy to write automated tests
for your routes and logic. In fact, Flask’s own documentation notes that “support for unit testing is
built-in” 9 , so you don’t need any extra tools to start testing your app.
Step-by-step: Building a Simple Flask App
1. Install Flask: Open a terminal (command prompt) and install Flask using pip. If you use a virtual
environment (recommended), activate it first. Then run, for example:
pip install Flask
This downloads and installs Flask and its dependencies. (Official tutorials show this as the first step
10 .)
2. Write the app code: Create a new Python file (e.g. [Link] ) and write a small Flask app. For
example:
from flask import Flask
app = Flask(__name__)
2
@[Link]("/")
def home():
return "Hello, World!"
This code does three things: (1) imports the Flask class, (2) creates an app object, and (3) defines a
function home() that runs when the “/” URL is visited (because of @[Link]("/") ). It simply
returns “Hello, World!” as plain text.
3. Run the development server: In the terminal, set the FLASK_APP environment variable to your file,
then start the server. On Linux/macOS you might do:
export FLASK_APP=[Link]
flask run
On Windows (cmd) you’d do:
set FLASK_APP=[Link]
flask run
After running, you should see output telling you the server is running. Open a web browser to
[Link] (or localhost:5000 ). You should see “Hello, World!” displayed.
(This matches the example in the Flask docs 11 12 .)
4. Add another page: To make more pages, just add more routes. For example, add in [Link] :
@[Link]("/about")
def about():
return "About this site"
Now restarting the server (if needed) and visiting [Link] will show
“About this site.” This demonstrates how easy it is to serve multiple URLs.
5. Use HTML templates: Instead of returning plain text, create a templates folder for HTML. Make a
file templates/[Link] containing, say, <h1>Welcome!</h1> . Then modify your route:
from flask import render_template
@[Link]("/")
def home():
return render_template("[Link]")
Flask will load and render that HTML file. (By default it looks in templates/ 13 .) You can put
placeholders in your HTML and pass values from Python, as mentioned above.
6. Handle a form (optional): For example, to add a simple input form, create
templates/[Link] :
3
<form method="post">
<input name="username" placeholder="Enter your name">
<input type="submit">
</form>
Then add a route in [Link] to show the form and process it:
from flask import request
@[Link]('/greet', methods=['GET','POST'])
def greet():
if [Link] == 'POST':
name = [Link]['username']
return f"Hello, {name}!"
else:
return render_template('[Link]')
Visiting /greet will display the form (GET) and after submitting it will greet the user (POST). This
follows the same pattern shown in Flask’s documentation 7 .
Flask vs Django
• Complexity: Flask has a smaller, simpler core than Django. A recent comparison notes “Flask is a
lightweight micro-framework… ideal for simple… apps, while Django is a full-featured framework best for
scalable, feature-rich projects.” 14 . In other words, Flask gives you only the basics, whereas Django
comes with many built-in parts (ORM, admin panel, authentication, etc.). This makes Django more
complex under the hood.
• Flexibility: Flask is very flexible because it doesn’t force you to use specific tools. You pick your
database, form library, or any component you want. Django is more “opinionated”: it has its own
ways (for example, a built-in ORM and a specific project layout). That means Django can get you up
and running quickly with its defaults, but Flask lets you customize everything. As noted above, Flask’s
“build from scratch” style contrasts with Django’s many ready-made features 14 .
• Setup and structure: A basic Flask app can be just one file and a few lines of code, so you can start
very quickly. Django usually starts with a project template ( django-admin startproject ) and
creates a whole directory structure. In practice, beginners often find Flask faster to set up for a
simple app, while Django takes more initial setup because of its extra layers. In fact, one source says
Flask “requires fewer lines of code” and is easy for beginners, whereas Django “requires extensive
expertise” for large projects 15 .
• Use cases: Flask is best for small-to-medium projects or services where you want control. It’s
popular for simple websites, REST APIs, prototypes, or microservices. Django is designed for larger,
full-featured websites – for example, anything needing a built-in admin interface, user management,
or complex data modeling. In short, use Flask when you need a lightweight or highly-custom app,
and use Django when you want an all-in-one solution with many features ready out-of-the-box.
• Learning curve: Flask is generally easier for beginners. Its minimalist design and straightforward
tutorials mean new developers can see results quickly 15 . Django has more “magic” under the hood
4
and more to learn upfront (models, MVT pattern, etc.), so its learning curve is steeper. As one guide
puts it, Flask has “a less steep learning curve than Django” 16 .
Sources: Official Flask and tutorial documentation, as well as recent comparisons, explain these points 1
3 2 5 6 7 14 15 . These emphasize Flask’s simplicity and flexibility versus Django’s full-featured
approach.
1 Welcome to Flask — Flask Documentation (3.1.x)
[Link]
2 Flask: A Beginner-Friendly Web Framework and Its Real-World Use Cases | by Abhishek Shaw | Medium
[Link]
3 Flask in Visual Studio tutorial Step 1, Flask basics | Microsoft Learn
[Link]
4 5 6 8 9 Introduction to Flask Framework its Uses and Components
[Link]
7 13 Quickstart — Flask Documentation (3.1.x)
[Link]
10 11 12 Flask Intro — Python Beginners documentation
[Link]
14 15 16 Flask vs Django: Let's Choose Your Next Python Framework - Kinsta®
[Link]