librelist archives

« back to archive

Flask Web Application: Need Suggestion

Flask Web Application: Need Suggestion

From:
khushboo Vashi
Date:
2015-07-08 @ 04:16
Hi,

I am using Flask for my web Application.

I have multiple database / server connection and I want to store them, So
whenever request comes
I can use stored connection to serve the request.

Here I can store them into Session or into App Context.

I want to know which is the best approach from above or Is there anything
else better than above.


Thanks,
Khushboo

Re: [flask] Flask Web Application: Need Suggestion

From:
Matt Fields
Date:
2015-07-08 @ 08:49
Khushboo,
You might try this page for starters:
http://pythonhosted.org/Flask-SQLAlchemy/binds.html  (Assuming you're using
flask-sqlalchemy for your database connections.)


You would set sqlalchemy binds for your different databases, by name, then
refer to them by name "bind=..."

Excerpts from the page:
Example Configuration

The following configuration declares three database connections. The
special default one as well as two others named users (for the users) and
appmeta (which connects to a sqlite database for read only access to some
data the application provides internally):

SQLALCHEMY_DATABASE_URI = 'postgres://localhost/main'SQLALCHEMY_BINDS = {
    'users':        'mysqldb://localhost/users',
    'appmeta':      'sqlite:////path/to/appmeta.db'}


Creating and Dropping Tables

db.create_all(bind=['users'])>>> db.create_all(bind='appmeta')>>>
db.drop_all(bind=None)

 Referring to Binds

If you declare a model you can specify the bind to use with the __bind_key__

<http://pythonhosted.org/Flask-SQLAlchemy/api.html#flask.ext.sqlalchemy.Model.__bind_key__>
attribute:

class User(db.Model):
    __bind_key__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)

- Matt


On Wed, Jul 8, 2015 at 12:16 PM, khushboo Vashi <khushboo.vashi@gmail.com>
wrote:

> Hi,
>
> I am using Flask for my web Application.
>
> I have multiple database / server connection and I want to store them, So
> whenever request comes
> I can use stored connection to serve the request.
>
> Here I can store them into Session or into App Context.
>
> I want to know which is the best approach from above or Is there anything
> else better than above.
>
>
> Thanks,
> Khushboo
>