Setup Flask Project

Prerequisite
What Is Flask ?
Flask is a lightweight Web framework written in Python.It is developed by Armin Ronacher.Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine

Setup Development Environment

First Install virtualenv
virtualenv is a virtual python environment builder.virtualenv  is used to manage python packages for different projects. Using virtualenv allows you to avoid installing python packages globally which could break system tools or other projects.
Install the virtualenv using the following command.This command needs administrator privileges

# pip install virtualenv

After installing the virtualenv
.
Make Folder

create new folder using the following command

 # mkdir flaskdemo

change to the current created directory/folder

# cd flaskdemo

Create Virtual Environment In Folder

Now we will create a virtualenv
The commands below will create a new directory named virtual_env inside our project folder that contains a complete, isolated python environment.

# virtualenv virtual_env

Activate Virtual Environment

Now we have to activate the environment

To activate it on windows use following command

# virtual_env\scripts\activate

For linux/os x use following

virtual_env/bin/activate 

After running the command in your terminal/command prompt  you should see (virtual_env) in your command prompt.

Install Flask 
Now we install flask using following command

# pip install flask

After the flask is installed.Add a new file in the project folder and name it demo.py
In that add the below code.

Importing flask module in the project is mandatory. An object of Flask class is our WSGI application.
Flask constructor takes the name of current module (__name__) as argument.
The route() function of the Flask class is a decorator, which tells the application which URL should call the associated function..

The __name__ == '__main__' Python idiom is used here to ensure that the development web server is started only when the script is executed directly.
The run() method of Flask class runs the application on the local development server.
Once the server starts up, it goes into a loop that waits for requests and services them.

Now run the script by using python shell
python demo.py

In console/terminal you will see 
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 


Comments

  1. http://chennaitraining.in/dotnet-training-in-chennai/
    http://chennaitraining.in/etl-testing-training-in-chennai/
    http://chennaitraining.in/salesforce-admin-training-in-chennai/
    http://chennaitraining.in/salesforce-developer-training-in-chennai/
    http://chennaitraining.in/sap-hana-training-in-chennai/
    http://chennaitraining.in/sap-mm-training-in-chennai/
    http://chennaitraining.in/sap-sd-training-in-chennai/

    ReplyDelete

Post a Comment

Popular posts from this blog

Upload And Download File In Node

How To Use Sequelize-Cli In Node