You seem to be a bit confused about what a hyperlink, well, is.
A text file is a file containing text. (It's simple, but it needs to be said!) It doesn't have pictures, animations, colours, headers, or anything like that. It's just text.
Since people often want more data with their text (x should be a heading, y should be red, z should make a funny cursor when you mouse over it), there are many schemes for encoding data about text. For example, Markdown is a text format used by StackOverflow. HTML is a markup language (a way of annotating text) that uses <tag> elements. It's useful because web browsers can take HTML pages as input and display them graphically.
A hyperlink as you describe it is a graphical element such as you might find on a website. You can't have them in a text file, because a text file is just text. But you can instruct a web browser to display a hyperlink by writing
<a href="where/you/want/the/link/to/go">text of the link</a>
If you open a file containing that in a web browser, it will display the text as a link. Note that files containing HTML are conventionally called something.html to indicate their contents, and that there are a bunch of required tags in any HTML document (<html><head></head><body></body></html>).
You seem to be a bit confused about what a hyperlink, well, is.
A text file is a file containing text. (It's simple, but it needs to be said!) It doesn't have pictures, animations, colours, headers, or anything like that. It's just text.
Since people often want more data with their text (x should be a heading, y should be red, z should make a funny cursor when you mouse over it), there are many schemes for encoding data about text. For example, Markdown is a text format used by StackOverflow. HTML is a markup language (a way of annotating text) that uses <tag> elements. It's useful because web browsers can take HTML pages as input and display them graphically.
A hyperlink as you describe it is a graphical element such as you might find on a website. You can't have them in a text file, because a text file is just text. But you can instruct a web browser to display a hyperlink by writing
<a href="where/you/want/the/link/to/go">text of the link</a>
If you open a file containing that in a web browser, it will display the text as a link. Note that files containing HTML are conventionally called something.html to indicate their contents, and that there are a bunch of required tags in any HTML document (<html><head></head><body></body></html>).
Creating hyperlinks in Python?
This is fairly trivial, as a hyperlink is of the format:
hyperlink_format = '<a href="{link}">{text}</a>'
One can parameterize this easily in Python. Here's several methods for doing so:
Call the .format method from the string
>>> hyperlink_format.format(link='http://foo/bar', text='linky text')
'<a href="http://foo/bar">linky text</a>'
Use a bound .format object:
link_text = hyperlink_format.format
Usage:
>>> link_text(link='http://foo/bar', text='foo bar')
'<a href="http://foo/bar">foo bar</a>'
Make a partial function
import functools
link_text = functools.partial(hyperlink_format.format)
Usage:
>>> link_text(link='http://foo/bar', text='linky text')
'<a href="http://foo/bar">linky text</a>'
Visit Example
```You can use this HTML string in your application to display the hyperlink. Alternatively, if you're working within a framework like Flask or Django, they provide their own methods for generating HTML, including hyperlinks.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>')
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.
If you're using sphinx to generate your documentation
"""
`here <url>`__
"""
in your docstrings should be parsed properly.
Just add the link as a string into the docstring, like so:
def foo():
"""Lorem ipsum for more info, see here: www.myfancydocu.com""
The doctring is just a string, so there is no Hyperlink. But anyone that wants to look at the website can just copy the link.
There are automatic documentation-builders that build a documentation out of your code and docstrings in e.g. html. Those can probably add hyperlinks to the documentation with a specific syntax, but that syntax then depends on which documentation-builder you use. If you only have your code, then just adding the url as a string is all you can do.