Install Flask and create your first web application
Tags: flask, python, webapplication
Published: 2018-11-05 18:15:24

---
title: Install Flask and create your first web application
published: true
description: 
tags: flask, python, webapplication
---
There are tons of [Python web frameworks](https://wiki.python.org/moin/WebFrameworks) and [Flask](http://flask.pocoo.org/) is one of them but it is not a full stack web framework.
It is “a microframework for Python based on [Werkzeug](http://werkzeug.pocoo.org/), [Jinja 2](http://jinja.pocoo.org/docs/2.10/) and good intentions.” Includes a built-in development server, unit tesing support, and is fully Unicode-enabled with [RESTful](https://en.wikipedia.org/wiki/Representational_state_transfer) request dispatching and [WSGI compliance](https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface).

## Installation
To install flask you can go [here](http://flask.pocoo.org/docs/1.0/installation/) or just follow below steps:
### Step1: Install virtual environment
If you are using Python3 than you don't have to install virtual environment because it already come with [venv](https://docs.python.org/3/library/venv.html#module-venv) module to create virtual environments.
If you are using Python 2, the venv module is not available. Instead, `install virtualenv`.<br>
**On Linux, virtualenv is provided by your package manager:**
```sh
//Debian, Ubuntu
$ sudo apt-get install python-virtualenv
//CentOS, Fedora
$ sudo yum install python-virtualenv
//Arch
$ sudo pacman -S python-virtualenv
```
If you are on Mac OS X or Windows, download [get-pip.py](https://bootstrap.pypa.io/get-pip.py), then:
```sh
$ sudo python2 Downloads/get-pip.py
$ sudo python2 -m pip install virtualenv
```
On Windows, as an administrator:
```sh
\Python27\python.exe Downloads\get-pip.py
\Python27\python.exe -m pip install virtualenv
```
### Step 2: Create an environment
Create a project folder and a `venv` folder within:
```sh
mkdir myproject
cd myproject
python3 -m venv venv
```

<img width="561" alt="pic1" src="https://user-images.githubusercontent.com/20112458/48008954-b673d080-e140-11e8-87b3-2e668098469e.png">

On Windows:
```sh
py -3 -m venv venv
```
If you needed to install virtualenv because you are on an older version of Python, use the following command instead:
```sh
virtualenv venv
```
On Windows:
```sh
\Python27\Scripts\virtualenv.exe venv
```
**Activate the environment**

Before you work on your project, activate the corresponding environment:
```sh
. venv/bin/activate
```
<img width="563" alt="pic2" src="https://user-images.githubusercontent.com/20112458/48008953-b5db3a00-e140-11e8-93d7-d3ca238d75e2.png">

On Windows:
```sh
venv\Scripts\activate
```
Your shell prompt will change to show the name of the activated environment.

### Step 3: Install Flask
Within the activated environment, use the following command to install Flask:
```sh
$ pip install Flask
``` 
<img width="1179" alt="screenshot 2018-11-05 at 9 22 44 pm" src="https://user-images.githubusercontent.com/20112458/48009094-f76be500-e140-11e8-882e-00917743fe68.png">

**Flask is now installed:** Check out the [Quickstart](http://flask.pocoo.org/docs/1.0/quickstart/) or go to the [Documentation](http://flask.pocoo.org/docs/1.0/).


## Create a applcation

So, let's build the most simplest `hello world` application.
**Follow these steps:**
1. As, you are already present in the `myproject` folder. Create a file `hello.py' and write the below code.
    - Import the Flask class. An instance of this class will be our WSGI application.

            from flask import Flask 
    - Next we create an instance of this class. The first argument is the name of the application’s module or package. If you are using a single module (as in this example), you should use __ name __ because depending on if it’s started as application or imported as module the name will be different ('__main__' versus the actual import name). This is needed so that Flask knows where to look for templates, static files, and so on. 
    
            app = Flask(__ name __)
    - We then use the route() decorator to tell Flask what URL should trigger our function.The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.
        
            @app.route('/')
            def hello_world():
                return 'Hello, World!'

    `Make sure to not call your application flask.py because this would conflict with Flask itself.`

    To run the application you can either use the flask command or python’s `-m` switch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting the `FLASK_APP` environment variable:
    
        $ export FLASK_APP=hello.py
        $ flask run
        //Or you can use
        $ export FLASK_APP=hello.py
        $ python -m flask run

     <img width="566" alt="screenshot 2018-11-05 at 11 07 58 pm" src="https://user-images.githubusercontent.com/20112458/48015704-c515b400-e14f-11e8-8690-9949dd356239.png">
     - Go to [http://127.0.0.1:5000/](http://127.0.0.1:5000/) to see your project running.
     <img width="1439" alt="screenshot 2018-11-05 at 11 10 19 pm" src="https://user-images.githubusercontent.com/20112458/48015810-fdb58d80-e14f-11e8-879f-dfc793828cd8.png">