SMALL VERSION OF THE QUESTION: " I need a lib for python or program (pref. for Linux) that receives a list of urls for images and gives me the hmtl for a table (maybe easy to configure(rows and look))
LONG VERSION OF THE QUESTION: I have and array with a list of url's for images, and I want to make a table (I don't know if this is the best practice, but I think it's the easiest). I don't care if the thumbs are the same file as the big one (just forced to be small). And I don't need to copy the images to anywhere.
I use the following code( d= ["http://.....jpg","http://.....jpg","http://.....jpg","http://.....jpg"]):
def stupidtable(d):
d = list(set(d))
antes=' <tr> <td><a href="'
dp11='"><img src="'
dp12= '" width="100%" /></a></td> <td><a href="'
dp21= '"><img src="'
dp22='" width="100%" /></a></td>'
bb=['<table border="0"> ']
ii=len( d)
i=0
while i<ii-1:
bb.append(antes)
bb.append(d[i])
bb.append(dp11)
bb.append(d[i])
bb.append(dp12)
bb.append(d[i+1])
bb.append(dp21)
bb.append(d[i+1])
bb.append(dp22)
i=i+2
return bb
(I know the code is shady 开发者_如何学Pythonand it skips the last one if it's an odd number... but it's caffeine fueled code and I just needed to get it done... nothing I'm proud of:) I know there must be a better way(and prettier.. 'cus this looks really ugly), and a way that I can specify the number of columns, and other options.
I couldn't find a gallery generator for my case (all I tested copied the files to new directory). Should I learn a templating lang? Is it worth it?
Or Should I use an HTML Generator?
Or should I look into better coding HTML?
What Would you do if you had my problem?
This is the solution I came up with (after adpting the code from kind Mr MatToufoutu):
from jinja2 import Template
my_template = Template("""
<html>
<style type="text/css">
img {border: none;}
</style>
<body>
<table border="0" cellpadding="0" and cellspacing="0">
<tr>
{% for url in urls %}
<td><a href="{{ url }}"><img width="100%" src="{{ url }}"/></td>
{% if loop.index is divisibleby cols %}
</tr><tr>
{% endif %}
{% endfor %}
</tr>
</table>
""")
cols=3
urls =["./a.jpg","./a.jpg","./a.jpg","./a.jpg","./a.jpg","./a.jpg","./a.jpg"]
html = my_template.render(cols=cols,urls=urls)
I think the easiest way to achieve this would be to use a template language like Mako, Cheetah or Jinja. Personally i'd choose Jinja, because it is very similar to the Django template language, but they are all very powerful.
A very simple example of what you want, using Jinja, would look like this:
from jinja2 import Template
my_template = Template("""
<html>
<body>
<table border="0">
<tr>
{% for url in urls %}
<td><a href="{{ url }}">{{ url }}</td>
{% endfor %}
</tr>
</table>
""")
urls = ["http://.....jpg","http://.....jpg","http://.....jpg","http://.....jpg"]
rendered_html = my_template.render(urls=urls)
Which is way more readable than building the html by hand like you did.
Spare a few minutes to learn a template engine like Jinja2. It really pays up, because you can focus on code and leave the layout to webdesigners.
精彩评论