I’m trying to display a list of dicts where one of the keys is to a base64 string.
So far, I have been unable to display the base64 strings as images during a for loop and not within a DataFrame.
My code:
JavaScript
x
5
1
from IPython import display
2
from base64 import b64decode
3
4
display.Image(b64decode(b64_str))
5
Unfortunately, while this works on a single code cell running a single base64 string, it does not show any image during the for loop
.
JavaScript
1
6
1
# imagine this is being done in a Jupyter Notebook code cell
2
arr = [{"b64_str": "...", "title": "product1"}, ]
3
4
for item in arr:
5
display.Image(b64decode(b64_str))
6
This also fails to show any image:
JavaScript
1
6
1
# again, imagine this is being done in a Jupyter Notebook code cell
2
arr = [{"b64_str": "...", "title": "product1"}, ]
3
4
for item in arr:
5
print( display.Image(b64decode(b64_str)) )
6
How can I get the base64 strings to display properly during the loop?
Advertisement
Answer
You’re missing a call to IPython.display.display
:
JavaScript
1
8
1
from IPython import display
2
3
jupyter = 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Jupyter_logo.svg/44px-Jupyter_logo.svg.png'
4
python = 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/110px-Python-logo-notext.svg.png'
5
6
for path in [jupyter, python]:
7
display.display(display.Image(path))
8