A gentle introduction to Flask 
Published: 2021-08-11 20:54:43

##What is Flask?
According to [wikipedia](https://en.wikipedia.org/wiki/Flask_(web_framework)) **Flask** is micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. Flask makes it possible to easily build web applications.

##Why use flask?
There are other options you could consider to build your web application, so you may be wondering why choose flask? 
Well, Flask  is much easier to learn and doesn't need a large code base to implement a simple web application as we will see later on.

##Getting started
In this article I will be showing you how to build a very simple hello world web application using flask. We will be using python 3.

#Using a virtual environment.
When developing in python, its always important to write your code in a **virtual environment**.

A virtual environment is an isolated environment where dependencies for a particular python project can be downloaded. Each python project should have it's own virtual environment where the specific dependencies are downloaded separate from the main global installation on your system.
With a virtual environment, everyone on the team can be able to run the python project with the same dependencies so this helps solve errors which could arise due to version changes in some dependencies.

###Setting up the virtual environment.
A virtual environment can be easily set up as shown below: 

1. First, fire up your favorite IDE and create a project folder.
2. In the terminal, type `pip3 install virtualenv`.
3. Type `virtualenv env` to create your virtual environment. Note that the `env` after `virtualenv` can be any name you choose to call your virtual environment. So it could be `virtualenv myapp`, or any name that you feel is appropriate. 

After successful creation of the virtual environment, you should be able to see a folder added to your project folder with the name of the virtual environment you just created. 

Now to activate your virtual environment, you just need to run the command `your_virtual_env_name\Scripts\activate.bat` on windows and for mac, 
run `source your_virtual_env_name/bin/activate`.

Your virtual environment should now be active. The name will be displayed in brackets just before the path in the terminal.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2yr2lp4gtpnn0g8yfkr.jpg)

Now that we have a virtual environment up and running we can start building our flask app.

###Setting up the flask app.
Within the activated virtual environment, install flask using the terminal  by typing `pip3 install Flask`.
Create a new python file in the project folder, you can name it **main.py** or **app.py**.

Open up the newly created python file and import flask.

`from flask import Flask`

Then make an instance of the Flask class and call it app. Calling it app is just by convention.

`app = Flask(__name__)`

The first first argument `__name__` is the name of the application module. This is  needed so that Flask knows where to look for resources such as **templates** and **static files**.

Then we use the `route()` decorator to tell Flask what URL should trigger our function. 

`@app.route('/')`

We then add a function to return what we want to display in the browser.
```
def Hello():
    return "Hello World!"
```
Finally the app is started by running, 
```
if __name__ == "__main__":
    app.run(debug=True)
```
You can use `app.run()` without the arguments and the app will still run, but usually its good to include the `debug=True `argument in order to be able to see any error logs in case there are any errors.

That's if for our set up of the basic app.

This is the full code for our app.
```
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

if __name__ == "__main__":
    app.run(debug=True)
```
Now the next step is to run the app in the browser.

To do this, go back to the terminal, ensure that you are still in the virtual environment and run `python main.py`. **main.py** is the name of the python file that is the entry point for your app. So you could give it any name that makes sense to your project.

On running the command `python main.py` a URL will show up in the terminal and you can open it up in the browser to see the web application.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vp07ey4cbaox39valda7.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzu1t1l37u0m33t5cspf.png)

Viola, that's how simple it is to get started with flask.

This is a very basic introduction just to whet your appetite for flask . Be sure to check out more resources on how you can use flask for a variety of cool stuff.

The official flask documentation [here](https://flask.palletsprojects.com/en/2.0.x/quickstart/) is a good start to understand flask.