The Rise of Computational Sensemaking

No selfrespecting artificial intelligence researcher would claim A.I. is going to take over the role of humans in this world any time soon. There are still many fundamental A.I. challenges that stop…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Connect Flask App to MongoDB Atlas Using Pymongo

When I needed to connect a flask app to MongoDB Atlas for my final year capstone project, I tried did some research online. Unfortunately, I did not find any article that explains this. Therefore, I decided to write one after I figured out how to do it.

Step 1 — Create an Account on MongoDB Atlas Website

Step 2 — Create a Starter Cluster

After you login to your MongoDB Atlas account, you can create a starter cluster by selecting your desired cloud service provider, cluster tier and your cluster name. It takes 1–3 mins for the cluster to be ready. When the cluster is ready, it looks like something below:

Then, you can create a data base and a collection by clicking collections →add my own data. A popup window will show up and you can fill in the data base name and collection name. See the figure below.

Step 3 — Create a Flask App

Now we are ready to move on to create a flask app. You can enter the following command to set up the project folder & the project environment.

Create a project folder and direct to the project folder:

Set up virtual environment for your project:

Mac OS/Linux

Windows

Create files:

This will give you the following structure:

Install Flask and add the installed libraries to our requirements.txt file:

Open up app.py in your favourite editor and add the following code:

Run the Flask app by typing:

Now you app is running on port 8000! If you go to localhost:8000, you should see the printed message:flask mongodb atlas!.

Step 4 — Connect the Flask App to MongoDB Atlas

Pymongo is the go to library for us to connect python with MongoDB data base. You will also need to dnsPython Library to query data from MongoDB Atlas. You can install the libraries by following command:

Pymongo uses connection string to connect the app to the MongoDB Atlas. To find the connection string, go back to your cluster main page:

Then click Clusters →connect, a popup window will show up:

From this window, you can add your IP address to the configuration so that your computer can access the data base. Or you can add IP address 0.0.0.0/0 to the configuration so that any IP address can access the data base.

You will also need to create a MongoDB user by typing your user name and password.

After you successfully complete the above steps, click choose a connection method on the bottom right corner. Choose connect to your application on the new window, then you will see a window like this:

Choose Python as Driver and choose the right version of the Python you are using and you will see the connection string.

Create another file called db.py and add the following code and replace <password> with your db user password in the connection string.

Also, add following code to app.py:

Now if your go to localhost:8000/test, you should see a printed message: Connected to the data base!

If you go to your data base, you should see the record you just inserted.

Congrats! You have successfully connected your Flask app to MongoDB Atlas.

Add a comment

Related posts:

The Leaders without Titles

Robin Sharma said become a leader with no title. I red that book in my 19 where a librian changes the world by being more than a bookkeeper. Yes ! I have promised to launch a book but as many are…

Cryptos and NFTs! The whole market is driven by FOMO and Hype.

Before I start showing you some obvious red flags. Let me make a statement about the underlying technology. Blockchain is a highly secure peer-to-peer network architecture, and it has potential to…

The Helping Trap

After 104 weeks of consistently producing content, I was starting to gain traction and it felt great. I was starting to feel good about myself. It felt as though I was making a difference in the…