Wednesday, 16 January 2019

Python Flask


What is Flask?
Flask is a popular Python web framework, a third-party Python library used for developing web applications.

What is the purpose of Web Framework in Python?
When a user develops a Python application, it is called as "command line applications" for which the user has to run these scripts in a shell or command prompt and pass input as arguments. This kind of applications is to build desktop applications — a program that allows users to interact using a mouse and keyboard, which contains menus and other interactive elements. In order build web applications — your users will interact with your program via their web browser. In order to build a web application, a web framework is necessary. Flask is one of the simple and powerful web framework for Python.

How to install Flask?

Method 1:
First you need a virtual environment to install flask.  Once you install the virtual environment, use the following command
pip install flask

Method 2:
First you need to install Anaconda Navigator. Anaconda Navigator is a desktop graphical user interface that allows you to launch applications and easily manage conda packages, environments and channels without the need to use command line commands. It will also install latest version of python.
Once installation is completed, open Anaconda Prompt and use the following command
pip install flask

Basic Example
The following is a simple python program with flask. The below code shows "Hello, World!" on “localhost:5000” in a web browser when run with the python app.py command and the Flask library installed.
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
  return "Hello, World!"
  
if __name__ == "__main__":
  app.run()
Why Flask?
1. It’s lightweight, simple to learn and extensively scalable.
2. A growing community is providing neat solutions to several problems being faced by developers.
3. It is super easy to deploy Flask in production.

Conclusion
To sum up, Flask is one of the most feature-rich micro frameworks available. Flask comes with all the benefits of fast templates, strong WSGI featuresthorough unit testability at the web application and library levelextensive documentation which helps us in building projects where good features and a vast number of extensions are needed.

4 comments: