[英]How to print images in a loop in html the code is shared below
I have made a loop of image urls through python and now I want to display all the images on html page using all the image urls ,i am using jinja for loop.我已经通过 python 制作了一个图像url循环,现在我想使用所有图像url在 html 页面上显示所有图像,我正在使用jinja for 循环。
what am I doing wrong here, need some help我在这里做错了什么,需要一些帮助
import boto3
session = boto3.Session(
aws_access_key_id='my_key',
aws_secret_access_key='My_key')
s3 = session.client('s3')
objects = s3.list_objects_v2(Bucket='My_bucket')
for obj in objects['Contents']:
print("\n")
print(obj['Key'])
print("\n")
url = s3.generate_presigned_url(ClientMethod='get_object',Params={'Bucket': "My_bucket", 'Key': obj['Key'], },ExpiresIn=36000,)
print(url)
f = open('index.html','w')
message = """<!DOCTYPE html>
<html>
<head>
<title>sahil</title>
</head>
<body>
<table>
<tr>
<td>
<ul>
{% for urls in url %}
<li>{{ <img src="urls"> }}</li>
{% endfor %}
</ul>
</td>
</tr>
</table>
</body>
</html>"""
f.write(message)
f.close()
There are two main issues with your code:您的代码有两个主要问题:
At no point are you rendering the Jinja template.您绝不会渲染 Jinja 模板。 This means that the message
variable is being written to the file as is, Jinja instructions and all.这意味着message
变量按原样、Jinja 指令和所有内容写入文件。 On top of that, since you're not actually saving each URL you generate in the Python loop, if you did render this template, it would operate on each character of the final URL you generate, which is less than useful.最重要的是,由于您实际上并没有保存在 Python 循环中生成的每个 URL,因此如果您确实渲染了此模板,它将对您生成的最终 URL 的每个字符进行操作,这不太有用。 Well, that is if you fixed the bug in the template, since it was attempting to execute some HTML.嗯,如果你修复了模板中的错误,因为它试图执行一些 HTML。
There are comments in this example on other minor issues.本例中还有其他小问题的评论。 That said, the inclusion of AWS secrets in code is not a minor issue.也就是说,在代码中包含 AWS 机密并不是一个小问题。 Do not do that.不要那样做。 It is a surprisingly common reason AWS secrets leak into the wild.这是 AWS 机密泄露到野外的一个非常普遍的原因。 You really should get out of that habit now, before it causes you problems.你现在真的应该改掉那个习惯,以免它给你带来麻烦。
import boto3
# Bring in the Template enging from Jinja
from jinja2 import Template
# NEVER store AWS secrets in code. Use one of the options available at
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html
session = boto3.Session()
s3 = session.client('s3')
# Create a pagination helper to get all objects if there are more than 1000
paginator = s3.get_paginator('list_objects')
# Store the generated URLs in an array
urls = []
for objects in paginator.paginate(Bucket='My_bucket'):
# Pull out the Contents for this page, it may not be present, so default to
# an empty list if this page has no items
for obj in objects.get('Contents', []):
# Note: Prevent problems by using the bucket name from the list_objects request
url = s3.generate_presigned_url(ClientMethod='get_object',Params={'Bucket': objects['Name'], 'Key': obj['Key'], },ExpiresIn=36000,)
# Add the new URL to the list of URLs
urls.append(url)
# Use with block to let Python manage the lifespan of the file object
with open('index.html','w') as f:
# Note: Compacting the HTML here just to make the example smaller
message = """<!DOCTYPE html>
<html><head><title>sahil</title></head>
<body><table><tr><td><ul>
{% for url in urls %}
<!-- The jinja portion here is just the "url" variable, the rest is raw HTML -->
<li> <img src="{{url}}"> </li>
{% endfor %}
</ul></td></tr></table></body></html>"""
# Need to actually call jinja to render the HTML
t = Template(message)
# Pass in the urls list to the render engine
f.write(t.render(urls=urls))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.