2

Say, I have a list full of html links that looks something like this:

https://www.nytimes.com/2017/05/19/realestate/they-can-afford-to-buy-but-they-would-rather-rent.html

When I run a script in Python 3.6 Idle, I get the list as an output in Python 3.6 shell; however, they are not clickable. Is there a simple way to make them clickable in the shell?

I've googled around but I can't seem to find anything involving the Python shell.

Thank you.

Moondra
  • 4,399
  • 9
  • 46
  • 104
  • 2
    That's a limitation of the shell in general. It's a text interface. Clicking isn't a "thing". – Arya McCarthy May 19 '17 at 21:04
  • 1
    I don't know if this will help you at all, but there is the `webbrowser` module that will let you launch your default web browser at a provided URL. – FamousJameous May 19 '17 at 21:07
  • A duplicate of http://stackoverflow.com/questions/40419276/python-how-to-print-text-to-console-as-hyperlink – whackamadoodle3000 May 19 '17 at 22:30
  • @aryamccarthy I see. What options do I have? Jupyter Notebook allows me to have an output of clickable links. Besides this, any suggestion as to what I should look into? – Moondra May 19 '17 at 23:56
  • @FamousJameous Yeah, I know about `webrowser` but I think I would have to create a GUI, to do the whole "click to launch" right? – Moondra May 19 '17 at 23:57
  • @whackamadoodle3000 That link seems to be referring to output of command prompt, which is not what I'm asking. – Moondra May 20 '17 at 00:01
  • @Moondra did my answer work for you? – rdrey Jul 11 '19 at 19:42

2 Answers2

10

Auto-linking terminals

It depends on the terminal you're using. Some terminals have configurable rules (mostly regexes) that will recognise links in your tools' output and make them clickable.

See

Custom links

Some terminals also let you create custom links via special shell escape sequences:

Since you mentioned py3.6, I'm using f-strings:

text = "This is a link"
target = "http://example.com"
print(f"\u001b]8;;{target}\u001b\\{text}\u001b]8;;\u001b\\")

This will make a clickable link in some terminals. (iTerm2, Gnome Terminal or even iPython running in one of these terminals.) It doesn't seem to work in Alacritty and Kitty, however.

To learn more about hyperlink escape sequences, see this Gist or this blog post https://purpleidea.com/blog/2018/06/29/hyperlinks-in-gnome-terminal/

ipython example of hyperlinks

rdrey
  • 9,379
  • 4
  • 40
  • 52
  • 1
    not working on python 3 , I only get a text 'This is a link" but its not a link – Tokci Jul 14 '21 at 08:16
  • 1
    @Tokci It's got nothing to do with python versions. Your terminal has to support the feature. I've only tried a few like iTerm2 & Gnome Terminal. I just tested Kitty & Alacritty and it doesn't work in these. – rdrey Jul 14 '21 at 12:58
  • Ok I see ..I did it on bash on windows. – Tokci Jul 14 '21 at 16:22
  • Related for PyCharm console: https://stackoverflow.com/questions/26300594/print-code-link-into-pycharms-console – Stefan Nov 02 '22 at 19:14
  • There is a shorter alternative `print(f"\x1b]8;;{target}\a{text}\x1b]8;;\a")` – sound wave Mar 13 '23 at 16:46
0

Sorry. No. IDLE is the most basic of editors and the shell is just that.

KDM
  • 152
  • 12