Flask in Python – In this Series, we are going to learn the complete Flask framework step by step. This is part one of this series.
Flask is a lightweight Python framework mainly used to develop web apps. Flask is one of the most popular python web apps frameworks. It is based on the Pocoo projects Werkzeug and Jinja2. I usually use flask for deploying machine and deep learning models for end-users.
The learning curve for flask is easy to compare to other python web application framework.
In this tutorial, we are going to learn how to install flask in python on windows.
Python must be installed in your system before following steps below
So Lets Start Without wasting further time…
Here I am going to use a practical approach directly. Lets Start with Hello Flask App in Flask
First Create a folder in your system where you want to develop the flask app.
In my case, I have created a folder in the flask folder name App and inside that, I created folder name Hello.
Open the Command prompt and go to that folder location using the CD command
And create a virtual environment first for that on command prompt write command like this
- G:/Flask/Apps/Hello> py -m venv env env is the name of the environment
- Than activate environment by writing following command in command prompt G:/Flask/Apps/Hello>env\Scripts\activate
- Once it is done install flask with the following command – G:/Flask/Apps/Hello>pip install flask
Please see the screenshot below for all above commands
Once the installation is done
- Create a file app.py in your app folder(My case its Hello) using your favorite editor. I am using Notpad++.(You can use whatever editor you want to use or comfortable with)
- Set file to run for the environment via command (env) G:\Flask\Apps\Hello>Set FLAST_APP=app.py
- As our file name is app.py So when we run environment it will automatically run this file
- Now let’s write code in our app.py file.
- Line 1 We are importing flask library from Flask class
- Line 3 we instantiate the class.
- Line 5 we are creating a route. means URL which we are going to use.
- Line 7 we are defining the method name index.
- In that method, we just simply return the Html code “Hello Flask!” with header tag.
- Once we have done with this save the file and back to command prompt
- Run the below command on prompt(env) G:\Flask\Apps\Hello>flask run
- If everything goes well your First Flask code is ready to run
- Open URL 127.0.0.1:5000 in your browser and you have done.Hurry, Your First flask app is ready !!!
Once you did that and want to go out from env
(env) G:\Flask\Apps\Hello> deactivate
I hope this helps you to install Flask in python and creating your first program in the flask.
There is one more way to run Flask App. Let’s see that too
Another approach to run flask App
In this approach add the following code in existing code of app.py at the end of that file
if __name__ == ‘__main__’:
and now directly run the below command without activating environment as we have done previously
It will run app the same way, by default it will use port number 80. And if this port is busy with some other service in your system in that case you have to specify the port as code shown below. Otherwise, it will not start the webserver.
instead of that
put this code
now it will take port 5000 and you can open URL 127.0.0.1:5000 in your browser and you have done.
Next tutorials we are going to discuss some other interesting aspects flask in python and routing in the flask