10 min read Author: klaas

Working with PythonOnWheels & MongoDB Atlas

You can now directly use a MongoDB Atlas Cloud Database in PythonOnWheels. This is pretty handy if you want to start a small project using mongoDB and don't want to install and manage mongoDB locally for that. The vision of PythtonOnWheels is to make you focus on your project and not the boilerplate, and I think this enhancement fits pretty well into that idea. 

MongoDB Atlas has a free plan with 512 MB Storage which is more than enough for a starting a small project. It's not a cloud lock-in as well since you can always go local whenever you want later on.

So it's now literally a matter of minutes to get from zero to generate an app, make a model and store your data in Atlas.

Btw: I am in no case related to MongoDB, I'm just using it pretty often and sometimes wished I didnt have to set it up locally and can go just like SQlite. So this is what using Atlas makes possible.

So let's see how it works

The workflow is pretty simple and will last from zero to data in ~5 to 10 Minutes. 

  • Generate a PythonOnWheels app
  • Create a mongoDB Atlas account
  • Setup the MongoDB Atlas DB connection
  • Generate a model
  • Insert data

Create a mongoDB Atlas account

First you need to create a free mongoDB Atlas account (if you don't already have one). Just got the Atlas page and follow the  instructions for a free account. Simple 4 Step Wizard process. 

Short remark here: If you want to avoid any IP based connection problems for this test, say you're sitting behind NAT/Firewals or so you can allow access from anywhere. If you're sure you know your IP you can limit access to only this IP as easy. Both happens in the whitelist you IP address step. 

Generate a PythonOnWheels app

Install PythonOnWheels (if you don't have already)

pip install -U pythononwheels

Generate a new PythonOnWheels app

generate_app -n atlastest -p <path_of_your_choice>

Create a virtualenv and install the PythonOnWheels dependencies 

virtualenv <path_of_your_app>

Activate your virtualenv (cd in it and source bin/activate [Unix] or Scripts\activate) [windows]

pip install -r requirements.txt

Setup the MongoDB Atlas DB connection

Get the connection string for your Atlas DB

Click on connect.

Then choose: Connect your Application.

Choose the short SRV connection string (pymongo 3.6+ Version) and copy the connection string.

Open your PythonOnWheels app_dir/config.py file and got to the database["mongodb"] section. 

  • set "atlas" to True
  • and paste the connection string to the "atlas_cstr" parameter and set the correct user:password.

Now we are ready to generate a model

Let's name it todo .. or give it any other name you prefer.

python generate_model.py -n todo -t mongodb

This will generate a standard NoSQL model in you app/models/mongodb directory, named todo.py

The default schema looks like this :

class Todo(MongoModel):
# Use the cerberus schema style
# which offer you immediate validation with cerberus
# http://docs.python-cerberus.org/en/stable/validation-rules.html
# types: http://docs.python-cerberus.org/en/stable/validation-rules.html#type
schema = {
'title' : { 'type' : 'string', 'maxlength' : 35 },
'text' : { 'type' : 'string' },
'tags' : { 'type' : 'list', "default" : [] },
"votes" : { "type" : "integer", "default" : 0 }

For now we will leave everything untouched. If you want to know more about PythonOnWheels models and schemas you can read this short introduction to PythonOnWheels models.

Now lets's Test the connection and insert some data

Open a command line and import your model.

>>> from atlastest.models.mongodb.todo import Todo
... setting it up for mongoDB Atlas: @cluster0-bskyr.mongodb.net/test?retryWrites=true

Create an instance and set some attributes

>>> t=Todo()
trying to find possible observer in atlastest.models.mongodb.todo_observer.TodoObserver
>>> t
{ '_uuid': 'e5b407a2-4611-4173-b297-9d37d72c4f74',
'created_at': datetime.datetime(2019, 1, 9, 13, 34, 6, 497683),
'id': 'e5b407a2-4611-4173-b297-9d37d72c4f74',
'last_updated': datetime.datetime(2019, 1, 9, 13, 34, 6, 497683),
'tags': [],
'text': '',
'title': '',
'votes': 0}

Set the title to "atlastest"

>>> t.title="atlastest"

Insert to the Atlas DB

>>> t.upsert()

Check back in the MongoDB Atlas console:

As you can see the data has been inserted to Atlas.

So that's it. 

You can read more in the short tutorial or the documentation Or add an API by generating a handler


Especially feel very free to tweet  questions, bugs .. anything to @pythononwheels.