Python. How to print text to console as hyperlink?

1 min read Original article ↗

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.