Python has become one of the most popular programming languages in the world, not just for data science but also for web development. When it comes to building lightweight, fast, and scalable web applications in Python, Python Flask Tutorial is one of the most preferred frameworks. In this tutorial blog, you will learn what Flask is, how it works, and how to create and deploy your first Flask application step by step.
What is Flask?
Flask is a micro web framework written in Python. It is called a micro framework because it is lightweight and does not come with too many built-in tools like Django. Instead, Flask gives developers flexibility to choose components such as databases, authentication, or template engines.
Key features of Flask include:
Lightweight and flexible – perfect for small to medium-sized projects.
Built-in development server and debugger.
RESTful request handling for building APIs easily.
Jinja2 templating engine for rendering HTML pages dynamically.
Extensible – you can add libraries for forms, authentication, or databases as needed.
Why Learn Flask?
There are several reasons why Flask is an excellent choice for developers:
- Beginner-friendly – Flask’s simple syntax makes it easy for newcomers to start building web apps.
- Versatile – Used for small projects, REST APIs, and even large applications.
- Industry adoption – Many companies use Flask in production (Netflix, Reddit, Lyft).
- Integration – Works well with front-end technologies like React, Angular, or Vue.
Setting Up Flask
Before building your first application, make sure Python is installed on your system.
Step 1: Install Flask
Open your terminal or command prompt and run:
pip install flask
Step 2: Create a Project File
Create a new file named app.py.
Your First Flask Application
Here’s the simplest Flask app:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
Explanation:
-
Flask(__name__)
initializes the Flask app. -
@app.route("/")
defines the route (homepage). -
home()
function returns the message. -
app.run(debug=True)
starts the development server with debugging enabled.
Run the file:
python app.py
Now open your browser and go to http://127.0.0.1:5000/. You’ll see the message:
Hello, Flask!
Working with Routes and Views
Flask allows you to define multiple routes for different pages.
@app.route("/about")
def about():
return "This is the About page"
Now, if you visit /about
, Flask will serve a different page.
Rendering HTML Templates
Flask uses the Jinja2 template engine to generate dynamic HTML pages.
- Create a folder named templates.
- Inside it, create a file index.html:
<!DOCTYPE html>
<html>
<head>
<title>Flask App</title>
</head>
<body>
<h1>Welcome, {{ name }}!</h1>
</body>
</html>
- Update
app.py
:
from flask import render_template
@app.route("/user/<username>")
def user(username):
return render_template("index.html", name=username)
Now visiting /user/John
will display:
Welcome, John!
Handling Forms in Flask
Flask can capture form data submitted via HTML.
<form method="POST" action="/submit">
<input type="text" name="username" placeholder="Enter your name">
<button type="submit">Submit</button>
</form>
Update app.py
:
from flask import request
@app.route("/submit", methods=["POST"])
def submit():
username = request.form["username"]
return f"Hello, {username}!"
Connecting Flask with a Database
Flask doesn’t force a database on you. You can use SQLite, MySQL, or PostgreSQL.
Here’s an example with SQLite using Flask-SQLAlchemy:
pip install flask-sqlalchemy
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
db.create_all()
This creates a User table in SQLite.
Deploying Your Flask App
Once your app is ready, you can deploy it to make it accessible online. Popular options include:
Heroku – Free and easy to start.
PythonAnywhere – Beginner-friendly hosting for Python apps.
AWS / Google Cloud / Azure – For large-scale production apps.
Docker – For containerized deployment.
Example: Deploy to Heroku
- Install Heroku CLI.
- Create a
requirements.txt
:
pip freeze > requirements.txt
- Create a
Procfile
:
web: python app.py
- Push your code to Heroku and deploy.
Advantages of Flask
- Minimalistic and easy to learn.
- Allows rapid prototyping.
- Flexible, unlike Django which has strict conventions.
- Large community support and many plugins available.
When to Use Flask?
Flask is the right choice if you:
- Want to learn web development fundamentals.
- Need to build small to medium applications quickly.
- Want flexibility to choose your own libraries.
- Plan to build REST APIs.
If you need a full-featured CMS, Django might be a better choice. But if you want simplicity and speed, Flask is ideal.
Conclusion
In this Python Flask Tutorial Blog: Learn, Build, Deploy, we explored what Python flask Tutorial is, how to install it, create routes, render templates, handle forms, connect to a database, and deploy applications online.
Flask is beginner-friendly yet powerful enough to run in production for large applications. By practicing these examples and gradually exploring Flask extensions, you can become proficient in Python web development.
Start with simple apps, experiment with forms and databases, and then deploy your project online. With Flask, you’ll learn not only web development but also the core concepts behind modern web frameworks.
Top comments (0)