librelist archives

« back to archive

To make a Python Image Gallery:

To make a Python Image Gallery:

From:
angela martin
Date:
2012-06-24 @ 15:01
Hi,

I am using Flask to develop a image gallery site. I am using MySQLdb. I
have stored the images name,size,type and path into the database and I am
using a function to get all the path from the database to fetch the images
from the drive and display them. The path stored in the database is like
/static/img/image.jpg. The function is like this :

import MySQLdb as db

def image():

    db1 = db.connect('localhost', 'user', 'password', 'images')

    cursor1 = db1.cursor()

    sql1 = 'SELECT Path FROM image;'

    cursor1.execute(sql1)

    data = list(cursor1.fetchall())

    for row in data:

        return "C:/Python27/FQI/"+ row[0]

this function returns all the path of the images stored in the database.
like this

C:/Python27/FQI/static/img/tumblr_fdfs.jpg

C:/Python27/FQI/static/img/tumblr_weererfgg.jpg

and so on

now i need to take all these path and pass them to a template and for that
i have to make a list of them or a dict i don't know how to do that and i
am having this problem from like 5-6 days and i am not able to solve it.
Please help

in the end i am rendering the template :

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

if __name__ == "__main__":

    app.run(debug=True)

I hope there is someone here who can address my problem. Since I am not
quite familiar with the language, I hope that, instead of solely giving me
a descriptive solution, the kind one can also give me the pieces of codes
necessary please.

Thank you very much

Re: [flask] To make a Python Image Gallery:

From:
Samrat Man Singh
Date:
2012-06-24 @ 15:21
You can make a list and append the images to the list:

images = []
for row in data:
    images.append(row)

Then, in the template file you have to do something like:
{% for image in images %}
<img src="{{image}}"/>
{%endfor%}

Also just a random tip, if you're planning to put your app in production,
you might want to try using it in Linux.

Anyway, hope that helps.

Samrat Man Singh.

On Sun, Jun 24, 2012 at 8:46 PM, angela martin <martinangela63@gmail.com>wrote:

> Hi,
>
> I am using Flask to develop a image gallery site. I am using MySQLdb. I
> have stored the images name,size,type and path into the database and I am
> using a function to get all the path from the database to fetch the images
> from the drive and display them. The path stored in the database is like
> /static/img/image.jpg. The function is like this :
>
> import MySQLdb as db
>
> def image():
>
>     db1 = db.connect('localhost', 'user', 'password', 'images')
>
>     cursor1 = db1.cursor()
>
>     sql1 = 'SELECT Path FROM image;'
>
>     cursor1.execute(sql1)
>
>     data = list(cursor1.fetchall())
>
>     for row in data:
>
>         return "C:/Python27/FQI/"+ row[0]
>
> this function returns all the path of the images stored in the database.
> like this
>
> C:/Python27/FQI/static/img/tumblr_fdfs.jpg
>
> C:/Python27/FQI/static/img/tumblr_weererfgg.jpg
>
> and so on
>
> now i need to take all these path and pass them to a template and for that
> i have to make a list of them or a dict i don't know how to do that and i
> am having this problem from like 5-6 days and i am not able to solve it.
> Please help
>
> in the end i am rendering the template :
>
> return render_template('index.html', images=images)
>
> if __name__ == "__main__":
>
>     app.run(debug=True)
>
> I hope there is someone here who can address my problem. Since I am not
> quite familiar with the language, I hope that, instead of solely giving me
> a descriptive solution, the kind one can also give me the pieces of codes
> necessary please.
>
> Thank you very much
>

Re: [flask] To make a Python Image Gallery:

From:
pronoyc@gmail.com
Date:
2012-06-24 @ 15:34
>
>         return "C:/Python27/FQI/"+ row[0]
>
 No need for the complete Path just after /static/ is required.

Pass all your images' path to a list or a dictionary. This is what
images=images is for.

>  return render_template('index.html', images=images)
>

img_list = []for row in data:
    img_list.append(row[0])


So,  pass your images' path into a list or a dictionary.

return render_template('index.html', images=img_list)


You've already done 75 percent of the work. Now the static function needs
to be called to serve the images. This is what your template code will look
like (index.html):

{% for i in images %}<img src="{{ url_for('static', filename=i }}"
/>{% endfor %}


-- 
Regards,

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

Re: [flask] To make a Python Image Gallery:

From:
angela martin
Date:
2012-06-24 @ 18:30
Thank you. Thank you so much. I can't believe it worked. Thanks a lot
Samrat Man Singh and thank you  pronoyc.