We are going to create a simple Python web application using Flask framework and MongoDB. It is very easy to work with Flask as well as MongoDB.

Download Python from here.You can select the customized installation choice for adding Python environment variables. If you choose default installation, please set these variables manually.

Please click Add Python 3.7 to PATH option. It will add one entry in the environment variable.

If needed you can modify the installation path. Please check the Python environment variables.

After successful installation, if you check the system environment variable you can see the below two entries are added in the system.

We installed the Python 3.7 version, so it added as Python 37-32.

\Scripts folder is used for handling additional packages needed for Python.

Now you can check the Python and PIP version in the command prompt. PIP is the abbreviation for Python package index which is used for installing, upgrading or removing the additional Python libraries from our system.

> python --version and pip -- version

By default, the pip version may not be 18. This is the latest version as of today. If your pip version is older than that, please upgrade it.

>python -m pip install -U pip

Now we are going to install Flask. Flask is a web framework for Python. Flask provides you with tools, libraries and technologies that allow you to build a web application in Python.

Its dependencies are,

Werkzeug a WSGI utility library

jinja2 which is its template engine

WSGI is basically a protocol defined so that the Python application can communicate with a web-server and thus be used as web-application outside of CGI.

Jinja2 is used for creating views in flask application. It holds all the static files like html, CSS and JavaScript files.

First install Flask using pip command in command prompt.

>pip install FlaskIt will install the latest version of flask library from its global repository and in Windows machines it saves the below file location

While installing the MongoDB in windows system, you can choose the compass community edition also.

This is a free edition and will help to handle our MongoDB databases, collections (tables in MongoDB) and documents (records) graphically

By default, MongoDB is running in localhost 27017 port.

We can see there are 3 databases, (admin, config and local) created automatically in the installation time.

Now we are going to install PyMongolibrary in python. PyMongo is a simple but powerful Python distribution containing tools for working with MongoDB and is the recommended way to work with MongoDB from Python. It's a kind of ODM (object document mapper). There is mongoengine and so many libraries also available as ODM for Python.

>pip install pymongo

We need to install another Python library, bson, too. It is used for getting objectId property of mongodb document.

>pip install bson

Now we are all set and ready to start our python program.Create a folder named FlaskwithMongo and add two sub folders, static and templates, inside it. There is a convention to name static and templates used in jinja2 framework. Please open the folder in any of the code editors. I am using Visual Studio code as an IDE. Create a new file named app.py. This is the only Python file we are using in this application. First, we are going to import the required libraries into our application.

from flask import Flask, render_template,request,redirect,url_for

from bson import ObjectId

from pymongo import MongoClient

import os

Now we are going to declare the app variable in our program.

app = Flask(__name__)

This app variable is used in the entire application.Now we are going to declare twovariable titles and headings which are later used in jinja2 template.

title = "TODO sample application with Flask and MongoDB"

heading = "TODO Reminder with Flask and MongoDB"

We must declare the connection string for MongoDB and select a database. Also, we are going to select our collection name to a variable.

client = MongoClient("mongodb://127.0.0.1:27017") #host uri

db = client.mymongodb #Select the database

todos = db.todo #Select the collection name

As I mentioned earlier mongodb is running in the port 27017 by default. Please note that we have selected mymongodb as database and todo as collection name. When we create our first transaction, pymongo will generate the database and collection automatically. Unlike SQL server or any other RDBMS mongodb doesn’t require predefined schemas.

In flask, it is easy to implement routing unlike any other programming language.

For that, we are using render_template, request, redirect and url_for. All these methods help us to establish redirection and show html templates in the browser

def redirect_url():

return request.args.get('next') or \

request.referrer or \

url_for('index')

In the above code we defined a method named redirect_url and it is used to redirect page to index page.

In the above code we defined a method named tasks and it is used for two routes. One for default “/” route and another for “/uncompleted” route. In this code we defined a variable todos_l and it gets the documents from mongodb filter by condition done equal to no. We defined one more variable named a2 and it is used for controlling active records. Both todos_land a2 variables passed with other two variables, title and heading, and passed to the jinja2 template index.html which we must create in the template folder.

Now we are going to finish all the remaining route definitions for adding and deleting the documents to mongodb