Your First Flask App: Building a “Hello, World” Website in 5 Minutes

3D isometric illustration of a Flask chemistry icon powering a Hello World website with a 5-minute timer.

You’ve learned Python’s basics. Now it’s time to put them into practice by creating your first Flask app on the web. This guide will help you build your first Flask app with ease.

In our Web Development Framework Guide, we recommended Flask as the best starting point for beginners because it’s simple and fast.

In this tutorial, you will go from an empty folder to a running website in under 5 minutes. It’s an exciting way to get started with your first Flask app.

Step 1: Install Flask

First, you need to install the Flask library necessary for your first Flask app. Open your terminal (command prompt) and run:

pip install Flask

(If that doesn’t work, try pip3 install Flask on Mac/Linux).

Step 2: Write the Code (Just 5 Lines!)

Create a new file named app.py. IMPORTANT: Do NOT name your file flask.py. This will break everything (causing an ImportError).

Copy and paste this code into app.py to continue developing your first Flask app:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World! Welcome to my first Flask site."

if __name__ == "__main__":
    app.run(debug=True)

What does this code do?

  1. from flask import Flask: Imports the Flask tool.
  2. app = Flask(__name__): Creates your web application.
  3. @app.route("/"): This is a “decorator.” It tells Flask, “When a user visits the homepage (/), run the function immediately below this line.”
  4. def home():: A standard Python function that returns the text to display in the browser.
  5. app.run(debug=True): Starts the web server. debug=True means if you make a mistake, Flask will show you a helpful error page in the browser.

Step 3: Run Your App

Go back to your terminal, make sure you are in the same folder as your app.py file, and run your first Flask app:

python app.py

(Or python3 app.py)

You should see output like this:

 * Serving Flask app 'app'
 * Debug mode: on
 * Running on http://127.0.0.1:5000

Step 4: View It in Your Browser

Copy that URL (http://127.0.0.1:5000) and paste it into Chrome, Firefox, or Safari.

You should see: “Hello, World! Welcome to my first Flask site.”

Conclusion

Congratulations! You just turned your computer into a web server. Your journey with your first Flask app is just beginning.

  • You installed a third-party library (Flask).
  • You wrote a Python script that listens for web requests.
  • You viewed your Python code in a web browser.

This is just the beginning. Next, you can learn how to use HTML templates to make your site look beautiful, instead of just returning plain text.

Similar Posts

Leave a Reply