3

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)

<a href="URL">Click Here To Login</a> 
7
  • For example www.facebook.com Commented Mar 2, 2014 at 7:45
  • 1
    I mean: where = console / webpage / gui ... Commented Mar 2, 2014 at 7:46
  • Perhaps a better question would be, how are you running the python program? What do you hope to do with the finished program? Commented Mar 2, 2014 at 7:48
  • @falsetru , in Console (Linux Terminal) Commented Mar 2, 2014 at 7:48
  • 1
    You can't output a link, you can only output a file. The program that is reading the file, will know what to do with it. Your question is quite vague; and it is not clear what it is that you actually want. Commented Mar 2, 2014 at 8:00

4 Answers 4

7

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>')
Sign up to request clarification or add additional context in comments.

Comments

3

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/\x1b\\Ctrl+Click here\x1b]8;;\x1b\\")

Further technical details are here.

Comments

0

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

print "Content-type: text/html"
print

print """
<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

Comments

0

Also, just to add, some Python running applications automatically support clickable links. All you have to do is print("https://example.com/"), and once you click on it, it will take you to whatever website. In this case, it is example.com. One application that I know of that supports this is Replit.com.

Just so you know, if you are running it on the Python app (IDLE), then it won't work.

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.