Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PyMongo with Flask Blueprints?

What is the correct way of picking up mongo object inside Blueprints?

Here is how I have my parent login.py:

app.config.from_object('config')
from flask.ext.pymongo import PyMongo
from child import child
from child2 import child2


app = Flask(__name__)
app.register_blueprint(child2.child2)
app.register_blueprint(child.child)

in my child.py

from app import app
from flask.ext.pymongo import PyMongo

mongo = PyMongo(app)
child = Blueprint('child', __name__)

child2.py is the same structure as child:

from app import app
from flask.ext.pymongo import PyMongo
    
mongo = PyMongo(app)
child2 = Blueprint('child2', __name__)

This is the error message I get:

raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"

I've tried the following in the blueprint

mongo = app.data.driver

but that raises error. Here is full traceback:

    Traceback (most recent call last):
  File "login.py", line 12, in <module>
    from child import child
  File "/home/xxx/xxx/child/child.py", line 13, in <module>
    mongo = PyMongo(app) #blueprint
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 121, in init_app
    raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"
(xxx)xxx@linux:~/xxx$ python login.py 
Traceback (most recent call last):
  File "login.py", line 12, in <module>
    from courses import courses
  File "/home/xxx/xxx/child/child.py", line 13, in <module>
    mongo = PyMongo(app) #blueprint
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 121, in init_app
    raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"

Once my app has created the connection, how should I pick it up in my blueprints?

So the question is how can one structure the connection strings to the db in each of the blueprints. Here is the file structure:

login.py
config.py
/child/child.py
/child2/child2.py

here is the config.py

MONGO_DBNAME = 'xxx'

MONGO_URL = os.environ.get('MONGO_URL')
if not MONGO_URL:
    MONGO_URL = "mongodb://xxx:[email protected]:55822/heroku_xxx";

MONGO_URI = MONGO_URL

I've tried the suggestion below in answers, but this did not work. See my comments below that prospective answer.

like image 566
disruptive Avatar asked Oct 16 '15 09:10

disruptive


People also ask

How do you use PyMongo in flask?

Step 1 — Setting Up PyMongo and Flask In this step, you will install Flask and the PyMongo library. With your virtual environment activated, use pip to install Flask and PyMongo: pip install Flask pymongo.

How do I import blueprints into flask?

To use any Flask Blueprint, you have to import it and then register it in the application using register_blueprint() . When a Flask Blueprint is registered, the application is extended with its contents. While the application is running, go to http://localhost:5000 using your web browser.

How do you install flask ext PyMongo?

You should run C:/Python36/Scripts/pip install Flask-PyMongo to install Flask-PyMongo.

How do I connect MongoDB Atlas with flask app?

You can enter the following command to set up the project folder & the project environment. Now you app is running on port 8000! If you go to localhost:8000, you should see the printed message:flask mongodb atlas!. Pymongo uses connection string to connect the app to the MongoDB Atlas.


2 Answers

One of the issues with the approach of performing an import in the blueprint as was suggest by Emanuel Ey, turns out that it causes a circular import. After much playing, it turns out that the only way (I could find) was to create a separate file called database.py that connects to the database and then I can import this connection to by blueprint as follows:

child.py

from database import mongo
courses = Blueprint('courses', __name__)

and my database.py

from flask.ext.pymongo import PyMongo
mongo = PyMongo() 

and the app, login.py but has to initialize the database

from database import mongo
app = Flask(__name__)
app.config.from_object('config')
mongo.init_app(app) # initialize here!

from child import child 
from child import2 child2

app.register_blueprint(child.child)
app.register_blueprint(child2.child2)
like image 85
disruptive Avatar answered Oct 02 '22 23:10

disruptive


You're are initializing the PyMongo driver twice, once in child.py and a second time on child2.py.

Try initializing the PyMongo connection in the file which sets up your app object and then import it in the children:

login.py:

app.config.from_object('config')
from flask.ext.pymongo import PyMongo
from child import child
from child2 import child2


app = Flask(__name__)
mongo = PyMongo(app)

# Register blueprints
def register_blueprints(app):
    # Prevents circular imports
    app.register_blueprint(child2.child2)
    app.register_blueprint(child.child)

register_blueprints(app)

in child.py

from app import app, mongo

child = Blueprint('child', __name__)

child2.py:

from app import app, mongo

child2 = Blueprint('child2', __name__)
like image 21
Emanuel Ey Avatar answered Oct 02 '22 21:10

Emanuel Ey