akai akai - 5 months ago 10
Python Question

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.

Answer

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.