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.

7
  • 2
    That's a limitation of the shell in general. It's a text interface. Clicking isn't a "thing". Commented May 19, 2017 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. Commented May 19, 2017 at 21:07
  • A duplicate of stackoverflow.com/questions/40419276/… Commented May 19, 2017 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? Commented May 19, 2017 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? Commented May 19, 2017 at 23:57

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

not working on python 3 , I only get a text 'This is a link" but its not a link
@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.
Ok I see ..I did it on bash on windows.
Related for PyCharm console: stackoverflow.com/questions/26300594/…
There is a shorter alternative print(f"\x1b]8;;{target}\a{text}\x1b]8;;\a")
0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.