Skip to content Skip to sidebar Skip to footer

How To Insert Links In Python

Is it possible to add links into the Python script and print it out to the terminal/console? Like in HTML; once it was clicked, we will be redirected to the URL. (I'm on Linux) <

Solution 1:

It all depends on where you want to print it out to. Some output locations do not support clickable hyperlinks.

For example, if you printed your output to a basic terminal, you would not be able to click on it.

One suggestion is to use python's webbrowser module to open links:

import webbrowser
webbrowser.open("http://www.example.com")

, which will open the link for you in a new window.

You could also output the text to a HTML file and open the HTML file in a web browser for the link:

open("link.html", "w").write('<a href="http://www.example.com"> Link </a>')

Solution 2:

Recently (in 2017) a few terminal emulators (namely iTerm2, GNOME Terminal, and Tilix; hopefully more to follow) have added support for custom hyperlinks.

Assuming your python app's output goes to such a terminal emulator, you can create ctrl+clickable (cmd+clickable on mac) hyperlinks like this:

print("\x1b]8;;http://example.com/\aCtrl+Click here\x1b]8;;\a")

Further technical details are here.

Solution 3:

Yes it is possible here is a simple python cgi script that does what you describe.

print"Content-type: text/html"printprint"""
<html>
<head><title>Sample</title></head>
<body>
<a href='http://google.com'>google</a>
</body></html>
"""

you can learn more about cgi here http://en.wikipedia.org/wiki/Common_Gateway_Interface

Post a Comment for "How To Insert Links In Python"