Flask in 5 Steps: Your First Web App
Think Building a Web App Is Hard? Not with Flask!
Imagine this: You have an idea for a cool website—maybe a personal blog, a portfolio, or even a simple to-do list app. But the thought of coding it from scratch feels overwhelming. What if I told you that in just five steps, you could have a working web app using Flask, one of Python’s simplest and most powerful frameworks?
Flask is perfect for beginners because it’s lightweight, easy to set up, and requires minimal boilerplate code. By the end of this guide, you’ll have a running web app—and the confidence to build more. Let’s dive in!
Step 1: Install Flask
Before we start, you’ll need:
- Python (3.6 or later) installed on your computer.
- pip (Python’s package manager), which comes with Python.
Open your terminal (Command Prompt, PowerShell, or Terminal) and run:
pip install flask
That’s it! Flask is now installed and ready to use.
💡 Pro Tip: If you’re using a virtual environment (recommended for keeping dependencies clean), create one first with:
python -m venv myenv
source myenv/bin/activate # On Windows: myenv\Scripts\activate
Step 2: Create a Python File
Next, create a new file named app.py
(or any name you like). This will be the heart of your web app.
Open the file in your favorite code editor (VS Code, PyCharm, or even Notepad) and add this basic setup:
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
Flask(__name__)
initializes your app.app.run(debug=True)
starts the server with debugging enabled (useful for development).
Step 3: Write a Route for Your Page
In Flask, a route determines what happens when a user visits a specific URL. Let’s create a simple homepage.
Add this right after app = Flask(__name__)
:
@app.route("/")
def home():
return "Hello, Flask! 🚀"
@app.route("/")
means this function runs when someone visits the root URL (/
).- The function returns the text that will display on the page.
Want to add another page? Easy!
@app.route("/about")
def about():
return "This is my first Flask app! 🎉"
Now, visiting /about
will show this new message.
Step 4: Run the Server
Save your file and go back to the terminal. Navigate to your project folder and run:
python app.py
You’ll see something like:
* Running on http://127.0.0.1:5000/
This means your Flask app is live on your local machine!
Step 5: Open Your Browser—Voilà!
Copy the URL (http://127.0.0.1:5000/
) and paste it into your browser. You should see:
Hello, Flask! 🚀
Try visiting http://127.0.0.1:5000/about
—your second page should appear!
🎉 Congratulations! You’ve just built your first web app with Flask.
What’s Next?
Now that you have a working app, here are some fun ways to expand it:
- Add HTML templates (Flask supports Jinja2 for dynamic pages).
- Build a simple API (return JSON data instead of plain text).
- Connect a database (SQLite is beginner-friendly).
- Deploy it online (try Heroku or PythonAnywhere).
Flask’s simplicity makes it a fantastic tool for learning web development. The best part? You can start small and gradually add complexity.
Your Turn!
What will you create first? A personal blog? A weather app? A habit tracker? Drop your ideas below—I’d love to hear them!
Happy coding! 👨💻👩💻