Getting Started with Flask: Your First Python Web App
Learn how to build your first web app with Flask Python. This beginner-friendly guide covers installation, routing, and rendering HTML templates in under 30 minutes.
If you’ve been learning Python and want to build something you can show in a browser, Flask is the perfect starting point. Flask is a lightweight Python web framework that lets you go from zero to a working web app in minutes. In this guide, I’ll walk you through building your first Flask Python app step by step.
What Is Flask and Why Use It?
Flask is a “micro” web framework — it gives you just enough structure to build web apps without forcing you into a rigid pattern. Unlike Django, which comes with everything built in, Flask lets you add only what you need. That makes it ideal for beginners and small projects.
To install Flask, activate a virtual environment first, then run:
pip install flask
Building Your First Flask Route
A route in Flask maps a URL to a Python function. Create a file called app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "<h1>Hello from Flask!</h1>"
if __name__ == "__main__":
app.run(debug=True)
Run it with python app.py, open your browser to http://localhost:5000, and you’ll see your first Flask page. The debug=True flag means Flask will automatically reload when you save changes — very helpful while developing.
Rendering HTML Templates
Returning raw HTML strings from your functions gets messy fast. Flask has a built-in templating system called Jinja2. Create a templates/ folder and add index.html:
<!DOCTYPE html>
<html>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
Then update your route to use it:
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html", name="Kaikobud")
The {{ name }} in the template gets replaced with whatever you pass from Python. This separation of logic and presentation is a key web development concept.
Conclusion
Flask is one of the best ways to start building real web apps with Python. You’ve learned how to install it, define routes, and render HTML templates — that’s enough to build a personal homepage, a portfolio, or a simple tool. From here, explore adding forms, databases, and user authentication.
Read next: Building a REST API with Python and Flask — take your Flask knowledge further by building an API.
External resource: Flask Official Documentation — the best reference for everything Flask.
Related Articles
Docker for Backend Developers: A Practical Introduction
Learn how Docker works, why backend developers need it, and how to containerize your first Python or Go application in under 30 minutes.
Containerising a Backend Service: From Docker to Kubernetes
A practical walkthrough of containerising a Python backend service with Docker, deploying it to Kubernetes on ECS, and the production gaps that only show up once real traffic hits.
Environment Variables Explained: Keeping Secrets Out of Code
Learn what environment variables are and why every developer needs them. This guide covers how to use .env files, os.environ in Python, process.env in Node.js, and best practices.