Building a web application with Python is easier than many beginners expect. One of the best tools for this is Flask. It is lightweight, simple, and flexible. You can create anything from a personal portfolio to a complete web platform.
In this Flask tutorial, you will learn how Flask works, how to install it, and how to build your first web app step by step. Even if you are new to Python, you can follow this guide without problems.
If you are still learning Python basics, it may help to first read Python for Beginners and how to install Python.
What Is Flask?
Flask is a micro web framework written in Python. A framework is a collection of tools that helps developers create applications faster.
Flask focuses on simplicity. It gives you the essentials needed to build a web app without forcing complex rules.
With Flask, you can:
- Create websites and dashboards
- Build APIs
- Handle forms and user input
- Connect databases
- Create login systems
- Deploy apps online
Many developers like Flask because it is beginner-friendly and highly customizable.
Tip: Flask is an excellent first framework for people starting with web development using Python.
Why Use Flask for Web Development?
There are many Python frameworks available today. Flask remains one of the most popular options because it is fast to learn and practical for real projects.
| Feature | Flask | Django |
|---|---|---|
| Difficulty | Easy | Medium |
| Flexibility | High | Medium |
| Built-in Features | Minimal | Many |
| Best For | Small and medium apps | Large platforms |
If you want to compare frameworks later, you can also explore what Django is.
Installing Flask on Your Computer
Before creating your first app, you need to install Flask.
First, make sure Python is installed correctly.
Open your terminal or command prompt and run:
python --versionIf Python appears, you are ready.
Now create a virtual environment. This keeps your project organized and avoids package conflicts.
python -m venv venvActivate the environment:
Windows:
venv\Scripts\activateMac/Linux:
source venv/bin/activateNow install Flask:
pip install flaskYou can learn more about virtual environments in this Python venv guide.
Create Your First Flask App
Now comes the fun part.
Create a file called app.py.
Add the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)This simple application creates a webpage that displays the text “Hello, Flask!”.
Run the app using:
python app.pyYou should see something similar to this:
Running on http://127.0.0.1:5000/Open the address in your browser.
Your first Flask web app is now running.
Understanding Flask Routes
Routes tell Flask what content should appear for each URL.
For example:
@app.route("/")
def home():
return "Home Page"The slash / represents the homepage.
You can create another route:
@app.route("/about")
def about():
return "About Page"Now visiting /about shows different content.
Routes are the foundation of Flask applications.
Using HTML Templates in Flask
Returning plain text works, but real websites use HTML.
Create a folder called templates.
Inside it, create a file called index.html.
<h1>Welcome to My Flask Website</h1>
<p>This is my first web app.</p>Now update your Python code:
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)Flask will now load the HTML page automatically.
This approach helps separate Python logic from website design.
Adding CSS Styling to Your Flask App
Without styling, websites look very plain.
Create a folder named static.
Inside it, create a file called style.css.
body {
font-family: Arial;
background-color: #f4f4f4;
padding: 40px;
}Now connect the CSS file inside your HTML:
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">Your Flask app now has custom styling.
If you want to build more modern interfaces, you may also like this Tailwind CSS project.
Handling User Input with Forms
Interactive applications need forms.
Here is a simple example:
from flask import Flask, request
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
name = request.form["name"]
return f"Hello, {name}"
return """
<form method="POST">
<input type="text" name="name">
<button type="submit">Send</button>
</form>
"""
if __name__ == "__main__":
app.run(debug=True)This example asks for the user’s name and displays a greeting.
Forms are essential for login systems, search bars, contact pages, and many other features.
Connecting Flask to Databases
Most modern web applications store data.
Flask works very well with databases like SQLite, MySQL, and PostgreSQL.
SQLite is perfect for beginners because it does not require installation.
Example:
import sqlite3
connection = sqlite3.connect("database.db")You can use databases to store:
- User accounts
- Messages
- Products
- Blog posts
- Application settings
You can also learn more in Python with SQLite and connecting Python to MySQL.
Deploying Your Flask App Online
Running your app locally is useful, but eventually you will want other people to access it online.
You can deploy Flask applications using platforms like:
- Render
- Railway
- PythonAnywhere
- Heroku alternatives
- DigitalOcean
Before deployment, create a file called requirements.txt:
pip freeze > requirements.txtThis file lists all installed packages.
Many hosting platforms automatically use this file during deployment.
You can also explore the official Flask documentation at Flask Docs.
Common Flask Beginner Mistakes
Many beginners face similar problems while learning Flask.
Here are some common issues:
- Forgetting to activate the virtual environment
- Typing the wrong route URL
- Incorrect indentation
- Missing template folders
- Not installing Flask properly
If your code shows import errors, check this ImportError guide.
If Flask does not start correctly, verify that Python is installed and available in your terminal.
You can also learn debugging basics in this VS Code debugging tutorial.
What Can You Build with Flask?
Flask is much more powerful than many people think.
Here are some project ideas:
- Personal portfolios
- To-do list apps
- Weather dashboards
- REST APIs
- Online stores
- Admin panels
- Chat applications
- Automation dashboards
Many startups begin with Flask because it allows fast development and easy testing.
You can also combine Flask with APIs and automation tools using concepts from REST APIs in Python.
Conclusion
This Flask tutorial showed how simple it is to build a web app with Python. You learned how to install Flask, create routes, use HTML templates, add CSS, process forms, connect databases, and deploy applications online.
Flask is one of the best frameworks for beginners because it removes unnecessary complexity and helps you focus on building real projects quickly.
The best way to improve now is through practice. Start small, experiment with new features, and gradually build larger applications.
Every professional developer started with a simple “Hello, World” project.
Perguntas Frequentes (FAQ)
1. What is Flask in Python?
Flask is a lightweight Python framework used to build websites and web applications.
2. Is Flask good for beginners?
Yes. Flask is simple, flexible, and easier to learn than many other frameworks.
3. Do I need HTML to use Flask?
Basic HTML knowledge helps a lot when building web pages with Flask.
4. Can Flask create APIs?
Yes. Flask is widely used to create REST APIs and backend services.
5. Is Flask free?
Yes. Flask is open-source and completely free to use.
6. Which database works best with Flask?
SQLite is great for beginners, while PostgreSQL is popular for larger apps.
7. Can Flask be used for large projects?
Yes. Many production applications use Flask successfully.
8. How long does it take to learn Flask?
You can learn the basics in a few days with consistent practice.
9. Do I need JavaScript with Flask?
Not always, but JavaScript improves interactive features in web apps.
10. Is Flask better than Django?
Flask is simpler and more flexible. Django includes more built-in features.






