0

I have a text file, where I have multiple image links. I can read that file line by line in Python as hyperlinks. But I am trying to use those links as a pop-up when I will click the link, not to open in a different window. Any tools or any library that I can use?

enter image description here

4
  • is this in a jupyter notebook?
    – JonSG
    Commented Apr 26, 2023 at 13:33
  • Hello @JonSG, It is in google colab.
    – jonson
    Commented Apr 26, 2023 at 13:35
  • rather than print the raw url, can you print a complete a tag including the target attribute?
    – JonSG
    Commented Apr 26, 2023 at 13:36
  • @jonson, You can use tkinter and PIL library in Python Commented Apr 26, 2023 at 13:46

1 Answer 1

1

You can create a JavaScript popup link as follows:

<a href="some url" target="popup" onclick="window.open('some url','popup','width=w,height=h'); return false;">
some url
</a>

In Google Colab you can display HTML from Python using:

IPython.display.display(HTML(html content))

So you process the file and add the corresponding html to a result variable for each line, then display the full contents.

MCVE:

from IPython.display import display, HTML

links = ["https://i.sstatic.net/8lOtO.png", "https://imgs.xkcd.com/comics/helium_reserve.png", "https://i.sstatic.net/gPtbt.jpg"]

def makepopup(link):
  return f"""<a href="{link}" target="popup" onclick="window.open('{link}','popup','width=600,height=200'); return false;">{link}</a><br>"""

linklist = ""
for link in links:
  linklist += makepopup(link) + "\n"

display(HTML(linklist))

Result (screenshot after clicking the first link): enter image description here

Note that you do need to provide some fixed width and height, otherwise the popup is just full screen.

1
  • Thank you for your guideline. It was really helpful for me.
    – jonson
    Commented Apr 26, 2023 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.