DEV Community

jlo128456
jlo128456

Posted on

Getting Started with Flask: A Lightweight Framework for Building Web Apps in Python

If you're learning Python and want to dip your toes into web development, Flask is one of the best frameworks to start with. It's lightweight, flexible, and easy to learn — making it perfect for building anything from simple websites to full-featured web applications.

In this post, you'll learn what Flask is, why it's popular, and how to get started with a working example.


What is Flask?

Flask is a micro web framework written in Python. It’s called “micro” not because it lacks features, but because it aims to keep the core simple and extendable.

"Flask gives you the tools, but you’re in charge of the architecture."

Flask doesn't come with built-in ORM, form validation, or authentication — but it integrates easily with extensions like SQLAlchemy, WTForms, and Flask-Login.


Why Choose Flask?

  • Minimal and Flexible – You decide how to structure your app.
  • Beginner-Friendly – Easy to read and learn, especially if you're already familiar with Python.
  • Extensible – Add only what you need via extensions.
  • Active Community – Tons of tutorials, docs, and third-party tools.

Installing Flask

First, set up a virtual environment (recommended):

python3 -m venv venv
source venv/bin/activate  # macOS/Linux
# OR
venv\Scripts\activate     # Windows
Enter fullscreen mode Exit fullscreen mode

Then install Flask:

pip install Flask
Enter fullscreen mode Exit fullscreen mode

Your First 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

Save this as app.py and run:

python app.py
Enter fullscreen mode Exit fullscreen mode

Visit http://127.0.0.1:5000/ in your browser, and you’ll see:

Hello, Flask!
Enter fullscreen mode Exit fullscreen mode

Routing in Flask

@app.route("/about")
def about():
    return "This is the about page."

@app.route("/user/<username>")
def profile(username):
    return f"Hello, {username}!"
Enter fullscreen mode Exit fullscreen mode

Live Demo: Flask + React Power Plan App

Want to see a real Flask app in action?

Try the live Power Plan Finder app here

This full-stack project uses Flask for the backend and React for the frontend. You can sign up, enter your usage history, and explore electricity plans — all built using the concepts discussed in this post.


Flask Folder Structure (Example)

Once your app grows, organize it like this:

myapp/
├── app.py
├── templates/
│   └── home.html
├── static/
│   └── style.css
└── requirements.txt
Enter fullscreen mode Exit fullscreen mode

You can render templates using:

from flask import render_template

@app.route("/")
def home():
    return render_template("home.html")
Enter fullscreen mode Exit fullscreen mode

Adding More: Extensions

Flask supports many extensions, such as:

  • Flask-SQLAlchemy – ORM for database access
  • Flask-WTF – Form validation
  • Flask-Login – User authentication

Install one like this:

pip install flask-sqlalchemy
Enter fullscreen mode Exit fullscreen mode

When to Use Flask

Use Flask if:

  • You want full control over your app’s structure
  • You’re building a small-to-medium web app or API
  • You want to learn web development fundamentals without relying on a large framework

Final Thoughts

Flask is a great choice for developers who enjoy simplicity, clarity, and control. It’s especially well-suited for:

  • REST APIs
  • Admin dashboards
  • Personal projects
  • Prototyping MVPs

If you’re comfortable with Python and want to start building web applications, Flask is the perfect next step.

Top comments (0)