通过flask/python在html中显示从couchDB附加的图像

2024-03-14

我正在使用 Flask 和 python 从 couchdb 获取图像附件,然后将图像传递到 imgurl.html 进行显示。 问题是我只得到这个:

返回 0x103b9c0b8> 处的 couchdb.http.ResponseBody 对象。

app.py

from flask import Flask, request, render_template, flash, request, url_for, redirect
import couchdb
import requests

app = Flask(__name__)

couch = couchdb.Server('http://127.0.0.1:5984/')

db = couch['test2']

doc = db.get_attachment('2', 'aboutme.jpg', default=None)
print("doc: ", doc)

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/imgurl/')
def imgurl():
    return render_template('imgurl.html', doc = doc)


@app.route('/page/')
def page():

    return("Andrew Irwin")    
    #return render_template('index.html')

if __name__ == '__main__':
    app.run()

测试.html

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>
    <img src="{{ doc }}" id="imgslot" >

    <h1 id="h1id"> {{ doc }} </h1>
</body>
</html>

一种选择可能是对从 couchdb 返回的图像数据进行 base64 编码,并将编码数据作为字符串传递到模板,您可以在其中渲染它。例如。

img = db.get_attachment('2', 'aboutme.jpg', default=None).read()
img_b64 = base64.b64encode(img)

def imgurl():
   return render_template('imgurl.html', doc = img_b64)

然后在模板中:

<img src="data:image/png;base64,{{ doc }}" id="imgslot" >

根据您的安全模型,另一个选项可能是通过将图像 url 添加到图像标签来直接从 couchdb 提供图像,例如获取附件的 url https://stackoverflow.com/questions/6803741/getting-url-for-an-attachment

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过flask/python在html中显示从couchDB附加的图像 的相关文章