Python and Flask Getting Started

As per mentioned earlier, these tutorial are purely focused towards api development using python so we will directly start with Flask

Flask is a very simple, un-opinionated framework to develop api’s in python. With very few lines of code you can get started with your api development and there no predefined structure to follow as such. At this point lets first install python.

We will install the latest python version 3.6+ you can find many guides online for it like this

To verify if python is installed write

python --version 

if you see version 3.6+ then good or else write

[email protected]:/mnt/c/work$ python --version
Python 2.7.15rc1
[email protected]:/mnt/c/work$ python3 --version
Python 3.6.7
[email protected]:/mnt/c/work$

This means python3 is installed. Next we need to install pip

This is a package manager for python, and use to install all libraries that we need to use during our development. Install it via this guide

Once this is installed you can verify this by

[email protected]:/mnt/c/work$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Now we can python and pip installed, next lets install flask.

Virtual Environments

Before we go into setting up flask, lets first understand what is virtual env.

When working with python, we always use different packages for different things like Flask, PyMongo, SQL etc etc. When a package gets installed, we install a certain version and it installs different dependencies related to the package. Now if you have two or more different apps running it might install different version of packages. This might cause conflicts when running the apps as the package versions would be different.

To solve this issue, python has the concept of virtual environment, in which it creates a specific directory and it has contained all package version inside it. And when we want to work on that application, we can just activate that environment. You can also read more about it here and here

So now let’s first activate a virtual environment for our new project

python3 -m venv testingenv

This will create a new directory “testingenv” next we need to go into the directory and do

cd testingenv
. bin/activate

This will activate the new virtual env and you will also see the environment name in command line. If you wish to deactivate it, just write

deactivate

in the command line and it will close the virtual env.

Installing Flask

Now, lets activate our virtual env and install flask

pip install Flask

Also, if you notice inside the virtualenv you don’t need to use pip3 anymore. pip works fine

Next create a new file in the directory named “hello.py” and this code



from flask import Flask
app = Flask(__name__)

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

PS: i am using visual code studio editor, you can install the python package on VS code as well

Next to run this minimal flask application

$ export FLASK_APP=hello.py
$ flask run
Serving Flask app "hello.py"
Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
Debug mode: off
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Next if you open the URL on your browser you should see hello world there.

This is the starting step of our api.

Next you can read through flask documentation to understand more about routing and other features. I recommend reading through this http://flask.pocoo.org/docs/1.0/quickstart/

excellence-social-linkdin
excellence-social-facebook
excellence-social-instagram
excellence-social-skype