Skip to content
Home » Introduction to  Flask – Part 1 – Installing Flask and Creating a Basic App

Introduction to  Flask – Part 1 – Installing Flask and Creating a Basic App

Spread the love

In this series we’ll learn how to create web applications using Flask. All you need to get started is a basic knowledge of the Python programming language. So, without further ado, let jump right in. In this part of the series we’re going to install Flask and create a basic app. Here are the steps to follow:

– Create your project directory using the terminal:

– Go into the directory:

– Create a virtual environment:

– Open in Visual Studio Code:

Now you can see the virtual environment in VSC:

virtual environment

You must tell VSC to use this environment for this project. In VSC hit Ctrl + Shift + P and click on Select Interpreter. Then select the newly-created environment:

This environment will be used throughout the entire project.

Install Flask:

For this basic application we’re going to create just a single file. Add the app.py file to the root of the project:

Add the following code to the file:

It imports the Flask class and creates the app.

Now we have to add endpoints (routes). To do that we use the @app.route decorator to which we pass the path:

Here we have just a slash in the path, which is the default path. Next, we have to add a function that will return some HTML or a simple string that we can see when we navigate to this endpoint:

Let’s run the app. To do that, we just call the app.run method and pass the following parameters:

– the host (we can use localhost – 127.0.0.1 ,a local IP address, or both by setting the host to 0.0.0.0)

– the port (the default port is 5000, but we can change it)

– whether we want to run it in debug mode (True if we do and we do while we’re developing the app so that all changes in code are updated automatically. When we deploy the app, we’ll set it to False.)

So, here’s the code:

If you now run the app and navigate to the specified address:

you will see the string we created:

the string we created

In the next part of the series we’ll be talking about routing in Flask.


Spread the love

Leave a Reply