【问题标题】:Render Blob from sqlite server in HTML with Flask/Python使用 Flask/Python 在 HTML 中从 sqlite 服务器渲染 Blob
【发布时间】:2020-11-30 02:05:06
【问题描述】:

这段代码使用我的数据库中的值呈现一个 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)

这是呈现值的 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>

我对 python、flask 和一般编码都很陌生,所以如果有人花时间解释我如何成功地在 html 页面上显示来自 blob 列的数据,我将不胜感激。提前致谢

【问题讨论】:

  • 我会跳过尝试对它进行 b64 编码,而是根据提供的 ID 设置一个单独的端点来提供单个图像。我answered a similar question here。如果这对您没有帮助,请包括您如何计算 displayrows(即 SQL 查询),我可以根据您的代码编写一个定制的答案。
  • 感谢您的回复,我删除了 PIL 代码并在链接中实现了您的解决方案,我还向我的模板传递了一个变量,但是它返回字节对象的地址,如 &lt;_io.BytesIO object at 0x1071a83b0&gt;,我不明白

标签: python html blob


【解决方案1】:

几个观察:

  • 在 for 循环中不能有 return 语句。视图函数只能返回一次。
  • 您需要一个路由来呈现个人资料页面,另一个路由来返回图像。

我想我正在对您在这里尝试做的事情做出一些假设,但希望这是有道理的......


有一个呈现个人资料页面的路由:

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

然后在模板profile.html 中,在for 循环中使用flask 的url_for 函数生成指向图像的链接,将row[0](应该是该配置文件的ID)作为ident 参数传递:

{% for row in rows %}
        ...
        <img src='{{ url_for("profile_image", ident = row[0]) }}' />
        ...
{% endfor %}

url_for 函数将为下一条路线输出正确的超链接。请注意它是如何接受ident 的,这是配置文件ID。您需要将 your_blob_col 替换为包含 bolb 的列标题:

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

因此&lt;img&gt; 标记中的超链接将呈现为类似于/i/123 的内容,当由profile_image 函数处理时将返回图像数据。

您可以在浏览器的开发工具(网络选项卡)中检查这些请求,以便更好地了解正在发生的事情。如果您的 profile 函数改为提取 n 配置文件,您将看到对 profile 路由的 1 个请求,以及对 n 路由的 n 请求。

如果有任何不清楚的地方请告诉我:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多