Install Flask and create your first web application

Sahil Rajput - Nov 5 '18 - - Dev Community

There are tons of Python web frameworks and Flask is one of them but it is not a full stack web framework.
It is “a microframework for Python based on Werkzeug, Jinja 2 and good intentions.” Includes a built-in development server, unit tesing support, and is fully Unicode-enabled with RESTful request dispatching and WSGI compliance.

Installation

To install flask you can go here 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 module to create virtual environments.
If you are using Python 2, the venv module is not available. Instead, install virtualenv.

On Linux, virtualenv is provided by your package manager:

//Debian, Ubuntu
$ sudo apt-get install python-virtualenv
//CentOS, Fedora
$ sudo yum install python-virtualenv
//Arch
$ sudo pacman -S python-virtualenv
Enter fullscreen mode Exit fullscreen mode

If you are on Mac OS X or Windows, download get-pip.py, then:

$ sudo python2 Downloads/get-pip.py
$ sudo python2 -m pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

On Windows, as an administrator:

\Python27\python.exe Downloads\get-pip.py
\Python27\python.exe -m pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Step 2: Create an environment

Create a project folder and a venv folder within:

mkdir myproject
cd myproject
python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

pic1

On Windows:

py -3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

If you needed to install virtualenv because you are on an older version of Python, use the following command instead:

virtualenv venv
Enter fullscreen mode Exit fullscreen mode

On Windows:

\Python27\Scripts\virtualenv.exe venv
Enter fullscreen mode Exit fullscreen mode

Activate the environment

Before you work on your project, activate the corresponding environment:

. venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

pic2

On Windows:

venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

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:

$ pip install Flask
Enter fullscreen mode Exit fullscreen mode

screenshot 2018-11-05 at 9 22 44 pm

Flask is now installed: Check out the Quickstart or go to the Documentation.

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
    

    screenshot 2018-11-05 at 11 07 58 pm

. . . . . . . . . . . . . . . . .
Terabox Video Player