DEV Community

suraj kumar
suraj kumar

Posted on

Python Flask Tutorial Blog: Learn, Build, Deploy

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:

  1. Beginner-friendly – Flask’s simple syntax makes it easy for newcomers to start building web apps.
  2. Versatile – Used for small projects, REST APIs, and even large applications.
  3. Industry adoption – Many companies use Flask in production (Netflix, Reddit, Lyft).
  4. 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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now open your browser and go to http://127.0.0.1:5000/. You’ll see the message:

Hello, Flask!
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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.

  1. Create a folder named templates.
  2. Inside it, create a file index.html:
<!DOCTYPE html>
<html>
<head>
  <title>Flask App</title>
</head>
<body>
  <h1>Welcome, {{ name }}!</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
  1. Update app.py:
from flask import render_template

@app.route("/user/<username>")
def user(username):
    return render_template("index.html", name=username)
Enter fullscreen mode Exit fullscreen mode

Now visiting /user/John will display:

Welcome, John!
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Update app.py:

from flask import request

@app.route("/submit", methods=["POST"])
def submit():
    username = request.form["username"]
    return f"Hello, {username}!"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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()
Enter fullscreen mode Exit fullscreen mode

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

  1. Install Heroku CLI.
  2. Create a requirements.txt:
pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. Create a Procfile:
web: python app.py
Enter fullscreen mode Exit fullscreen mode
  1. 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)