lundi 2 novembre 2020

Image won't load in dash - python

I created an if statement in my app to load images. If the emotion is == value from the callback, load the image in the app.

# load image for sentiment

if overall_sentiment == "fear":
  image_filename = "fear.png" 
  image = base64.b64encode(open(image_filename, 'rb').read()).decode('ascii')
if overall_sentiment == "sadness":
  image_filename = "sadness.png" 
  image = base64.b64encode(open(image_filename, 'rb').read()).decode('ascii')
if overall_sentiment == "anger":
  image_filename = "anger.png" 
  image = base64.b64encode(open(image_filename, 'rb').read()).decode('ascii')
if overall_sentiment == "disgust":
  image_filename = "disgust.png" 
  image = base64.b64encode(open(image_filename, 'rb').read()).decode('ascii')
if overall_sentiment == "enjoyment":
  image_filename = "enjoyment" 
  image = base64.b64encode(open(image_filename, 'rb').read()).decode('ascii')

I load the image in my app like this:

    html.Img(
         html.H4("Overall sentiment"),
         id ="overall_sentiment",
         src='data:image/png;base64,{}'.format(image)
        )])

For some reason, the image will not show.

Aucun commentaire:

Enregistrer un commentaire