[英]display image attach from couchDB in html via flask/python
我想使用flask&python从ouchdb中获取图像附件,然后将图像传递给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()
test.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>
一种选择是base64编码从couchdb返回的图像数据,然后将编码后的数据作为字符串传递给模板,以便在其中进行渲染。 例如
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添加到图像标签中来直接从ouchdb中提供图像,例如, 获取附件的URL。
在看到@SHC的答案之前,我设法提出了自己的解决方案。 我所做的是检索附件的名称,然后将其附加到URL的末尾,例如“ http:localhost:5984 / dbName / docId / attachmentName”。 我将该网址传递给html img src,然后它起作用了。 谢谢@SHC的回答。 对不起,我没早看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.