Home > Net >  Insert python list elements in HTML <img> tag
Insert python list elements in HTML <img> tag

Time:08-26

I am writing a code that I want to use to plot points on a map and insert a corresponding picture with a description. Among other things, the name of the place should be displayed below the image. I have stored the different names in Python as strings in a list.

names = ['location1', 'location2', 'location3']

I create the images in a foor loop like this:

for i in (range(len(df))):
        points = kml.newpoint(description='''<img src="path_to_picture.jpg" width="400" height="250"/><br><font size=" 1" font color="black">Name</font>''', coords=[(df['longitude'].iloc[i], df['latitude'].iloc[i], df['altitude'].iloc[i])])

The length of my dataframe 'df' and my list 'names' is equal.

Can I refer to my list in the img tag?

CodePudding user response:

The easiest way to do this is probably with an f-string

This can be done as an example seen below:

f"Name: {name_variable}" becomes f"Name: Harry" when interpreted.

In your case, as your list is the same length as the dataframe, and you have index you could replace your string with the following:

f"<img src="path_to_picture.jpg" width="400" height="250"/><br><font size=" 1" font color="black">{names[i]}</font>"

  • Related