akai
akai

Reputation: 2052

Why need to use Flask extension, not just the bare library?

Can somebody explain why you should Use a flask extension instead of a bare library?

For example, if you want to use mongoDB (or whatever) from Flask, it seems you need to do this:

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

and use this instance throughout the application.

But, apparently, using a "normal" pymongo is simpler:

from pymongo import MongoClient
mongo = MongoClient()

I'd like to know what are so special about the extensions.

Upvotes: 0

Views: 238

Answers (2)

andreffs
andreffs

Reputation: 85

Using the pymongo library instead of the flask-mongoengine extension gives you full access to all the db connection methods and also other utilities that might be somehow useful.

Instead, by using the flask-mongoengine extension, you will have a simplified "wrapper" around the pymongo library, which is safer and user friendlier.

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123710

I'd like to know what are so special about the extensions.

The PyMongo extension allows you to configure the Mongo connection using the same configuration mechanism used for the rest of Flask, see the Configuration section in the PyMongo documentation what is supported.

The library also offers a few convenience functions that you may find helpful when writing a Flask app backed by Mongo.

There is no requirement in using the extension; if you feel that using MongoClient directly is easier, then do so.

This applies to most extensions; they'll offer some level of integration with the Flask ecosystem. You'll need to decide for each how much you need to make use of that integration vs. how much you'd have to re-invent the wheel. Like any library really.

Upvotes: 3

Related Questions