librelist archives

« back to archive

how to connect to multiple databases and show their data on a single page?

how to connect to multiple databases and show their data on a single page?

From:
Ign Aarushi
Date:
2012-06-15 @ 05:29
*hi,*
*i need to access multiple databases and show each of their data on a
single page*
*it includes displaying images, and text all combined in one. so how to do
it?*
*i am new to flask so may be it's a very simple question but i still need
help*
*so please help and thank you :)*

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
pronoyc@gmail.com
Date:
2012-06-15 @ 05:35
What database are you using?

Either way the correct approach is to fetch the data in the view function
and pass it to the template in the context.

you can follow this complete tutorial
http://flask.pocoo.org/docs/tutorial/introduction/

On Fri, Jun 15, 2012 at 10:59 AM, Ign Aarushi <ign.aarushi@gmail.com> wrote:

> *hi,*
> *i need to access multiple databases and show each of their data on a
> single page*
> *it includes displaying images, and text all combined in one. so how to
> do it?*
> *i am new to flask so may be it's a very simple question but i still need
> help*
> *so please help and thank you :)*
>



-- 
Regards,

Pronoy Chopra
http://blog.pronoy.in <http://www.pronoy.in/about>/
http://www.twitter.com/pronoyc

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Ign Aarushi
Date:
2012-06-15 @ 09:48
i am using mysql and i have stored the path of the images in the database
the main problem i am facing is to access multiple databases and show it on
a single page as i have to map it to one url only and also one template

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Igor Davydenko
Date:
2012-06-15 @ 10:22
Do you use some ORM? Or just MySQL-python?

If MySQL-python - you able to connect to any number of databases with
``MySQLdb.connect`` function and then fetch data from queries using
connection's cursor.

Pseudo code::

    import MySQLdb as db

    from flask import Flask, render_template

    app = Flask(__name__)

    @app.route('/')
    def index():
        db1 = db.connect('localhost', 'user', 'password', 'db1')
        db2 = db.connect('localhost', 'user', 'password', 'db2')

        cursor1, cursor2 = db1.cursor(), db2.cursor()
        sql = 'SELECT * FROM images;'

        cursor1.execute(sql)
        cursor2.execute(sql)

        data = list(cursor1.fetchall())
        data.extend(list(cursor2.fetchall()))

        return render_template('index.html', data=data)

On 15 June 2012 12:48, Ign Aarushi <ign.aarushi@gmail.com> wrote:
> i am using mysql and i have stored the path of the images in the database
> the main problem i am facing is to access multiple databases and show it on
> a single page as i have to map it to one url only and also one template



-- 
Sincerely,
Igor Davydenko

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Ign Aarushi
Date:
2012-06-15 @ 18:00
thank you. I was doing a silly mistake of connecting the databases and all
outside the function.
thanks for the help :)

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Ign Aarushi
Date:
2012-06-15 @ 18:51
i am facing a little problem. in the function when i return the whole list
to it. it only displays the 1st entry.
when i run it in the command prompt it displays all the entries but when i
use it in the function, it only show the very first entry in the list and
leaves the rest. so only the first picture gets showed and everything is
left out. My code:


import MySQLdb as db
from flask import Flask, render_template,send_file
app = Flask(__name__)
@app.route('/')
def index():
    db1 = db.connect('localhost', 'user', 'password', 'db1')
    db2 = db.connect('localhost', 'user', 'password', 'db2')

    cursor1, cursor2 = db1.cursor(), db2.cursor()
    sql1 = 'SELECT Path FROM images;'
    sql2 = 'SELECT Path FROM graphs;'
    cursor1.execute(sql1)
    cursor2.execute(sql2)
    data = list(cursor1.fetchall())
    data.extend(list(cursor2.fetchall()))
    for row in data:
        t="C:/Python27/"+ row[0]
        return send_file(t, mimetype='image/jpg')
if __name__ == "__main__":
    app.run(debug=True)

thanks for your time and help :)

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Simon Sapin
Date:
2012-06-15 @ 19:27
Le 15/06/2012 20:51, Ign Aarushi a écrit :
>      for row in data:
>          t="C:/Python27/"+ row[0]
>          return send_file(t, mimetype='image/jpg')

You can only return once from a function. After you have returned, the 
function stops executing and any loop in there is dropped.

-- 
Simon Sapin

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Ign Aarushi
Date:
2012-06-15 @ 19:52
there must be a way to display all the images in flask in one go. do any
one know of it?

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Simon Sapin
Date:
2012-06-15 @ 19:56
Le 15/06/2012 21:52, Ign Aarushi a écrit :
> there must be a way to display all the images in flask in one go. do any
> one know of it?

If you want to display an HTML page with multiple images, make a Jinja 
templates that generates <img> tags (and whatever you want around them).

Now each <img> needs an URL. Either:

1. Make a view that uses send_file() for a single image (so you would 
have one HTTP request per image)
2. Encode the images to base64 and use data: URIs 
http://en.wikipedia.org/wiki/Data_URI_scheme

2 might be more convenient if you get all the images at the same time, 
but 1 allows more caching. Also, browsers tend to have slow-downs with 
multi-megabytes data: URIs.

-- 
Simon Sapin

Re: [flask] how to connect to multiple databases and show their data on a single page?

From:
Ign Aarushi
Date:
2012-06-17 @ 15:17
thanks for the help.