0

I'm tying to get an image attachment from couchdb using flask & python then pass the image to imgurl.html to be displayed. The problem is that I'm only get this:

couchdb.http.ResponseBody object at 0x103b9c0b8> returned.

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>
Chris Snow
  • 23,813
  • 35
  • 144
  • 309
Andrew Irwin
  • 691
  • 12
  • 40

2 Answers2

1

One option could be to base64 encode the image data returned from couchdb and pass the encoded data as a string to the template where you can render it. E.g.

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)

Then in the template:

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

Another option depending on your security model could be to serve the image directly from couchdb by adding the image url to the image tag, e.g. Getting url for an attachment

Community
  • 1
  • 1
Chris Snow
  • 23,813
  • 35
  • 144
  • 309
  • Thanks very much for your answer I really appreciate it ! Before you answered though I managed to figure it by getting the the attachment name and appending it to the end of the url which I would then pass to the html img src. Now That Im able to download and display and image, Im trying to go the other way around, from html upload the couchdb. the image is going to be the canvas. and Im a bit stuck ! :( could you give me a hand with that? – Andrew Irwin Nov 28 '16 at 15:55
  • I think I've found the answer. It may be an idea to post as a separate question. – Chris Snow Nov 28 '16 at 18:39
  • yeah you can post it as a seperate question if you like. Im now trying to upload a html canvas which Im trying to convert to an image,then upload and store it in couch db as an attachment. – Andrew Irwin Nov 28 '16 at 19:07
  • Have a look here and see if this works for you http://stackoverflow.com/questions/40852404/how-to-upload-an-image-with-flask-and-store-in-couchdb/40852405#40852405 – Chris Snow Nov 28 '16 at 19:55
1

Before I saw @SHC's answer I managed to come up with a solution my self. What I did was retrieve the name of the attachment and then append that to the end of a url like i.e "http:localhost:5984/dbName/docId/attachmentName". I passed that url to the html img src and it worked then. Thank you you @SHC for your answer. sorry I didnt see it sooner.

Andrew Irwin
  • 691
  • 12
  • 40