Installation of Python and Flask

Python – Flask

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace.

Install pip for Python 3 in ubuntu 18.04

$ sudo apt update
$ sudo apt install python3-pip
# Once the installation is complete, verify the installation by checking the pip version:
$ pip3 --version
# The version number may vary, but it will look something like this:
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Install Python3 in ubuntu 18.04

$ sudo apt-get install python3.6
# Once the installation is complete, verify the installation by checking the python3 version:
$ python3 -V

Installing Packages with pip

$ pip3 install redis

Note : Replace pip3 with pip if using Python 2

Installing Packages with Pip using the Requirements Files

requirement.txt is a text file that contains a list of pip packages with their versions which are required to run a specific Python project.

Use the following command to install a list of requirements specified in a file:

requirement.txt

Flask
Flask-SocketIO
pika
redis

Command Line :

$ pip3 install -r requirements.txt

Listing Installed Packages

$ pip3 list

Upgrade a Package With Pip

$ pip3 install --upgrade package_name

Uninstalling Packages With pip

$ pip3 uninstall package_name

Flask

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

Why is Flask a good web framework choice?

Flask is considered more Pythonic than the Django web framework because in common situations the equivalent Flask web application is more explicit. Flask is also easy to get started with as a beginner because there is little boilerplate code for getting a simple app up and running.

For example, here is a valid “Hello, world!” web application with Flask:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

The above code shows “Hello, World!” on localhost port 5000 in a web browser(http:\localhost:5000) when run with the python app.py command and the Flask library installed.

Handle CORS policy by Flask-CORS in python

Installation

$ pip install -U flask-cors

In the simplest case, initialize the Flask-Cors extension with default arguments in order to allow CORS for all domains on all routes.

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route("/")
def helloWorld():
  return "Hello, cross-origin-world!"

This extension also exposes a simple decorator to decorate flask routes with. Simply add @cross_origin() below a call to Flask’s @app.route(..) to allow CORS on a given route.

@app.route("/")
@cross_origin()
def helloWorld():
  return "Hello, cross-origin-world!"

Leave a Reply