'Diplaying images inline a table in Python from csv URLs
I am trying to display URLs from my CSV file as actual images in the table. If possible back in some sort of excel or CSV file.
I've looked at different using pandas.
My CSV file has many columns as shown in the output below, but I am trying to work with the thumbnail URLs. Eventually, I would like to do this from the list of URLs in photos, but it sounds more complicated than just working with the single URL I get from thumbnails.
Column Name: photos
Column Contents : ['[]' '[]' '[]' "['https://pbs.twimg.com/media/FGLX9-SVUAMntkm.jpg']"
"['https://pbs.twimg.com/tweet_video_thumb/FTA5Ze9VEAARfp5.jpg']" '[]'
'[]' "['https://pbs.twimg.com/media/FTA5YTGaAAEtjPI.jpg']"
"['https://pbs.twimg.com/media/FTA5X-9aMAAyqFB.jpg']" '[]'
"['https://pbs.twimg.com/media/FTA5WhBUEAAHPAk.jpg']" '[]'
"['https://pbs.twimg.com/media/FTA47SGVUAAgrKH.jpg']" '[]' '[]' '[]'
"['https://pbs.twimg.com/tweet_video_thumb/FTA5TlHVIAE_EHZ.jpg']" '[]'
'[]' '[]' "['https://pbs.twimg.com/media/FTA49bHacAEX5j_.jpg']"]
Column Name : video
Column Contents : [0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 1]
Column Name : thumbnail
Column Contents : [nan nan nan 'https://pbs.twimg.com/media/FGLX9-SVUAMntkm.jpg'
'https://pbs.twimg.com/tweet_video_thumb/FTA5Ze9VEAARfp5.jpg' nan nan
'https://pbs.twimg.com/media/FTA5YTGaAAEtjPI.jpg'
'https://pbs.twimg.com/media/FTA5X-9aMAAyqFB.jpg' nan
'https://pbs.twimg.com/media/FTA5WhBUEAAHPAk.jpg' nan
'https://pbs.twimg.com/media/FTA47SGVUAAgrKH.jpg' nan nan nan
'https://pbs.twimg.com/tweet_video_thumb/FTA5TlHVIAE_EHZ.jpg' nan nan nan
'https://pbs.twimg.com/media/FTA49bHacAEX5j_.jpg']
I have tried code found in other stackoverflow posts like this and websites like this.
This is what I have so far:
import pandas as pd
from IPython.display import display, Image, HTML
import base64
from PIL import Image
from io import BytesIO
df = pd.read_csv('file path/Documents/pandatweetmodi 2.csv')
# print(df)
#Debugging to check for column name
for column in df:
columnSeriesObj = df[column]
print('Column Name : ', column)
print('Column Contents : ', columnSeriesObj.values)
# TRY 1
def path_to_image_html(path):
return '<img src="'+ path + '" width="60" >'
# HTML(df.to_html(escape=False,formatters=dict(thumbnail=path_to_image_html)))
HTML(df.to_html(formatters={'thumbnail': path_to_image_html}, escape=False))
# print(df['thumbnail'])
# ***TRY 2***
# def path_to_image_html(path):
# return '<img src="'+ path + '" width="60" >'
# for row in df['thumbnail']:
# HTML(df.to_html(escape=False,formatters=dict(row=path_to_image_html)))
# print(df['thumbnail'])
#TRY 3
# df['img_html'] = df['thumbnail']\
# .str.replace(
# '(.*)',
# '<img src="\\1" style="max-height:124px;"></img>'
# )
# with pd.option_context('display.max_colwidth', 10000):
# HTML(df.to_html(escape=False))
# display(df['thumbnail'])
#display(df)
df.to_csv('pandafinish2.csv', encoding='utf-8', index=False)
The output of all the tries looks exactly the same, like this:
1 NaN
2 NaN
3 https://pbs.twimg.com/media/FGLX9-SVUAMntkm.jpg
4 https://pbs.twimg.com/tweet_video_thumb/FTA5Ze...
5 NaN
6 NaN
7 https://pbs.twimg.com/media/FTA5YTGaAAEtjPI.jpg
8 https://pbs.twimg.com/media/FTA5X-9aMAAyqFB.jpg
9 NaN
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
