Skip to main content

Creating the first Flask application

In this tutorial, we'll be guiding you through creating your very first Flask application. Flask is a popular web framework for Python, known for its simplicity, flexibility, and fine-grained control. By the end of this article, you will have set up a basic Flask environment, and created a simple web application. Let's dive in!

Flask Environment Setup

Before we can start creating our Flask application, we need to set up our environment. Flask requires Python to run, so make sure you have a version of Python installed on your machine. Flask supports Python 3.5 and newer.

Step 1: Install Flask

The first step is to install Flask. We do this using pip, Python's package manager. Open your terminal and type the following command:

pip install flask

This will download and install Flask onto your machine.

Step 2: Verify Flask Installation

To verify that Flask has been installed correctly, you can use the following command in your terminal:

python -m flask --version

This should output the version of Flask that you have installed.

Creating Your First Flask Application

Now, we can start creating our Flask application.

Step 1: Create a New Python File

Create a new Python file in your preferred text editor or IDE. You can name this file anything you like, but for this tutorial, we'll call it app.py.

Step 2: Import Flask

At the top of app.py, you will need to import the Flask class from the flask module. Add the following line to your file:

from flask import Flask

Step 3: Create an Instance of the Flask Class

Next, create an instance of the Flask class. This will be our web application. Add the following line to app.py:

app = Flask(__name__)

Step 4: Define a Route

In Flask, a route is a URL pattern that the application responds to. To define a route, we use the @app.route() decorator, followed by a function that returns the response.

Add the following code to app.py to define a route for the URL /:

@app.route('/')
def home():
return "Hello, World!"

Step 5: Run the Application

Finally, we need to run our application. Add the following code to the bottom of app.py:

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

This tells Python to run the application if the script is run directly. The debug=True argument enables debug mode, which will give us more detailed error messages if something goes wrong.

Running Your First Flask Application

To run your application, open your terminal, navigate to the directory where app.py is located, and type the following command:

python app.py

This will start your Flask application. You can view your application by opening a web browser and navigating to http://localhost:5000/.

You should see a page that says "Hello, World!". Congratulations, you've just created your first Flask application!

In following tutorials, we will dive deeper into Flask's features like templates, forms, and databases. For now, enjoy your success and experiment with what you've learned. Happy coding!