CodingNic

Flask Fundamentals

Your First Flask App

Flask Fundamentals 15 min read

Your First Flask App

Your First Flask App

Now it’s time to build your first web application using Flask.

This will help you connect everything you’ve learned so far.

Installing Flask

Before starting, install Flask in your virtual environment:

id="install-flask"
pip install flask

Creating Your First App

Create a new file called app.py and add the following:

id="first-app"
from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "Hello, World!" if __name__ == "__main__": app.run(debug=True)

Understanding the Code

Let’s break it down:

  • Flask is imported to create the app
  • app = Flask(__name__) initializes the application
  • @app.route("/") defines a route
  • The function returns a response
  • app.run() starts the server

Running the Application

Run the app using:

id="run-app"
python app.py

Then open your browser and go to:

id="url"
http://127.0.0.1:5000/

You should see:

code
Hello, World!

How It Works

Here’s what happens when you open the page:

  • The browser sends a request
  • Flask receives the request
  • Flask matches the route
  • The function runs
  • A response is returned

Flask Flow Diagram

Why This Matters

This simple app demonstrates the core of web development:

  • Handling requests
  • Defining routes
  • Returning responses

Everything you build in Flask expands on this pattern.


Challenge

Add more pages to your Flask app.

What to do

  1. Open your app.py file
  2. Below your existing route, add a new route for /hello
  3. Create a function that returns a custom message (example: “Welcome to my app”)
  4. Add another route /about
  5. Return a short message about yourself

What you should see

  • Visiting /hello shows your custom message
  • Visiting /about shows your description

Summary

  • You created your first Flask app
  • You defined a route
  • You returned a response
  • You ran a local server

In the next lesson, you’ll learn how routing works in more detail.