HTML 生成器、图库生成器还是模板?

发布于 2024-09-30 14:57:30 字数 1981 浏览 0 评论 0原文

问题的小版本:“我需要一个用于 python 或程序的库(Linux 首选),它接收图像的 url 列表并为我提供表格的 hmtl(可能很容易配置(行并查看))

问题的长版本: 我有一个带有图像url列表的数组,我想制作一个表格(我不知道这是否是最佳实践,但我认为这是最简单)。 我不在乎拇指是否与大拇指相同(只是被迫变小)。而且我不需要将图像复制到任何地方。

我使用以下代码( 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

我知道代码是可疑的,如果它是奇数,它会跳过最后一个......但它是咖啡因驱动的代码,我只需要完成它......没有什么值得我骄傲的:) 我知道一定有更好的方法(而且更漂亮……因为这看起来真的很难看),以及一种可以指定列数和其他选项的方法。

我找不到适合我的情况的图库生成器(我测试的所有内容都将文件复制到新目录)。 我应该学习模板语言吗?值得吗?

或者我应该使用 HTML 生成器

或者我应该研究更好的 HTML 编码?

如果您遇到我的问题,您会怎么做?

这是我想出的解决方案(在采用了 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)

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 and 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-10-07 14:57:30

我认为实现这一点的最简单方法是使用模板语言,例如 MakoCheetahJinja。我个人会选择 Jinja,因为它与 Django 模板语言非常相似,但它们都非常强大。

使用 Jinja 的一个非常简单的示例如下所示:

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)

这比像您一样手动构建 html 更具可读性。

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.

东京女 2024-10-07 14:57:30

花几分钟时间学习一下 Jinja2 这样的模板引擎。这真的很值得,因为您可以专注于代码,而将布局留给网页设计师。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文