Flask Tutorials- Python Flask MongoDB Setup in Simple Steps
What is a Python flask?
Flask is a Python WSGI web application framework. Flask is a micro web application framework written in Python. It is a very lightweight web application framework. The Flask micro web application framework is based on the Pocoo projects, Werkzeug, and Jinja2.
In this Tutorials, we’ll learn how to set up Python with flask MongoDB the very first thing you need to do is check if you already have Python installed on your laptop therefore go to the terminal and just type py then you will get the python details with the version. If it is not installed on your computer. Then follow the below installation steps.
Python 3.8.7rc1 (tags/v3.8.7rc1:e320109, Dec 7 2020, 16:42:32) [MSC v.1927 64 b
it (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
How to install Python Flask MongoDB?
1. Installation process
Go to the browser and search Python, choose Python.org, and download the latest version it’s 3.8 to click on download and once it’s done downloading just double click on the exe file and the installation will pop up now make sure that you have this checked at “Add Python 3.8 to path“.
because it will add one entry in the environment variable and this will also allow us to just go ahead and type py from the terminal and we will need that so when you’re installing it make sure that this is selected.
Now select Install Now and this will pop up an agreement window select yes and click on the Next button The installation process takes place it will take a few minutes for installation. Once installations have been successful, we can close the installation window.
Now we can check the version in the CMD terminal by simply typing the below command. PIP is the abbreviation for Python package index which is used for installing, upgrading, or removing the extra libraries from your system.
You can upgrade the PIP by using the below command.
python -m pip install -U pip
Now I’m going the install the Flask by using PIPcommand in terminal.
Go to the browser and search “MongoDB” MongoDB official site will open. Go to software community server and select the current version for Windows and this MSI is the package that will install everything that I need on my computer click download it will take a few minutes to download. Once downloading is completed.
Now I’m installed MongoDB before you even go to MongoDB that you go to your C Drive and inside your C Drive create a new folder call it mongodb this is the default place where MongoDB will set up your database obviously you can change it at some point using a command that is a flag that is –db path.
Double Click on the MongoDB exe file the installation pop-up will appear then select next, and check the agreement checkbox.
select next to do a complete installation, and don’t touch anything there everything is done for you by default so just click the next button.
To install MongoDB compass check the “install MongoDB compass” checkbox and click on the Next button the installation will take place. It will take a few minutes for installation.
After successful installation, the MongoDB compass is running automatically. If you didn’t get the compass running automatically you can just go to the window search and search for a compass when you click on the MongoDB compass button you will get to the MongoDB compass window.
Here so let’s check if everything is running fine we should be able to connect to the database and now we have these three default data databases created for us don’t ever touch them those are the default.
After successful Installation, We need to install PyMongolibrary in python and bson. PyMongo is a kind of Object Document Mapper(ODM) and BSON is a Python library used for getting objectid property of MongoDB document.
pip install pymongo
pip install bson
Now Flask and MongoDB installation is completed. In the next tutorial, we will learn how to create a simple project in Flask.