Steps to start Flask project with python 3

I will try to keep this post as short as possible because if you are a developer reading this article, it should not be too hard for you to figure out what I am doing.

Preliminary steps: Install the below tools.


Create a virtual environment for your project.

Virtual environment will ensure your project dependencies does not pollute your system level libraries.

python3 -m venv venv

Active the virtualenv

source venv/bin/activate

You should be inside your virtual environment now. Look for the name (venv) of your environment in your path.


Install flask

pip install flask

Look at your libraries in pip and move them into your requirements.txt file.

pip freeze
pip freeze > requirements.txt

Create a sample Flask Rest API

Create file and add your first API.

from flask import Flask
from flask import request, jsonify
from kanpai import Kanpai

app = Flask(__name__)

register_schema = Kanpai.Object({
    "email": Kanpai.Email().required("email id is required"),
    "password": Kanpai.String().required("Please enter your password")

@app.route('/signup', methods=['POST'])
def signup():
    validation_result = register_schema.validate(request.json)

    if validation_result.get('success', False) is False:
        return jsonify({
            "status": "Error",
            "errors": validation_result.get("error")

    # implement the code for created_user
    return jsonify({
        "status": "Success",
        "payload": created_user

I am using Kanpai for validating my json requests. You should definitely take a look at this beautiful library.

Start your flask app using the below command.

(venv) $ export
(venv) $ flask run

Test your API

Call your API using the curl command

curl -X POST \
  http://localhost:5000/signup \
  -H 'content-type: application/json' \
  -d '{
	"password" : "p"