Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert python list elements in HTML <img> tag

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can I refer to my list in the img tag?

>Solution :

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>"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading