[英]Render Blob from sqlite server in HTML with Flask/Python
This piece of code renders an html template with values from my db这段代码使用我的数据库中的值呈现一个 html 模板
displayrows = db.execute("SELECT * FROM profiles WHERE profile_id = :profile_id", (session["user_id"],))
displayrows = db.fetchall()
displaycount = db.execute("SELECT COUNT(*) FROM bucketlist WHERE wish_id = :wish_id", (session["user_id"],))
for i in displayrows:
bytes_io = BytesIO(i[5])
for i in displaycount:
return render_template("profile.html", rows = displayrows, count = i[0], image = bytes_io)
This is part of the html page which renders the values...这是呈现值的 html 页面的一部分...
<div class="card bg-dark" style="width: 18rem;">
<p class="list-group-item">BucketList Size - {{ count }}</p>
{%for row in rows%}
<ul class="list-group list-group-flush">
<li class="list-group-item">Location - {{ row[1] }}, {{row[2]}}</li>
<li class="list-group-item">Looking for - {{row[3] }}</li>
<li class="list-group-item">Bio - {{row[4]}}</li>
</ul>
{%endfor%}
<img src="{{image}}" />
</div>
I'm very new to python, flask, and coding in general so I would appreciate it if anybody takes time to explain how I can successfully display data from the blob column on an html page.我对 python、flask 和一般编码非常陌生,所以如果有人花时间解释我如何成功地在 ZFC35FDC70D5FC69D269883A822C7A53 页面上显示来自 blob 列的数据,我将不胜感激。 Thanks in advance
提前致谢
A couple of observations:几点观察:
I think I'm making some assumptions on what you're trying to do here, but hopefully this makes sense...我想我正在对您在这里尝试做的事情做出一些假设,但希望这是有道理的......
Have one route that renders the profile page:有一个呈现个人资料页面的路线:
@app.route('/profile')
def profile():
# You could pull several profiles here, but as you provide an id it just pulls one:
db.execute("SELECT * FROM profiles WHERE profile_id = ?", (session["user_id"],))
# Again, this supports multiple had the query been `SELECT * FROM profiles`
displayrows = db.fetchall()
return render_template("profile.html", rows = displayrows)
Then in the template profile.html
, within the for loop use flask's url_for
function to generate a link to the image, passing row[0]
(which should be that profile's id) as the ident
argument:然后在模板
profile.html
中,在 for 循环中使用烧瓶的url_for
function 生成图像链接,传递row[0]
(应该是该配置文件的 id)作为ident
参数:
{% for row in rows %}
...
<img src='{{ url_for("profile_image", ident = row[0]) }}' />
...
{% endfor %}
That url_for
function will output the correct hyperlink for the next route.那个
url_for
function 将 output 为下一条路线正确的超链接。 Notice how this accepts the ident
which is the profile id.请注意它如何接受作为配置文件 ID 的
ident
。 You'll need to replace your_blob_col
with the column title which holds the bolb:您需要将 your_blob_col 替换
your_blob_col
的列标题:
from flask import send_file
@app.route('/i/<int:ident>')
def profile_image(ident):
db.execute("SELECT your_blob_col FROM profiles WHERE profile_id = ?", (ident,))
result = db.fetchone() # There's only one
image_bytes = result[0] # The first and only column
bytes_io = BytesIO(image_bytes)
return send_file(bytes_io, mimetype='image/jpeg')
So the hyperlink in the <img>
tag would render as something like /i/123
which when handled by the profile_image
function will return the image data.因此,
<img>
标记中的超链接将呈现为类似于/i/123
的内容,当由profile_image
function 处理时,它将返回图像数据。
You can inspect these requests in your browser's dev tools (Network tab) for a better understanding of what's going on.您可以在浏览器的开发工具(网络选项卡)中检查这些请求,以便更好地了解正在发生的事情。 If your
profile
function had instead pulled n
profiles, you'd be looking at 1 request for the profile
route, and n
requests to the profile_image
route.如果您的
profile
function 已拉取n
配置文件,那么您将看到 1 个对profile
路由的请求,以及对profile_image
路由的n
请求。
Let me know if any of this isn't clear:)如果有任何不清楚的地方,请告诉我:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.