22

I'm working on a console application. My application uses urwid lib. In some cases, I need to show very long hyperlinks as short text inside table columns. I want to open links in the browser when I click on the text inside the column.

So, my question is:

It is possible to print text as a hyperlink to the console?

Can you provide a small example of how to print text as a hyperlink using python?

2
  • 7
    That depends on your console, it has nothing to do with Python. FWIW, konsole, the standard KDE console, supports hyperlinks. If you print a URL to the console, it will get underlined if you hover over it, and if you right-click on it a menu pops up, and one of the menu options is "Open link". Commented Nov 4, 2016 at 9:38
  • 3
    You just print it syntactically correct. To identify the hyperlink is the job of the terminal application. Commented Nov 4, 2016 at 9:38

3 Answers 3

27

A bit late, but there totally is a way to do that now, without curses or anything, just pure text and collaboration from your terminal emulator.

def link(uri, label=None):
    if label is None: 
        label = uri
    parameters = ''

    # OSC 8 ; params ; URI ST <name> OSC 8 ;; ST 
    escape_mask = '\033]8;{};{}\033\\{}\033]8;;\033\\'

    return escape_mask.format(parameters, uri, label)

Call that function with link('https://example.com/') to get a simple, clickable link, or link('https://example.com/', 'example') for a custom label.

Note that links are faintly underlined in your terminal, and not all of them support the feature.

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

2 Comments

What are available values for parameters ?
Check this gist out: gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda The only parameter is id=someID that allows the emulators to highlight correctly a link that would be "broken" in multiple places, such as by tmux or a windowed text editor
4

Yes, using some tools, like gNewt or Curses, you could create a button and 'on click' do an action (like open a browser to a given url).

gNewt : http://gnewt.sourceforge.net/

nCurses : https://docs.python.org/3.7/library/curses.html

Otherwise, it's the terminal application that will manage the text you give it, and if it doesn't implement uri's recognition your program won't work as you'd like.

2 Comments

It should be! Using widgets : urwid.org/reference/widget.html see set_text() and mouse_event()
Did gNewt get a new website? The sourceforge link is broken.
1

No, some consoles do recognize urls and convert them to a clickable hyperlink. All you can do is make it easy to recognize for console applications by putting a http:// in your url.

Also see How does bash recognize a link?

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.