Welcome to this Flask tutorial series. In this series, we are going to learn about Flask – Python-based web framework. Flask is one of the most popular web frameworks for Python. Flask is easy to use, flexible and has less learning curve.
You can checkout Flask’s official website to learn more about the Flask.
In this post, we are going to install Flask in our system and code a simple “Hello World” application.
Here is the Video version of the article.
Before installing Flask, make sure your Python version is upgraded and it is version 2.7 or above. You can check the Python version using the following command.
Virtualenv is a python environment builder. It helps you to create multiple Python environments in your system to avoid package and version conflict. This is a very handy and useful tool and I highly recommend you to use it.
If you are running a Linux based system, you can install Virtualenv using the following command.
You can also install it using pip.
Now let’s install Flask. Create a new folder and switch to it using the terminal.
Run this command to create a virtual environment.
Now activate the virtual environment using the following command.
You should see (env) on the left side of the terminal.
Install Flask using the following command.
That’s it. You have successfully installed Flask in your machine.
Building First Flask Program
Before wasting any time, here is our code to return Hello World message when the API is hit.
app = Flask(__name__)
return 'Hello World'
if __name__ == '__main__':
Save the code in a file name hello.py and run it using the following command.
Here is the output in the terminal.
Now open your browser and go to the localhost:5000 URL.
There you go, you just build the first program using the Python Flask framework. Let’s understand the code.
First, we imported the Flask library in our code using the following line.
Then, we created a new instance of Flask and provided the name using the __name__ variable.
In the next line, we have created a new route. Routes are endpoints that can be used to design the structure of your web application.
return 'Hello World'
In the last line, we are are running our app.
In the next post, we are going to learn about Flask routes and how to build API’s using Flask.