Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clickable hyperlinks

1,941 views
Skip to first unread message

Deborah Swanson

unread,
Jan 3, 2017, 3:37:34 PM1/3/17
to
Excel has a formula:

=HYPERLINK(url,description)

that will put a clickable link into a cell.

Does python have an equivalent function? Probably the most common use
for it would be output to the console, similar to a print statement, but
clickable.

Devin Jeanpierre

unread,
Jan 3, 2017, 3:58:07 PM1/3/17
to
Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way
to make an URL clickable is to use a terminal that makes URLs clickable,
and print the URL:

print("%s: %s" % (description, url))

-- Devin

On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson <pyt...@deborahswanson.net>
wrote:
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Deborah Swanson

unread,
Jan 3, 2017, 4:34:39 PM1/3/17
to
Devin Jeanpierre wrote, on January 03, 2017 12:57 PM

>Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The
way to
>make an URL clickable is to use a terminal that makes URLs clickable,
and
>print the URL:
>
>
>print("%s: %s" % (description, url))
>
>
>
>
>-- Devin

I'm sorry, I should have said a GUI console because I wouldn't expect a
text-based console to produce clickable links. But it appears that a
simple GUI console can't do it either. I have 3 GUI consoles and in all
3, when I ask:

print("%s: %s" % ("python.org list",
"https://mail.python.org/mailman/listinfo/python-list"))

I get:
python.org list: https://mail.python.org/mailman/listinfo/python-list

(Outlook made the url clickable here, the python GUI consoles just
output plain text.)

Pretty clearly the output is plain text because your format parameters
are %s. Maybe the trick, if there is one, is in a format parameter for
urls.

Dan Strohl

unread,
Jan 3, 2017, 4:36:23 PM1/3/17
to
The best bet (unless you know that you are outputting to a specific place, like html or excel) is to always include the "https://" or "http://" since most of the consoles / terminals that support clickable links are parsing them based on "seeing" the initial "http://". If your output just looks like "mysite.com/coolpage.html", many systems will simply ignore them.

At one point I had made a class that you could pass the link to, and then you could request different output based on your needs... so basically something like:

>> url = url_class("mysite.com/coolpage.html")
>> print(url)
"http://mysite.com/coolpage.html")
>> print(url.plain)
"mysite.com/coolpage.html"
>> print(url.html('My Site"))
'<a href="http://mysite.com/coolpage.html">My Site</a>'

(or whatever... I think I actually just sub-classed url.parse or something)




-----Original Message-----
From: Python-list [mailto:python-list-bounces+d.strohl=f5....@python.org] On Behalf Of Devin Jeanpierre
Sent: Tuesday, January 03, 2017 12:57 PM
To: pyt...@deborahswanson.net
Cc: comp.lang.python <pytho...@python.org>
Subject: Re: Clickable hyperlinks

Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to make an URL clickable is to use a terminal that makes URLs clickable, and print the URL:

print("%s: %s" % (description, url))

-- Devin

On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson <pyt...@deborahswanson.net>
wrote:

> Excel has a formula:
>
> =HYPERLINK(url,description)
>
> that will put a clickable link into a cell.
>
> Does python have an equivalent function? Probably the most common use
> for it would be output to the console, similar to a print statement,
> but clickable.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list

Dan Strohl

unread,
Jan 3, 2017, 4:48:13 PM1/3/17
to
Keeping mind how this all works...

Python is providing the data, the console/terminal/app handles how that data is displayed. There is no specification for text output to be hyperlinked (that I know about at least), so while some apps may handle specific coding to tell them that "this text should be a hyperlink", most will either parse the text looking for hyper-linkable string, or more commonly, just output the text as text.

If you control the app that is displaying info to the user (such as using QT or another GUI tool), or generating html yourself, you can control if a text string is a hyperlink or not, and what to do with the hyperlink when clicked.

So, if you want clickable links, you would need to find a console/terminal that supported them.

Some references:
https://opensource.com/life/15/11/top-open-source-terminal-emulators (see Gnome)
http://unix.stackexchange.com/questions/63417/is-there-a-terminal-app-that-allows-filenames-to-be-clickable
http://superuser.com/questions/654116/configure-os-x-terminal-to-detect-urls-and-make-them-clickable
http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/url-launching.html


-----Original Message-----
From: Python-list [mailto:python-list-bounces+d.strohl=f5....@python.org] On Behalf Of Deborah Swanson
Sent: Tuesday, January 03, 2017 1:35 PM
To: 'Devin Jeanpierre' <jeanpi...@gmail.com>
Cc: 'comp.lang.python' <pytho...@python.org>
Subject: RE: Re: Clickable hyperlinks

Devin Jeanpierre wrote, on January 03, 2017 12:57 PM

>Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The
way to
>make an URL clickable is to use a terminal that makes URLs clickable,
and
>print the URL:
>
>
>print("%s: %s" % (description, url))
>
>
>
>
>-- Devin

I'm sorry, I should have said a GUI console because I wouldn't expect a text-based console to produce clickable links. But it appears that a simple GUI console can't do it either. I have 3 GUI consoles and in all 3, when I ask:

print("%s: %s" % ("python.org list",
"https://mail.python.org/mailman/listinfo/python-list"))

I get:
python.org list: https://mail.python.org/mailman/listinfo/python-list

(Outlook made the url clickable here, the python GUI consoles just output plain text.)

Pretty clearly the output is plain text because your format parameters are %s. Maybe the trick, if there is one, is in a format parameter for urls.



Tim Chase

unread,
Jan 3, 2017, 5:37:18 PM1/3/17
to
On 2017-01-03 11:46, Deborah Swanson wrote:
> Excel has a formula:
>
> =HYPERLINK(url,description)
>
> that will put a clickable link into a cell.
>
> Does python have an equivalent function? Probably the most common
> use for it would be output to the console, similar to a print
> statement, but clickable.

Depends on what you're outputting. In your context, you're creating
content (and metadata) for a cell in an Excel workbook.

If that's the case you might have to use something like the xlwt
module to create an Excel-style worksheet and adjust the properties
of the cell to include the hyperlink property.

Or you can write out a .csv file with a hyperlink in a cell, which I
believe Excel can interpret as a hyperlink.

Or write an HTML document with the corresponding HTML <a> tag in it.

Or you can just print it to stdout as normal as some terminals detect
them and auto-linkify them.

But you have to specify where you want this link to appear to know
how to solve the problem.

-tkc



Grant Edwards

unread,
Jan 3, 2017, 6:13:43 PM1/3/17
to
On 2017-01-03, Deborah Swanson <pyt...@deborahswanson.net> wrote:

> I'm sorry, I should have said a GUI console because I wouldn't expect a
> text-based console to produce clickable links.

What's a "GUI console"?

--
Grant Edwards grant.b.edwards Yow! I want you to MEMORIZE
at the collected poems of
gmail.com EDNA ST VINCENT MILLAY
... BACKWARDS!!

Erik

unread,
Jan 3, 2017, 6:30:46 PM1/3/17
to
Hi.

On 03/01/17 19:46, Deborah Swanson wrote:
> Excel has a formula:

When you start a new topic on the list, could you please write a new
message rather than replying to an existing message and changing the
title/subject?

For those reading the list in a threaded email client, this message is
shown as a continuation of your "Cleaning up conditionals" thread, and
that whole thread in turn shows up as a continuation of the "mentor
training python Romania with certification" discussion (which you had
presumably "reply"ed to originally) ...

Thanks. E.

Deborah Swanson

unread,
Jan 3, 2017, 6:31:47 PM1/3/17
to
Grant Edwards wrote, on January 03, 2017 3:13 PM
The GUI consoles I have are in Pycharm, the IDLE that comes with
Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when
I open them, so they're capable of opening links, but whether that means
their output space is capable of handling clickable links I don't know.

I do know printing a full url with the %s specifier or entering a url
and clicking enter just gives you the plain text url. Obviously, not all
GUI consoles are enabled recognize and make clickable links from
correctly formatted urls.

I was hoping there was some functionality in python to make clickable
links. Could be a package, if the core language doesn't have it.

Deborah Swanson

unread,
Jan 3, 2017, 7:49:43 PM1/3/17
to
Erik wrote, on January 03, 2017 3:30 PM
> To: pytho...@python.org
> Subject: Re: Clickable hyperlinks
>
Certainly. I've been on many other lists before (but none since about
2011), and no one complained of or even mentioned this problem. But if
it's a problem with some email readers now, I can easily just start a
new message. Just being lazy and following old habits, I guess. ;)

Steve D'Aprano

unread,
Jan 3, 2017, 7:56:29 PM1/3/17
to
On Wed, 4 Jan 2017 10:32 am, Deborah Swanson wrote:


> The GUI consoles I have are in Pycharm, the IDLE that comes with
> Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when
> I open them, so they're capable of opening links, but whether that means
> their output space is capable of handling clickable links I don't know.
>
> I do know printing a full url with the %s specifier or entering a url
> and clicking enter just gives you the plain text url. Obviously, not all
> GUI consoles are enabled recognize and make clickable links from
> correctly formatted urls.
>
> I was hoping there was some functionality in python to make clickable
> links. Could be a package, if the core language doesn't have it.

Unfortunately there is no such thing as an application-
independent "clickable link".

Excel can make clickable links, because it only has to deal with a single
suite of related applications: Excel, Word, and the rest of Microsoft
Office. Likewise LibreOffice.

But Python has to deal with an infinite number of potential or hypothetical
consoles, and there is no standard for "clickable links" that all, or even
most, consoles understand.

Python can't force the console to treat something as a clickable link, if
the console has no capacity for clickable links. Nor can Python predict
what format the console uses to recognise a link.

The best you can do is to experiment with a couple of simple formats and see
which, if any, your console understands:

# HTML
<a href="http://www.example.com">Example.</a>

# URL in angle brackets
Example <http://www.example.com>

# URL alone
http://www.example.com

# I can't remember what these are called
<url:http://www.example.com>

# Markup
[Example](http://www.example.com)

# Rest
`Example <http://www.example.com>`_





--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

Michael Torrie

unread,
Jan 3, 2017, 8:23:21 PM1/3/17
to
On 01/03/2017 04:32 PM, Deborah Swanson wrote:
> The GUI consoles I have are in Pycharm, the IDLE that comes with
> Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when
> I open them, so they're capable of opening links, but whether that means
> their output space is capable of handling clickable links I don't know.

Hmm I still don't understand what you mean by "GUI console." Just
because a program asks for internet access does not mean anything about
whether or not your python program can display a clickable link in a
console window. Besides that, a clickable link would probably ask the
OS to open it with the default application, not cause your program to
access the internet.

The standard out pipe (where stuff goes when you call print() or write
to sys.stdout) from a python program usually goes to a console or a
terminal emulator. PyCharm and IDLE both provide windows to display
this output (they emulate a terminal). But maybe you're misunderstanding
what this actually is. These windows just display a stream of bytes
that come out of your program. Certain escape codes can be emitted that
can instruct the console or terminal emulator to do things like set text
color or display text at a certain position. But there's certainly no
special way to instruct the terminal emulator to make a section of text
into a hyperlink. Maybe if hyperlinks had existed years ago when
terminal escape codes were being defined we'd have a "hyperlink" code
that all consoles and terminals would understand. A few years ago I saw
some proposals to add an escape code to the ANSI scheme that would
encode hyperlinks, but nothing ever came of it because, honestly, it
would be too much hassle to roll this out to ever terminal emulator out
there (to say nothing of real terminals).

> I do know printing a full url with the %s specifier or entering a url
> and clicking enter just gives you the plain text url. Obviously, not all
> GUI consoles are enabled recognize and make clickable links from
> correctly formatted urls.

On my Linux machine, the terminal emulators I've used all make a regular
url printed out into a clickable link (or at least a right-clickable
link). This is just something they try to do with all things that look
like urls. Sometimes it's helpful, often it's annoying.

> I was hoping there was some functionality in python to make clickable
> links. Could be a package, if the core language doesn't have it.

No, there is not. If you made a full GUI app using a toolkit like GTK
or Qt, you can indeed place hyperlinks on your forms and the operating
system will automatically connect them to the web browser. But not in
text-mode terminal apps.

David

unread,
Jan 3, 2017, 9:36:19 PM1/3/17
to
On 4 January 2017 at 11:50, Deborah Swanson <pyt...@deborahswanson.net> wrote:
> Erik wrote, on January 03, 2017 3:30 PM
>>
>> When you start a new topic on the list, could you please write a new
>> message rather than replying to an existing message and changing the
>> title/subject?
>>
> Certainly. I've been on many other lists before (but none since about
> 2011), and no one complained of or even mentioned this problem. But if
> it's a problem with some email readers now, I can easily just start a
> new message. Just being lazy and following old habits, I guess. ;)

To be clear, the problem is not "with some email readers".

The problem is that it breaks the thread management features that are
built into every email message, making it impossible to display threads
correctly.

As can be seen here for example:
https://mail.python.org/pipermail/python-list/2017-January/thread.html

Note how most threads start in the leftmost column and replies are indented.
But this does not occur where you began the new subject: "Clickable hyperlinks".

Thanks.

Deborah Swanson

unread,
Jan 3, 2017, 10:04:16 PM1/3/17
to
Steve D'Aprano wrote, on January 03, 2017 4:56 PM

> On Wed, 4 Jan 2017 10:32 am, Deborah Swanson wrote:
>
>
> > The GUI consoles I have are in Pycharm, the IDLE that comes with
> > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access
> > when I open them, so they're capable of opening links, but whether
> > that means their output space is capable of handling
> clickable links I
> > don't know.
> >
> > I do know printing a full url with the %s specifier or
> entering a url
> > and clicking enter just gives you the plain text url.
> Obviously, not
> > all GUI consoles are enabled recognize and make clickable
> links from
> > correctly formatted urls.
> >
> > I was hoping there was some functionality in python to make
> clickable
> > links. Could be a package, if the core language doesn't have it.
>
> Unfortunately there is no such thing as an application-
> independent "clickable link".

I'm getting that.
I tried all of your examples in IDLE, and they all get syntax errors.
I'd expect the same from PyCharm, though I didn't try it.

I think I need to write a class to do it in a particular application,
preferably in PyCharm, though I'd need some kind of internet access
engine to do it anywhere. I can look up how Firefox does it, pretty sure
I have that somewhere. Maybe there's a way...

Maybe it's a stupid idea, but originally I wanted to output links and
click on them while I was still debugging in PyCharm, without having to
save the csv and reopen it in Excel, to see what was in a listing* while
I was still debugging. PyCharm does have links in its console output
that take you to positions in the code when you click on them, so it
seems like all the basic infrastructure is there. I just have to figure
out how to do it for internet urls that I output.

* listing is a real estate listing with a url, part of a project I'm
working on, in case you haven't read the "Cleaning up conditionals"
thread.

Deborah Swanson

unread,
Jan 3, 2017, 10:46:21 PM1/3/17
to
David wrote, on January 03, 2017 6:36 PM
>
> On 4 January 2017 at 11:50, Deborah Swanson
> <pyt...@deborahswanson.net> wrote:
> > Erik wrote, on January 03, 2017 3:30 PM
> >>
> >> When you start a new topic on the list, could you please
> write a new
> >> message rather than replying to an existing message and
> changing the
> >> title/subject?
> >>
> > Certainly. I've been on many other lists before (but none
> since about
> > 2011), and no one complained of or even mentioned this
> problem. But if
> > it's a problem with some email readers now, I can easily
> just start a
> > new message. Just being lazy and following old habits, I guess. ;)
>
> To be clear, the problem is not "with some email readers".

Actually it is, or at least it doesn't happen in all email readers.
Mine, for instance, never breaks up threads.

> The problem is that it breaks the thread management features
> that are built into every email message, making it impossible
> to display threads correctly.

Again, I think this is in some modern email readers. Apparently older
ones were more robust.

> As can be seen here for example:
> https://mail.python.org/pipermail/python->
list/2017-January/thread.html
>
> Note how most threads start in the leftmost column and
> replies are indented. But this does not occur where you began
> the new subject: "Clickable hyperlinks".

Yes, pipermail does seem to have a few bugs, this is the second one I've
seen.

> Thanks.

I did say in the message you're replying to that I will try to remember
to start new threads with brand new messages. (Even though I think
pipermail's behavior is a bug, that's what many people read the list
from.)

Michael Torrie

unread,
Jan 3, 2017, 10:51:52 PM1/3/17
to
On 01/03/2017 08:28 PM, Deborah Swanson wrote:
> I think you're making this too complicated. I meant a console in a GUI
> application.

Ahh. Well, a "console in a GUI application" is whatever you make it[1].
There's no single "GUI console" hence my confusion and the confusion
expressed by the other poster. I was under the impression you are
talking about printing something to standard out with, for example,
print(). Is this so, or are you using a GUI toolkit to construct your
application. What GUI toolkit are you using? As I said, in Qt or GTK
there are various ways to display hyperlinks. For example, Qt lets you
place a hyperlink in a form, or inside of a text entry/display widget.

I still get the impression that you're working with standard out, using
print(). If so, then no, there's not going to be a way to force the OS
to make the output clickable, at least on Windows.

> Not true. Pycharm uses links in it's console output, they just aren't
> internet links. They link back to lines of code being referred to.

I think the problem here is the terminology with specific meaning in
Windows and Linux. I'm referring to either the Win32 console window
(which is where cmd.exe runs), or a terminal emulator in Linux, which is
where you can interact with the bash shell and run command-line
programs. When people normally run python apps that are not graphical,
they normally do it from the Windows console (via cmd.exe) or in Linux
from Bash running in a terminal emulator. Graphical apps do their own
thing as far as displaying data and making windows with clickable links
in them.

[1] PyCharm and IDLE make "console" windows that are really normal GUI
windows and they direct the output from Python apps there. They may also
choose to display clickable links for things like errors. But once the
app is run outside of PyCharm, the output of the app would go to either
a Windows console window, or a terminal in Linux. If you wanted your
app to make it's own window and display clickable links, you're back to
looking at a GUI toolkit (which is what PyCharm and IDLE are built with)
like Qt, GTK, Tk, wxWidgets, or something else.


Steven D'Aprano

unread,
Jan 3, 2017, 11:04:16 PM1/3/17
to
On Wednesday 04 January 2017 14:04, Deborah Swanson wrote:

> Steve D'Aprano wrote, on January 03, 2017 4:56 PM
[...]
>> Python can't force the console to treat something as a
>> clickable link, if the console has no capacity for clickable
>> links. Nor can Python predict what format the console uses to
>> recognise a link.
>>
>> The best you can do is to experiment with a couple of simple
>> formats and see which, if any, your console understands:
>>
>> # HTML
>> <a href="http://www.example.com">Example.</a>
>>
>> # URL in angle brackets
>> Example <http://www.example.com>
>>
>> # URL alone
>> http://www.example.com
>>
>> # I can't remember what these are called
>> <url:http://www.example.com>
>>
>> # Markup
>> [Example](http://www.example.com)
>>
>> # Rest
>> `Example <http://www.example.com>`_
[...]

> I tried all of your examples in IDLE, and they all get syntax errors.
> I'd expect the same from PyCharm, though I didn't try it.

Syntax errors? How can you get syntax errors from *output* text?

The above are just text, no different from:

Hello World!


Of course you have to put quotes around them to enter them in your source code.
We don't expect this to work:

print(Hello World!)


you have to use a string literal with quotes:

print('Hello World!')


Same for all of the above.

py> print('<a href="http://www.example.com">Example.</a>')
<a href="http://www.example.com">Example.</a>


That's the Python interactive interpreter (with a custom prompt, I prefer "py>"
rather than the default ">>>"). Or I can do this from the operating system's
shell prompt:

steve@runes:~$ python -c "print 'http://www.example.com'"
http://www.example.com


If I do this in GNOME Terminal 2.30.2, the URL http... is a clickable link. But
that's specific to the terminal. Other terminals may or may not recognise it.




--
Steven
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." - Jon Ronson

Michael Torrie

unread,
Jan 3, 2017, 11:05:34 PM1/3/17
to
On 01/03/2017 08:46 PM, Deborah Swanson wrote:
> Actually it is, or at least it doesn't happen in all email readers.
> Mine, for instance, never breaks up threads.

Mine doesn't either, which illustrates the issue. This message, for
example appears under a long thread that started out life as "mentor
training python Romania with certification" and then changed to
"Cleaning up conditionals" and then changed to "Clickable hyperlinks."
All in one thread. My client doesn't break them up because they all tie
together via the message-id header.

And most of us would not like our client to break a thread just because
the subject changes. Often in long conversations there are twists and
turns in the discussion and sometimes side-paths are explored, and the
subject often is changed to reflect this. With a truly threaded email
reader (one that shows a tree of messages, not just chronological
order), this works out very well. So if a discussion has a natural
evolution into various other topics, it is often considered okay to just
change the subject but continue the thread. Other times, it's better to
start a new thread. Where that line is is hard to say!

> I did say in the message you're replying to that I will try to remember
> to start new threads with brand new messages. (Even though I think
> pipermail's behavior is a bug, that's what many people read the list
> from.)

Sounds good.

I don't know of anyone that reads on the pipermail archive, except in
response to web searches. Most people use clients of some kind, NNTP or
email. And those that group messages according to message-id (most
clients except for ones that try to be smart like Gmail web or Outlook)
will show all the topics I mentioned before as one giant thread, which
is by design (that's what message-id headers are for).

Deborah Swanson

unread,
Jan 3, 2017, 11:46:08 PM1/3/17
to
Steven D'Aprano wrote, on January 03, 2017 8:04 PM
I closed the IDLE window these were on, but the error arrow was pointing
to punctuation in each case, a colon in one case, angle bracket in
another. Sorta seems like IDLE is trying to do something with them and
not taking them as simple plain text. But it's not making hyperlinks, so
I'm not sure how much I care exactly what it's doing.

> Of course you have to put quotes around them to enter them in
> your source code.
> We don't expect this to work:
>
> print(Hello World!)
>
>
> you have to use a string literal with quotes:
>
> print('Hello World!')
>
>
> Same for all of the above.

I didn't try printing them before, but I just did. Got:

>>> print([Example](http://www.example.com)

SyntaxError: invalid syntax (arrow pointing at the colon)

> py> print('<a href="http://www.example.com">Example.</a>')
> <a href="http://www.example.com">Example.</a>
>
>
> That's the Python interactive interpreter (with a custom
> prompt, I prefer "py>"
> rather than the default ">>>"). Or I can do this from the
> operating system's
> shell prompt:
>
> steve@runes:~$ python -c "print 'http://www.example.com'"
http://www.example.com


If I do this in GNOME Terminal 2.30.2, the URL http... is a clickable
link. But
that's specific to the terminal. Other terminals may or may not
recognise it.




--
Steven
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." - Jon Ronson

I need to get a new power supply in my Linux-capable machine, but for
now I'm stuck with Winblows on a very old PC.

As I've mentioned in other posts on this thread, I'm now thinking that I
need to write a class to do this, and find out how Firefox and url aware
terminals in Linux do it. There must be a way.

boB Stepp

unread,
Jan 4, 2017, 12:18:35 AM1/4/17
to
On Tue, Jan 3, 2017 at 10:46 PM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
>

>
> I didn't try printing them before, but I just did. Got:
>
> >>> print([Example](http://www.example.com)
>
> SyntaxError: invalid syntax (arrow pointing at the colon)

As Steve had said, you need to put everything inside quotes. Also,
you are missing a matching paren. Thus:

py3: print("[Example](http://www.example.com)")
[Example](http://www.example.com)

As to whether anything will be "clickable" or not, what has already
been said about different types of terminals applies.

--
boB

Steven D'Aprano

unread,
Jan 4, 2017, 12:40:14 AM1/4/17
to
On Wednesday 04 January 2017 15:46, Deborah Swanson wrote:

> Steven D'Aprano wrote, on January 03, 2017 8:04 PM
[...]
>> Of course you have to put quotes around them to enter them in
>> your source code.
>> We don't expect this to work:
>>
>> print(Hello World!)
>>
>>
>> you have to use a string literal with quotes:
>>
>> print('Hello World!')
>>
>>
>> Same for all of the above.

> I didn't try printing them before, but I just did. Got:
>
>>>> print([Example](http://www.example.com)
>
> SyntaxError: invalid syntax (arrow pointing at the colon)

You missed the part where I said you have to put them in quotes.

Like any other string in Python, you have to use quotation marks around it for
Python to understand it as a string. None of these things will work:

print( Hello World! )

print( What do you want to do today? )

print( 3 2 1 blast off )

print( http://www.example.com )


This isn't specific to print. This won't work either:

message = Hello World!

In *all* of these cases, you have to tell Python you're dealing with a string,
and you do that with quotation marks:

message = "Hello World!"
print( 'What do you want to do today?' )
count_down = '3 2 1 blast off'
url = 'http://www.example.com'

Dan Sommers

unread,
Jan 4, 2017, 1:08:46 AM1/4/17
to
On Wed, 04 Jan 2017 16:40:00 +1100, Steven D'Aprano wrote:

> On Wednesday 04 January 2017 15:46, Deborah Swanson wrote:
>
>> Steven D'Aprano wrote, on January 03, 2017 8:04 PM
> [...]
>>> Of course you have to put quotes around them to enter them in
>>> your source code.
>>> We don't expect this to work:
>>>
>>> print(Hello World!)
>>>
>>> you have to use a string literal with quotes:
>>>
>>> print('Hello World!')
>>>
>>> Same for all of the above.
>
>> I didn't try printing them before, but I just did. Got:
>>
>>>>> print([Example](http://www.example.com)
>>
>> SyntaxError: invalid syntax (arrow pointing at the colon)
>
> You missed the part where I said you have to put them in quotes.

ObPython: "When I've got these antlers on I am dictating and when I
take them off I am not dictating." :-)

Paul Rudin

unread,
Jan 4, 2017, 3:22:22 AM1/4/17
to
"Deborah Swanson" <pyt...@deborahswanson.net> writes:

>
> I didn't try printing them before, but I just did. Got:
>
>>>> print([Example](http://www.example.com)
>
> SyntaxError: invalid syntax (arrow pointing at the colon)
>

With respect, if you typed that at python then it's probably a good idea
to take a step back and work through the excellent tutorial.

https://docs.python.org/3/tutorial/

Deborah Swanson

unread,
Jan 4, 2017, 4:00:36 AM1/4/17
to
Michael Torrie wrote, on January 03, 2017 8:05 PM
I suppose. Times change of course, which always suits some and not
others. Personally, I think putting messages that have different titles
all in one thread is a bad design, but as I've said a couple of times
now I intend to comply with the new rules. But compliance doesn't imply
agreement. I prefer the old system, which ordered threads by titles, but
there's obviously no pleasing everyone on this issue.

Deborah Swanson

unread,
Jan 4, 2017, 4:31:47 AM1/4/17
to
Steven D'Aprano wrote, on January 03, 2017 9:40 PM
Thanks, Steven. Yes, of course if you want to print strings you must
enclose them in quotes. I think you learn that in Week 1 of any
introductory course on Python.

But we aren't trying to print strings here, the point is to produce
clickable links. I didn't enclose them with quotes because I didn't see
any point in printing plain text when I wanted clickable links. I
actually didn't understand why you thought I should print them, but it
never would have occurred to me that you wanted me to print out a bunch
of silly plain text strings, apparently just for the heck of it.

At this point, if I pursue this any farther, it will be to look into how
Firefox takes webpage titles and urls out of its sqlite database and
makes objects you can click on to open the webpages. That's the basic
technology I'd need to find a way to talk (write) python into doing.

If it's even worth it. On a practical level it's not worth it, way too
much work for the teensy advantage of having it to use. It might be some
giggles to figure out how to do it and maybe I will sometime just for
funsies.

My original question was whether python had anything to provide this
functionality, and the answer appears to be a resounding NO!!!

Answer received, and thanks to all who contributed.

Steve D'Aprano

unread,
Jan 4, 2017, 5:10:28 AM1/4/17
to
On Wed, 4 Jan 2017 08:00 pm, Deborah Swanson wrote:

[speaking of threading emails]

> I suppose. Times change of course, which always suits some and not
> others. Personally, I think putting messages that have different titles
> all in one thread is a bad design, but as I've said a couple of times
> now I intend to comply with the new rules. But compliance doesn't imply
> agreement. I prefer the old system, which ordered threads by titles, but
> there's obviously no pleasing everyone on this issue.

Indeed :-)

However, as far as I am aware, the use of threading by following the
In-Reply-To and References header lines goes back to at least 1982, which
makes them pretty old, and certainly pre-dates Gmail and Outlook by many
years. They're also official standards which ALL email programs are
supposed to follow, so if Gmail and Outlook fail to follow the standard,
well, I wouldn't be surprised. I don't absolutely know for a fact that
threading in this way is older than threading by subject line, but I'm
fairly confident that what you are calling the "new rules" are actually the
much older rules.

Are there any old-timers here who were using email prior to 1982 that would
care to comment?


Here's a discussion by Jamie Zawinski, who wrote Netscape Navigator, before
it became Mozila and Firefox, so he knows what he's talking about:

https://www.jwz.org/doc/threading.html


The concept here is not so much that people start a new topic and change the
subject, but that sometimes the topic just naturally evolves to the point
that a change in subject is sensible. Take this thread for example: the
topic has drifted from "Clickable hyperlinks" to talking about email
threading. I should be able to change the subject without breaking the
thread:

Clickable hyperlinks
├─ RE: Clickable hyperlinks
│ ├─ Re: RE: Clickable hyperlinks
│ └─ RE: Clickable hyperlinks
│ └─ Threading [was Re: Clickable hyperlinks]
│ └─ Re: Threading
├─ RE: Clickable hyperlinks
└─ Re: Clickable hyperlinks


Adding a "Re:" or "RE" to the subject doesn't change the thread, and neither
does changing the subject line in a more substantial way.

Of course, I can always *choose* to sort by subject line, or date.
Personally I hardly ever sort by thread, so it is no skin off my nose what
you do. But when you hit "Reply" to a message, you inherit the "Reference"
and "In-Reply-To" headers from the previous message, so at least some
people will see it threaded in an existing thread rather than starting a
brand new one.

Steve D'Aprano

unread,
Jan 4, 2017, 5:20:47 AM1/4/17
to
On Wed, 4 Jan 2017 03:46 pm, Deborah Swanson wrote:

> As I've mentioned in other posts on this thread, I'm now thinking that I
> need to write a class to do this, and find out how Firefox and url aware
> terminals in Linux do it. There must be a way.


A GUI application can interpret text any way it chooses. Firefox takes a
HTML file and renders it, using whatever GUI library it chooses. That GUI
library understands that text like:

<b>Hello World!</b>

should be shown in bold face, and text like:

<a href="http://www.example.com">Example</a>

should be shown as the word "Example" underlined and in some colour, and
when you click on it the browser will navigate to the URL given. Firefox
can do this because it controls the environment it runs in.

Same for Excel, which also controls the environment it runs in.

That's *not* the case for Python, which is at the mercy of whatever console
or terminal application it is running in.

However, you can use Python to write GUI applications. Then it becomes
*your* responsibility to create the window, populate it with any buttons or
text or scroll bars you want, and you can choose to interpret text any way
you like -- including as clickable Hyperlinks.

The bottom line is, there is no "a way" to do this. There are a thousand,
ten thousand, ways to do it. Every web browser, every terminal, every
URL-aware application, can choose its own way to do it. There's no one
single way that works everywhere, but if you are working in a console or
terminal, just printing the URL is likely to be interpreted by the console
as a clickable link:

print("http://www.example.com")

Steve D'Aprano

unread,
Jan 4, 2017, 5:39:26 AM1/4/17
to
On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote:

> Thanks, Steven. Yes, of course if you want to print strings you must
> enclose them in quotes. I think you learn that in Week 1 of any
> introductory course on Python.
>
> But we aren't trying to print strings here, the point is to produce
> clickable links. I didn't enclose them with quotes because I didn't see
> any point in printing plain text when I wanted clickable links. I
> actually didn't understand why you thought I should print them, but it
> never would have occurred to me that you wanted me to print out a bunch
> of silly plain text strings, apparently just for the heck of it.

What we have here is a failure to communicate :-)

I apologise for ruffling your feathers, but its difficult to judge another
person's level of knowledge. In someways you're obviously quite
knowledgeable about Python, but in other ways you're still learning (as we
all are!) and I'm afraid I may have misjudged exactly what your level of
knowledge was. Sorry about that.

I'm not suggesting that you print "silly plain text strings" just for the
heck of it. You've asked how to get a clickable link using Python. There is
only one way I know of to get a clickable link using Python:

Write out a link as plain text to another application which then interprets
the plain text as a clickable link.

You *might* be able to get clickable links in Python by writing an entire
GUI application, using (say) the tkinter library, or one of the third-party
GUI libraries like wxPython, kivy, pyqt, or others, but I haven't a clue
how. But even there, your links will start off as text, which means you
will still need to surround them with quotes to make them strings.


Aside: you've actually raised a fascinating question. I wonder whether there
are any programming languages that understand URLs as native data types, so
that *source code* starting with http:// etc is understood in the same way
that source code starting with [ is seen as a list or { as a dict?


But back to your problem: short of writing you own GUI application, in which
case you can do *anything you like*, you can:

- write out a HTML file containing the URLs you want, in <a href= ... </a>
tags, then open the HTML file in a web browser and let the web browser
interpret the HTML tags as clickable links;


- write out an Excel spreadsheet, using whatever format Excel expects, open
the spreadsheet in Excel, and let Excel interpret the mystery format as a
clickable link;

- print the URL to the console, and see if the console is smart enough to
interpret it as a clickable link.


I'm sorry that I can't give you a one-sentence answer "just use
such-and-such a function, that does exactly what you want in a
platform-independent manner" :-(

Deborah Swanson

unread,
Jan 4, 2017, 6:15:25 AM1/4/17
to
Steve D'Aprano wrote, on January 04, 2017 2:09 AM
+- RE: Clickable hyperlinks
│ +- Re: RE: Clickable hyperlinks
│ L- RE: Clickable hyperlinks
│ L- Threading [was Re: Clickable hyperlinks]
│ L- Re: Threading
+- RE: Clickable hyperlinks
L- Re: Clickable hyperlinks


Adding a "Re:" or "RE" to the subject doesn't change the thread, and
neither does changing the subject line in a more substantial way.

Of course, I can always *choose* to sort by subject line, or date.
Personally I hardly ever sort by thread, so it is no skin off my nose
what you do. But when you hit "Reply" to a message, you inherit the
"Reference" and "In-Reply-To" headers from the previous message, so at
least some people will see it threaded in an existing thread rather than
starting a brand new one.




--
Steve
"Cheer up," they said, "things could be worse." So I cheered up, and
sure enough, things got worse.

Interesting history lesson. I actually wouldn't know which came first,
since I've only been online since 1992 (offline computing since 1972). I
do know that I've been on a lot of mailing lists like this one since
1992 (remember Fidonet?), some with actively posting members in the
thousands. And I have never heard anyone whine and complain about
threading issues that had anything to do with the message title until
yesterday.

So it goes. I'll repeat again that I'll comply with the rules of this
group that are brand spanking new to me, and I've been around a corner
or two.

Deborah Swanson

unread,
Jan 4, 2017, 6:25:13 AM1/4/17
to
Steve D'Aprano wrote, on January 04, 2017 2:20 AM
I can appreciate all that and pretty much knew most of it already. At
this point I'm not so much concerned with how to put characters on a
white space that are clickable, we've pretty much established at least a
couple dozen messages ago that there's nothing for python to get hold of
there because of the vast number of places people might want to put
clickable links. (I actually read every message on a thread that
interests me enough to participate.)

I think the entry point to this problem is to find out how to connect to
the internet when you click that link. Then I think the nuts and bolts
of what symbols to put where for a particular target would fall into
place.


Deborah Swanson

unread,
Jan 4, 2017, 6:42:34 AM1/4/17
to
Steve D'Aprano wrote, on January 04, 2017 2:39 AM
Well, well. People mix and people misunderstand and misjudge each other.
It's the way of things with people. I just needed to tell you how it all
looked from my side. So are we done with that? I certainly hope so.

I'm quite well aware by now that there is no one-sentence answer to my
original question, if there's any coherent answer at all. Them's the
breaks. Live with it or live without it, it doesn't care.

I do appreciate the education I've gotten on this thread about the
issues involved. But I rather imagine that near term anyway, I'll only
be pondering it now and then, and maybe poking around a bit. Who knows,
maybe I'll eventually invent the solution and make my first million
dollars from it. haha, yeah right.

So it goes.


Deborah


Chris Angelico

unread,
Jan 4, 2017, 7:16:07 AM1/4/17
to
On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
> I'm quite well aware by now that there is no one-sentence answer to my
> original question, if there's any coherent answer at all. Them's the
> breaks. Live with it or live without it, it doesn't care.

Yeah, there's no simple answer; however, you'll find that Python on
many platforms is entirely capable of popping a URL up in the user's
default browser. Check this out:

>>> import antigravity

This uses the 'webbrowser' module, which knows about a number of
different ways to open a browser, and will attempt them all. So if you
can figure out the UI part of things, actually making the link pop up
in a browser isn't too hard; for instance, if you're doing OAuth at
the command line and need the user to go and authenticate, you can
simply webbrowser.open("http://......./") and it'll DTRT.

ChrisA

D'Arcy Cain

unread,
Jan 4, 2017, 8:13:35 AM1/4/17
to
Deborah - please trim your quoted text.

On 2017-01-04 04:32 AM, Deborah Swanson wrote:
> Thanks, Steven. Yes, of course if you want to print strings you must
> enclose them in quotes. I think you learn that in Week 1 of any
> introductory course on Python.

Closer to minute one. When I investigated Python years ago the first
thing I learned was;

print "Hello, world"

> But we aren't trying to print strings here, the point is to produce
> clickable links. I didn't enclose them with quotes because I didn't see
> any point in printing plain text when I wanted clickable links. I

I'm not sure what your links are composed of but mine all look like
sequences of characters or "strings." It sounds like you are trying to
make URL a first class type like strings. integers, floats, etc. I
can't think of any language that treats URLs as first class objects.
Even HTML needs quotes:

<A HREF="http://...">Go here</A>

> actually didn't understand why you thought I should print them, but it

You want to output them to something. That often involves printing them
to a particular handler.

> never would have occurred to me that you wanted me to print out a bunch
> of silly plain text strings, apparently just for the heck of it.

Is that really what you got from his message?

> At this point, if I pursue this any farther, it will be to look into how
> Firefox takes webpage titles and urls out of its sqlite database and
> makes objects you can click on to open the webpages. That's the basic
> technology I'd need to find a way to talk (write) python into doing.

I can assure you that FF prints the string at some point. It may wrap
it in HTML tags first but printing is what it does. Also, the URLs are
stored as strings. SQLite has no URL type. If it did then it would
still store it as a string somewhere. PostGreSQL would let you create a
URL type if you wanted but you would still need to wrap it in quotes
(single in this case) when you created the entry.

> If it's even worth it. On a practical level it's not worth it, way too
> much work for the teensy advantage of having it to use. It might be some
> giggles to figure out how to do it and maybe I will sometime just for
> funsies.

In all the messages in this thread I still don't understand what this
"teensy advantage" is supposed to be. Do you want to be able to do this:

make_web_link(http://...)

instead of:

make_web_link("http://...")

--
D'Arcy J.M. Cain
System Administrator, Vex.Net
http://www.Vex.Net/ IM:da...@Vex.Net
VoIP: sip:da...@Vex.Net

Rodrigo Bistolfi

unread,
Jan 4, 2017, 8:45:01 AM1/4/17
to
2017-01-04 7:39 GMT-03:00 Steve D'Aprano <steve+...@pearwood.info>:

> On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote:
>
> Aside: you've actually raised a fascinating question. I wonder whether
> there
> are any programming languages that understand URLs as native data types, so
> that *source code* starting with http:// etc is understood in the same way
> that source code starting with [ is seen as a list or { as a dict?
> ...
>

Some Smalltalk implementations have something that comes close:

st> 'https://python.org' asUrl retrieveContents

`asUrl` would be a string method returning a URL instance, which also has a
convenient method `retrieveContents` wrapping an http client. Not hard to
do with Python, I think this could be an interesting exercise for a learner.

D'Arcy Cain

unread,
Jan 4, 2017, 8:58:30 AM1/4/17
to
On 2017-01-04 08:44 AM, Rodrigo Bistolfi wrote:
> 2017-01-04 7:39 GMT-03:00 Steve D'Aprano <steve+...@pearwood.info>:
>> Aside: you've actually raised a fascinating question. I wonder whether
>> there
>> are any programming languages that understand URLs as native data types, so
>> that *source code* starting with http:// etc is understood in the same way
>> that source code starting with [ is seen as a list or { as a dict?
>
> Some Smalltalk implementations have something that comes close:
>
> st> 'https://python.org' asUrl retrieveContents

But notice that even here the URL has to be defined as a string. To be
a first class object you would need to do something like this:

url = https://python.org/

The only time you would see that is in config files and languages like
shell where everything is a string so no quotes necessary. They are
implied.

> `asUrl` would be a string method returning a URL instance, which also has a
> convenient method `retrieveContents` wrapping an http client. Not hard to
> do with Python, I think this could be an interesting exercise for a learner.

Sure but the issue is, what to do with it. In any case, it's still just
a wrapper around various string methods. You still need to give the
class a string to create the object.

Grant Edwards

unread,
Jan 4, 2017, 10:36:11 AM1/4/17
to
On 2017-01-03, Deborah Swanson <pyt...@deborahswanson.net> wrote:
> Grant Edwards wrote, on January 03, 2017 3:13 PM
>>
>> On 2017-01-03, Deborah Swanson <pyt...@deborahswanson.net> wrote:
>>
>> > I'm sorry, I should have said a GUI console because I
>> wouldn't expect
>> > a text-based console to produce clickable links.
>>
>> What's a "GUI console"?

> The GUI consoles I have are in Pycharm, the IDLE that comes with
> Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when
> I open them, so they're capable of opening links, but whether that means
> their output space is capable of handling clickable links I don't know.

Thanks, that's a bit clearer. For those of us from the Unix world
"console" means something else.

> I do know printing a full url with the %s specifier or entering a url
> and clicking enter just gives you the plain text url. Obviously, not all
> GUI consoles are enabled recognize and make clickable links from
> correctly formatted urls.
>
> I was hoping there was some functionality in python to make clickable
> links. Could be a package, if the core language doesn't have it.

There is no definition for what a "clickable link" is unless you're
sending HTML to a web browser. That means there's no way to create
funcationality to make one.

--
Grant Edwards grant.b.edwards Yow! I'm not an Iranian!!
at I voted for Dianne
gmail.com Feinstein!!

Grant Edwards

unread,
Jan 4, 2017, 10:43:03 AM1/4/17
to
On 2017-01-04, Michael Torrie <tor...@gmail.com> wrote:

> On my Linux machine, the terminal emulators I've used all make a regular
> url printed out into a clickable link (or at least a right-clickable
> link). This is just something they try to do with all things that look
> like urls. Sometimes it's helpful, often it's annoying.

What I have done is defined a window manager root menu entry that
opens a web browser on the current text selection. That lets me
"click" on a link in any application that suport the standard X11
text-selection mechanism (which is almost all of them).

>> I was hoping there was some functionality in python to make clickable
>> links. Could be a package, if the core language doesn't have it.
>
> No, there is not. If you made a full GUI app using a toolkit like
> GTK or Qt, you can indeed place hyperlinks on your forms and the
> operating system will automatically connect them to the web browser.
> But not in text-mode terminal apps.

For me it's double-click to select the url in the terminal window or
PDF viewer or whaterver, then right-click on the root window and pick
the menu entry that says 'Firefox [sel]' or 'Chrome [sel]'. It's a
few extra steps, but it works for almostly any application that
displays text.

--
Grant Edwards grant.b.edwards Yow! Where do your SOCKS
at go when you lose them in
gmail.com th' WASHER?

Deborah Swanson

unread,
Jan 4, 2017, 5:59:06 PM1/4/17
to
Chris Angelico wrote, on January 04, 2017 4:16 AM
>
> On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson
> <pyt...@deborahswanson.net> wrote:
> > I'm quite well aware by now that there is no one-sentence
> answer to my
> > original question, if there's any coherent answer at all.
> Them's the
> > breaks. Live with it or live without it, it doesn't care.
>
> Yeah, there's no simple answer; however, you'll find that
> Python on many platforms is entirely capable of popping a URL
> up in the user's default browser. Check this out:
>
> >>> import antigravity
>
> This uses the 'webbrowser' module, which knows about a number
> of different ways to open a browser, and will attempt them
> all. So if you can figure out the UI part of things, actually
> making the link pop up in a browser isn't too hard; for
> instance, if you're doing OAuth at the command line and need
> the user to go and authenticate, you can simply
> webbrowser.open("http://......./") and it'll DTRT.
>

Thank you, thank you! Finally, at least one person on this list knows
about something (anything) in the python world that is internet aware.
It's also occurred to me that Beautifulsoup downloads data from a url,
so that code must have access to some kind of an internet engine too.

I googled antigravity and found a number of interesting links.

The History of Python: import antigravity
http://python-history.blogspot.com/2010/06/import-antigravity.html

Among other things, it was added to Python 3 in 2010, so it's been
around a little while. And a comment mentions that "The antigravity
module is also included in Python 2.7."

And a reddit poster tells us that "if you type 'import antigravity' into
a Python command line your default browser opens the XKCD comic 'Python'
in a tab."
https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type
_import_antigravity_into_a_python/

An "import antigravity" video at
https://www.youtube.com/watch?v=_V0V6Rk6Fp4

And its page in the Package Index:
https://pypi.python.org/pypi/antigravity/0.1, with a Page Not Found
Error for the Home Page. So it doesn't look like there's any alternative
but to download it and look at the code.

Yes, I'd gotten as far as figuring out that you don't need a clickable
link. Code that opens a url in a browse would do the job just fine. Or
the webbrowser.open("http://......./") in a Linux terminal you suggest.
(I just have to get my Linux machine up and running again to try it.)

All in all, given that clickable urls in a console is a non-starter,
this hits the nail on the head. Many thanks again!

Deborah




Chris Angelico

unread,
Jan 4, 2017, 6:49:35 PM1/4/17
to
On Thu, Jan 5, 2017 at 9:58 AM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
> Chris Angelico wrote, on January 04, 2017 4:16 AM
>> This uses the 'webbrowser' module, which knows about a number
>> of different ways to open a browser, and will attempt them
>> all. So if you can figure out the UI part of things, actually
>> making the link pop up in a browser isn't too hard; for
>> instance, if you're doing OAuth at the command line and need
>> the user to go and authenticate, you can simply
>> webbrowser.open("http://......./") and it'll DTRT.
>>
>
> Thank you, thank you! Finally, at least one person on this list knows
> about something (anything) in the python world that is internet aware.
> It's also occurred to me that Beautifulsoup downloads data from a url,
> so that code must have access to some kind of an internet engine too.

We've been all talking at cross purposes a bit in this thread. Most of
us thought you were talking about the *user interface* of a clickable
link, but if you're talking about the *mechanics* of HTTP downloads,
Python has excellent facilities. I'd recommend checking out the
third-party 'requests' module on PyPI.

> I googled antigravity and found a number of interesting links.
>
> The History of Python: import antigravity
> http://python-history.blogspot.com/2010/06/import-antigravity.html
>
> Among other things, it was added to Python 3 in 2010, so it's been
> around a little while. And a comment mentions that "The antigravity
> module is also included in Python 2.7."
>
> And a reddit poster tells us that "if you type 'import antigravity' into
> a Python command line your default browser opens the XKCD comic 'Python'
> in a tab."
> https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type
> _import_antigravity_into_a_python/
>
> An "import antigravity" video at
> https://www.youtube.com/watch?v=_V0V6Rk6Fp4

Hehe, yeah. It's a big joke that started because XKCD mentioned the
language. But actually, the source code for antigravity.py itself
isn't significant; all it does is call on the webbrowser module:

https://docs.python.org/3/library/webbrowser.html

> Yes, I'd gotten as far as figuring out that you don't need a clickable
> link. Code that opens a url in a browse would do the job just fine. Or
> the webbrowser.open("http://......./") in a Linux terminal you suggest.
> (I just have to get my Linux machine up and running again to try it.)
>
> All in all, given that clickable urls in a console is a non-starter,
> this hits the nail on the head. Many thanks again!

Cool! Glad I could help out a bit. There are a few different things
you can consider:

1) Open up a browser tab and let the user see the result
2) Make the request programmatically and access the text of the page
for further processing
3) Invoke a hidden web browser, browse to a URL, submit form data,
etc, as a means of testing a web server.

All three are possible. Take your pick!

ChrisA

Terry Reedy

unread,
Jan 4, 2017, 6:58:18 PM1/4/17
to
On 1/4/2017 4:32 AM, Deborah Swanson wrote:

> My original question was whether python had anything to provide this
> functionality, and the answer appears to be a resounding NO!!!

I would say 'Yes, but with user effort'.

To have a string interpreted as a clickable link, you send the string to
software capable of creating a clickable link, plus the information
'this is a clickable link'*. There are two ways to tag a string as a
link. One is to use markup around the url in the string itself.
'<url>' and html are example. Python provides multiple to make this
easy. The other is to tag the string with a separate argument. Python
provides tkinter, which wraps tk Text widgets, which have a powerful tag
system. One can define a Link tag that will a) cause text to be
displayed, for instance, blue and underlined and b) cause clicks on the
text to generate a web request. One could then use
mytext.insert('insert', 'http://www.example.com', Link)
Browser must do something similar when they encounter when they
encounter html link tags.

* If the software directly recognizes a bare url such as
'http://www.example.com' as a link, without further indication, then it
should have a way to disable conversion to a clickable link.

--
Terry Jan Reedy

Deborah Swanson

unread,
Jan 4, 2017, 7:07:27 PM1/4/17
to
D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM
>
> Deborah - please trim your quoted text.

Yes, I will. Some lists want to have it all to review in one message,
some want it trimmed to just the lines you are responding to. I was just
waiting to see what this list wants.

> On 2017-01-04 04:32 AM, Deborah Swanson wrote:
<snip>

> > But we aren't trying to print strings here, the point is to produce
> > clickable links. I didn't enclose them with quotes because I didn't
> > see any point in printing plain text when I wanted
> clickable links. I
>
> I'm not sure what your links are composed of but mine all look like
> sequences of characters or "strings." It sounds like you are
> trying to
> make URL a first class type like strings. integers, floats, etc. I
> can't think of any language that treats URLs as first class objects.
> Even HTML needs quotes:
>
> <A HREF="http://...">Go here</A>

It seemed reasonable that you might be able to print urls, which is why
I tried the experiment with all of Steven's suggested formats. But I was
highly skeptical that any would work without some kind of modifiers to a
bare print statement.

> > actually didn't understand why you thought I should print
> them, but it
>
> You want to output them to something. That often involves
> printing them
> to a particular handler.

Yes, that's one of the things I would expect if we could print them.

> > never would have occurred to me that you wanted me to print out a
> > bunch of silly plain text strings, apparently just for the
> heck of it.
>
> Is that really what you got from his message?

Please forgive me, and I hope Steven forgives me too, but I was sick to
death of all the beating on a dead horse (using Python to make clickable
links in a console, any console). I'd taken heart when he first
suggested his print experiment, because it was a plausible approach. But
I lost my temper when he upbraided me in this message for failing to
enclose my strings in quotes, in a most patronizing kind of way, when
printing out plain text was absolutely nowhere on the progress toward a
solution scale. I've been quite impressed with Steven's knowledge and
talent, and after fending off the throng of unseeing naysayers all
afternoon, it was just a little too much. I really should have closed my
email reader hours before I read and replied to this message. Shoulda,
coulda, woulda.

<snip>

> I can assure you that FF prints the string at some point. It
> may wrap
> it in HTML tags first but printing is what it does. Also,
> the URLs are
> stored as strings. SQLite has no URL type. If it did then it would
> still store it as a string somewhere. PostGreSQL would let
> you create a
> URL type if you wanted but you would still need to wrap it in quotes
> (single in this case) when you created the entry.

I have no doubt that some variant of printing is involved. Transporting
urls to the internet is an output process. FF's sqlite implementation
does store urls as a text field in at least 2 tables. I would be
interested in how FF takes those text urls and opens web pages with
them, although I've learned and figured out just today some ways that
Python can also do it. Turns out clickable links were a red herring.

If Steven's original suggestion included anything but a bare print
statement, like the use of a special specifier or linking the print
statement to some module, the use of quoted strings would have at least
been worthy of consideration. But we all know what
print("http://www.wherever.com") would print, and it would be utterly
worthless for the purpose at hand. Trying the print statement without
the quotes was a least a possibility, if there was any awareness in the
print code of urls and what to do with them. That was the whole point of
this fishing expedition, as I saw it. To see if there was any
undocumented or narrowly known-of features in the print code.

<snip>
> In all the messages in this thread I still don't understand what this
> "teensy advantage" is supposed to be. Do you want to be able
> to do this:
>
> make_web_link(http://...)
>
> instead of:
>
> make_web_link("http://...")
>
> --
> D'Arcy J.M. Cain
> System Administrator, Vex.Net
> http://www.Vex.Net/ IM:da...@Vex.Net
> VoIP: sip:da...@Vex.Net

You probably didn't see my oneliner on the "why do it" part in the swarm
of messages on this thread yesterday. In it I mentioned that the use
would be to open urls in the data I'm working with while I'm debugging
the code that uses them. I want to see what pages they open, without
having to leave my IDE. (Obviously I'd have to open another .py file,
but that would be easier and quicker than the alternatives.) I never
intended my original question to be any more than a frivolous toss out
into the sea, to see if anyone knew an answer. I was flat out astonished
when it blew up into the mini-monster that it did.

Is make_web_link("http://...") valid python code? That's exactly the
kind of answer I was looking for, and I will try it (or look it up if it
needs something imported) as soon as I send this off. Thank you.

It's possible you caught just the tail end of a hot mess without seeing
all the irrational vitriol and nonsense that led up to this message.
Lucky you.

Deborah

Michael Torrie

unread,
Jan 4, 2017, 7:47:55 PM1/4/17
to
On 01/04/2017 03:58 PM, Deborah Swanson wrote:
> Thank you, thank you! Finally, at least one person on this list knows
> about something (anything) in the python world that is internet aware.
> It's also occurred to me that Beautifulsoup downloads data from a url,
> so that code must have access to some kind of an internet engine too.

Except that you never mentioned anything about this in your posts
before. It seemed to me you were asking about printing out clickable
hyperlinks with python. Calling the OS to launch a browser to view a
url is a different beast which is why no one mentioned it before.

If you don't tell us what you're actually trying to do (your end goal),
things are more frustrating for everyone. If you had said early on you
just want to be able to send the user to a particular url in a web
browser, what Chris suggested would have been said a long time ago,
rather than focusing on console output which is what you were asking
about and focusing attention on.

Just so you know, BeautifulSoup does not do any internet access itself;
it's only an HTML parser. You have to fetch web pages using something
like python's urllib and then feed the data to BeautifulSoup. urllib is
not a web browser though. It can pretend to be a browser as far as the
server is concerned but it knows nothing about javascript or rendering.
It just retrieves bytes which you can then feed to BeautifulSoup or some
other parser.

> Yes, I'd gotten as far as figuring out that you don't need a clickable
> link. Code that opens a url in a browse would do the job just fine.

Good! I just wish you would have mentioned this much earlier as your end
goal.

> Or the webbrowser.open("http://......./") in a Linux terminal you suggest.
> (I just have to get my Linux machine up and running again to try it.)

The webbrowser module does not require console or terminal output. It
will work on Windows or Linux if I'm not mistaken. It asks the OS to
launch the default browser and load the indicated url.

D'Arcy Cain

unread,
Jan 4, 2017, 10:00:21 PM1/4/17
to
On 2017-01-04 05:58 PM, Deborah Swanson wrote:
>> the user to go and authenticate, you can simply
>> webbrowser.open("http://......./") and it'll DTRT.
>
> Thank you, thank you! Finally, at least one person on this list knows
> about something (anything) in the python world that is internet aware.

Lots of things in Python are Internet aware. That's not the question
you asked though.

> It's also occurred to me that Beautifulsoup downloads data from a url,
> so that code must have access to some kind of an internet engine too.

Nope. You have to feed it HTML. You either need to generate that or
capture it from somewhere.

Rustom Mody

unread,
Jan 4, 2017, 10:22:40 PM1/4/17
to
This thread does lead to the question:
Is the Url type in python less first-class than it could be?

In scheme I could point to something like this
https://docs.racket-lang.org/net/url.html

Is there something equivalent in python?

D'Arcy Cain

unread,
Jan 4, 2017, 10:24:51 PM1/4/17
to
On 2017-01-04 07:07 PM, Deborah Swanson wrote:
> D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM
>> In all the messages in this thread I still don't understand what this
>> "teensy advantage" is supposed to be. Do you want to be able
>> to do this:
>>
>> make_web_link(http://...)
>>
>> instead of:
>>
>> make_web_link("http://...")

[...]

> Is make_web_link("http://...") valid python code? That's exactly the

It's just a made up name. My point was that the first was syntactically
incorrect and the second, while not a real method, was at least parseable.

I think I saw someone else mention this but it bears repeating. When
you ask a question make sure you are presenting the problem and not your
solution to a secret problem. Always tell us the actual problem you are
trying to solve. You will get much better answers.

Think of it this way. You drop a ring down a drain. You can ask two
questions, "How do I remove a drain trap?" or "How do I recover a ring
that I dropped down the drain?" If you ask the first question you will
get lots of advice on tools and buckets, etc. People will assume that
the drain is blocked. Ask the second question and someone might mention
a magnet and a piece of string.

Chris Angelico

unread,
Jan 4, 2017, 11:08:17 PM1/4/17
to
On Thu, Jan 5, 2017 at 2:24 PM, D'Arcy Cain <da...@vex.net> wrote:
> Think of it this way. You drop a ring down a drain. You can ask two
> questions, "How do I remove a drain trap?" or "How do I recover a ring that
> I dropped down the drain?" If you ask the first question you will get lots
> of advice on tools and buckets, etc. People will assume that the drain is
> blocked. Ask the second question and someone might mention a magnet and a
> piece of string.

... and then you follow up with "it's a gold ring, magnet won't touch
it", and we'll go off on a tangent about whether the ring is
sufficiently plain that it might be the One Ring, and shouldn't you
destroy it instead of retrieving it.... because that's what we do here
:D

ChrisA

Deborah Swanson

unread,
Jan 4, 2017, 11:19:52 PM1/4/17
to
Chris Angelico wrote, on January 04, 2017 4:16 AM
>
> Yeah, there's no simple answer; however, you'll find that
> Python on many platforms is entirely capable of popping a URL
> up in the user's default browser. Check this out:
>
> >>> import antigravity

I downloaded the code from the Package Index, but there really wasn't
much in it. This is the entire .py file:

STRIP_URL = "http://xkcd.com/353/"

def start():
return STRIP_URL

And setup.py is equally disappointing:
from distutils.core import setup

setup(
name='antigravity',
version='0.1',
description='A really simple module that allow everyone to do
"import antigravity"',
author='Fabien Schwob',
author_email='antig...@x-phuture.com',
url='http://fabien.schwob.org/antigravity/',
packages=['antigravity'],
)

> This uses the 'webbrowser' module, which knows about a number
> of different ways to open a browser, and will attempt them
> all. So if you can figure out the UI part of things, actually
> making the link pop up in a browser isn't too hard; for
> instance, if you're doing OAuth at the command line and need
> the user to go and authenticate, you can simply
> webbrowser.open("http://......./") and it'll DTRT.
>
> ChrisA

All the action of antigravity must be done by the import statement. When
import opens a module that immediately returns a url, it must have a
mechanism to open it in a browser.

It would be very easy to do the same thing with my own .py and import it
into another .py.

Or, take a look at import's code and figure out how it opens a url in a
browser. I imagine it's the 'webbrowser' module you mention. If it tries
several methods, just pick one that will work for you.

Or, take a look at this Index of Packages Matching 'webbrowser' (~50
packages)
https://pypi.python.org/pypi?%3Aaction=search&term=webbrowser&submit=sea
rch

D'Arcy was right, there's lots in python that's internet aware, though
that wasn't the question I knew to ask.

Chris Angelico

unread,
Jan 4, 2017, 11:27:32 PM1/4/17
to
On Thu, Jan 5, 2017 at 3:19 PM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
> I downloaded the code from the Package Index, but there really wasn't
> much in it. This is the entire .py file:

Ehh, wrong file. Try the one in the standard library:

https://github.com/python/cpython/blob/master/Lib/antigravity.py
https://github.com/python/cpython/blob/master/Lib/webbrowser.py

Or you can look in your installed Python - "import webbrowser;
print(webbrowser.__file__)" will tell you where.

ChrisA

Michael Torrie

unread,
Jan 4, 2017, 11:41:46 PM1/4/17
to
On 01/04/2017 09:19 PM, Deborah Swanson wrote:
> Or, take a look at import's code and figure out how it opens a url in a
> browser. I imagine it's the 'webbrowser' module you mention. If it tries
> several methods, just pick one that will work for you.

webbrowser is part of the python standard library:
https://docs.python.org/3/library/webbrowser.html

It uses whatever method is appropriate for the platform for opening a
url in the user's default browser.

> Or, take a look at this Index of Packages Matching 'webbrowser' (~50
> packages)
> https://pypi.python.org/pypi?%3Aaction=search&term=webbrowser&submit=sea
> rch

There may be modules in there that will be useful, but the first port of
call should always be the python standard library.

> D'Arcy was right, there's lots in python that's internet aware, though
> that wasn't the question I knew to ask.

That's why it's always good to state your end goal when asking about things.




Deborah Swanson

unread,
Jan 4, 2017, 11:52:46 PM1/4/17
to
Chris Angelico wrote, on January 04, 2017 3:49 PM
>
> On Thu, Jan 5, 2017 at 9:58 AM, Deborah Swanson
> <pyt...@deborahswanson.net> wrote:
> >
> > Thank you, thank you! Finally, at least one person on this list
knows
> > about something (anything) in the python world that is internet
aware.
<snip>
>
> We've been all talking at cross purposes a bit in this
> thread. Most of us thought you were talking about the *user
> interface* of a clickable link, but if you're talking about
> the *mechanics* of HTTP downloads, Python has excellent
> facilities. I'd recommend checking out the third-party
> 'requests' module on PyPI.

My original question was in fact whether there was a way to make
clickable hyperlinks in a console. I was persuaded after about 10
replies that the answer was no, and I tried with little success to
change the question to one of directly opening a url in a browser from
Python. (In hindsight, maybe I should have started a new thread.) Turns
out this version of the question does have good answers, but the revised
question got totally drowned out in the scramble to show that clickable
links in a console are a no-go. I'm not surprised that you didn't see
the shift in what I was looking for.

<snip>
>
> Hehe, yeah. It's a big joke that started because XKCD
> mentioned the language. But actually, the source code for
> antigravity.py itself isn't significant; all it does is call
> on the webbrowser module:
>
https://docs.python.org/3/library/webbrowser.html

I saw how insignificant antigravity's code is when I downloaded it from
the Package Index, and I copied the current version into another message
of yours that I replied to before this one. The original antigravity may
have used webbrowser, but now it just returns a url and hands it off to
import to open it. Maybe the author didn't know import had that
capability until after antigravity had been in the wild awhile.
Regardless, antigravity does point the way to pythonic web access.

> Yes, I'd gotten as far as figuring out that you don't need a clickable

> link. Code that opens a url in a browser would do the job just fine.
Or
> the webbrowser.open("http://......./") in a Linux terminal you
> suggest. (I just have to get my Linux machine up and running again to
> try it.)
>
> All in all, given that clickable urls in a console is a non-starter,
> this hits the nail on the head. Many thanks again!

Cool! Glad I could help out a bit. There are a few different things you
can consider:

1) Open up a browser tab and let the user see the result
2) Make the request programmatically and access the text of the page for
further processing
3) Invoke a hidden web browser, browse to a URL, submit form data, etc,
as a means of testing a web server.

All three are possible. Take your pick!

ChrisA

I have a fourth option. I use Firefox with profiles and I could make one
specifically for this task, and have the code open the page in that
profile. This would give it it's own window, history, bookmarks, and
sqlite database, which could be handy if a particular url continues to
be a problem, or I want to track it for some reason. In this project I
usually would want to look at the photos, so a text download wouldn't be
that helpful. But it could be in some other project, and I expect to be
doing quite a bit of web work down the road.

Deborah Swanson

unread,
Jan 4, 2017, 11:56:09 PM1/4/17
to
Chris Angelico wrote, on January 04, 2017 8:27 PM
print>(webbrowser.__file__)" will tell you where.
>
>ChrisA

Great! I will check it out. Thanks again!

Deborah Swanson

unread,
Jan 5, 2017, 12:11:41 AM1/5/17
to
Terry Reedy wrote, on January 04, 2017 3:58 PM
>
> On 1/4/2017 4:32 AM, Deborah Swanson wrote:
>
> > My original question was whether python had anything to provide this

> > functionality, and the answer appears to be a resounding NO!!!
>
> I would say 'Yes, but with user effort'.
>
> To have a string interpreted as a clickable link, you send the string
to
> software capable of creating a clickable link, plus the information
> 'this is a clickable link'*. There are two ways to tag a string as a
> link. One is to use markup around the url in the string itself.
> '<url>' and html are example. Python provides multiple to make this
> easy. The other is to tag the string with a separate argument. Python

> provides tkinter, which wraps tk Text widgets, which have a powerful
tag
> system. One can define a Link tag that will a) cause text to be
> displayed, for instance, blue and underlined and b) cause clicks on
the
> text to generate a web request. One could then use
> mytext.insert('insert', 'http://www.example.com', Link)
> Browser must do something similar when they encounter when they
> encounter html link tags.

I've actually moved on from my original question to one of opening a url
in a browser with python, which seems to be a much more easily achieved
goal.

But someone else mentioned tkinter, and I looked at it while ago but
haven't used it for anything. That really could be the way to go if you
want to make clickable links, although you still need some kind of
internet engine to open the url in a browser. PyCharm has clickable
local links in its console output, but they're not internet enabled,
they just jump to the relevant line of code.

You say, "There are two ways to tag a string as a link. One is to use
markup around the url in the string itself. '<url>' and html are
examples. Python provides multiple ways to make this easy."

Can you tell me where I'd begin to look for these? Are they in the core
language, or in packages?

> * If the software directly recognizes a bare url such as
> 'http://www.example.com' as a link, without further
> indication, then it
> should have a way to disable conversion to a clickable link.

One would think so. Thanks for all your info!

>
> --
> Terry Jan Reedy


Steven D'Aprano

unread,
Jan 5, 2017, 12:31:00 AM1/5/17
to
On Thursday 05 January 2017 14:22, Rustom Mody wrote:

> This thread does lead to the question:
> Is the Url type in python less first-class than it could be?
>
> In scheme I could point to something like this
> https://docs.racket-lang.org/net/url.html

Those docs say:

"To access the text of a document from the web, first obtain its URL
AS A STRING..." [emphasis added]

which means that URLs are not a first-class data type in Racket at all. URLs in
Racket are just strings, exactly the same as in Python.

There is a url struct:

https://docs.racket-lang.org/net/url.html#%28def._%28%28lib._net%2Furl-
structs..rkt%29._url%29%29

but there no first-class syntactic support for them, as ints and lists have in
Python:

123 # rather than int("123")
[1, 2] # rather than list(1, 2)


> Is there something equivalent in python?

Just like Racket, URLs in Python are not first-class. They start as a string,
and then you parse them into a tuple:

https://docs.python.org/3/library/urllib.parse.html

https://docs.python.org/2/library/urlparse.html


--
Steven
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." - Jon Ronson

Terry Reedy

unread,
Jan 5, 2017, 1:18:17 AM1/5/17
to
On 1/5/2017 12:11 AM, Deborah Swanson wrote:
> Terry Reedy wrote, on January 04, 2017 3:58 PM

>> To have a string interpreted as a clickable link, you send the string
> to
>> software capable of creating a clickable link, plus the information
>> 'this is a clickable link'*. There are two ways to tag a string as a
>> link. One is to use markup around the url in the string itself.
>> '<url>' and html are example. Python provides multiple to make this
>> easy. The other is to tag the string with a separate argument. Python
>
>> provides tkinter, which wraps tk Text widgets, which have a powerful
> tag
>> system. One can define a Link tag that will a) cause text to be
>> displayed, for instance, blue and underlined and b) cause clicks on
> the
>> text to generate a web request. One could then use
>> mytext.insert('insert', 'http://www.example.com', Link)
>> Browser must do something similar when they encounter when they
>> encounter html link tags.
>
> I've actually moved on from my original question to one of opening a url
> in a browser with python, which seems to be a much more easily achieved
> goal.

> But someone else mentioned tkinter, and I looked at it while ago but
> haven't used it for anything. That really could be the way to go if you
> want to make clickable links, although you still need some kind of
> internet engine to open the url in a browser.

IDLE allows a user to add help menu entries that, when clicked on, open
either a local file or an internet url. For instance, adding the pair
'Pillow' and "https://pillow.readthedocs.io/en/latest/" in the Settings
dialog adda "Pillow" to the help menu (after the standard stuff).
Clicking on Help => Pillow opens
"https://pillow.readthedocs.io/en/latest/" in the default browswer.
IDLE just used the webbrowser module to do this. No use re-inventing
the wheel. If instead "Pillow" were a link in text, the click handler
should do something similar.

> You say, "There are two ways to tag a string as a link. One is to use
> markup around the url in the string itself. '<url>' and html are
> examples. Python provides multiple ways to make this easy."
>
> Can you tell me where I'd begin to look for these? Are they in the core
> language, or in packages?

I was referring to using either % or .format string formatting. Both
are in the core and described somewhere in the Library manual. '%'
should be in the Symbols page of the Index and 'format' on the 'F' page.

--
Terry Jan Reedy

Rhodri James

unread,
Jan 5, 2017, 7:05:07 AM1/5/17
to
On 05/01/17 04:52, Deborah Swanson wrote:
> My original question was in fact whether there was a way to make
> clickable hyperlinks in a console. I was persuaded after about 10
> replies that the answer was no,

Then you were persuaded wrong; the actual answer was "this isn't a
meaningful question since it's based on incorrect assumptions."
Translating that to "No" is just as much a mistake as translating it to
"Yes."

--
Rhodri James *-* Kynesim Ltd

Deborah Swanson

unread,
Jan 5, 2017, 5:29:19 PM1/5/17
to
Terry Reedy wrote, on January 04, 2017 10:18 PM
>
> On 1/5/2017 12:11 AM, Deborah Swanson wrote:
> > Terry Reedy wrote, on January 04, 2017 3:58 PM
>
> >> To have a string interpreted as a clickable link, you send the
string to
> >> software capable of creating a clickable link, plus the information

> >> 'this is a clickable link'*. There are two ways to tag a string as
a
> >> link. One is to use markup around the url in the string itself.
> >> '<url>' and html are example. Python provides multiple to make
this
> >> easy. The other is to tag the string with a separate argument.
> >> Python provides tkinter, which wraps tk Text widgets, which have a
> >> powerful tag system. One can define a Link tag that will a) cause
text
> >> to be displayed, for instance, blue and underlined and b) cause
clicks on
> >> the text to generate a web request. One could then use
> >> mytext.insert('insert', 'http://www.example.com', Link) Browser
> >> must do something similar when they encounter when they encounter
> >> html link tags.
> >
> > I've actually moved on from my original question to one of opening a

> > url in a browser with python, which seems to be a much more easily
> > achieved goal.
>
> > But someone else mentioned tkinter, and I looked at it awhile ago
but
> > haven't used it for anything. That really could be the way to go if
> > you want to make clickable links, although you still need some kind
of
> > internet engine to open the url in a browser.
>
> IDLE allows a user to add help menu entries that, when clicked on,
open
> either a local file or an internet url. For instance, adding the pair

> 'Pillow' and "https://pillow.readthedocs.io/en/latest/" in > the
Settings
> dialog adda "Pillow" to the help menu (after the standard stuff).
> Clicking on Help => Pillow opens
> "https://pillow.readthedocs.io/en/latest/" in the default browswer.
> IDLE just used the webbrowser module to do this. No use re-inventing
> the wheel. If instead "Pillow" were a link in text, the click handler

> should do something similar.

Yes, unless someone suggests something better, the webbrowser module
looks like the way to go for opening urls in a browser.
>
> > You say, "There are two ways to tag a string as a link. One is to
use
> > markup around the url in the string itself. '<url>' and html are
> > examples. Python provides multiple ways to make this easy."
> >
> > Can you tell me where I'd begin to look for these? Are they in the
> > core language, or in packages?
>
> I was referring to using either % or .format string formatting. Both
> are in the core and described somewhere in the Library manual. '%'
> should be in the Symbols page of the Index and 'format' on
> the 'F' page.
>
> --
> Terry Jan Reedy

I looked up % in the Symbols page, but I didn't see any specifier
related to urls. It would be nice if there was something like a %u for
url format, but it isn't in there.

I also tried

print(<url>"http//python.org"</url>)
^
but got 'SyntaxError: invalid syntax', with the red arrow pointing at
the first angle bracket. I also tried

print(<html>"http//python.org"</html>)
^
and got the same syntax error, but I'm not sure if that's how you meant
html should be used.

I also tried to look up 'format', but there's no such entry in the
Index. There are a lot of entries that begin with 'format', but none of
them mention urls or anything link related. 'format_field()
(string.Formatter method)' looked like a possibility, but again, I
didn't see anything to format a link with. Maybe I didn't look hard
enough, or didn't see something that _would_ work.

As I've said, at this point I've moved on to directly opening a url in a
browser with the webbrowser module. All I originally wanted to do was be
able to open a url without leaving my IDE while I was debugging data
that has urls in it. Clickable links are really just eye candy that I
don't need if I can get the same result programmatically. But I was
curious whether there are any ways to tag a string as a link in a print
statement. If there are, I didn't find any, but thanks for your
suggestions!

Deborah

Deborah Swanson

unread,
Jan 5, 2017, 5:53:22 PM1/5/17
to
Rhodri James wrote, on January 05, 2017 3:53 AM
Actually, your statement "this isn't a meaningful question since it's
based on incorrect assumptions" is false. PyCharm outputs clickable
links to the console, but they aren't web links, they simply link to
lines of code. I'd seen that PyCharm can make links in the console that
aren't web enabled, so it seemed, and in fact it is, reasonable to
assume that it could be done for urls. I just wanted to know if anyone
knew how to do it.

Granted, the suggestion to use tkinter to enable the links came up much
later than in the first 10 or so replies, and since tkinter makes
clickable links possible, that's another reason my question wasn't based
on false assumptions. It simply appears that the early responders to my
question went off on a tangent of what is or is not technologically
possible, and all of the approaches under consideration were in fact
dead ends.

But clickable links turns out to be just eye candy, and the real result
I wanted, which is opening urls in a browser from my IDE, is much more
quickly and easily done programmatically. Although I didn't see this
before I asked my question, and only saw it after reading quite a few
replies.

Perhaps though, I should have said "I was persuaded after about 10
replies that that no one understood what I was asking." But that just
seemed plain rude, so I went with "the answer was no".

Steve D'Aprano

unread,
Jan 6, 2017, 1:15:19 AM1/6/17
to
On Wed, 4 Jan 2017 10:32 am, Deborah Swanson wrote:


> The GUI consoles I have are in Pycharm, the IDLE that comes with
> Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when
> I open them, so they're capable of opening links, but whether that means
> their output space is capable of handling clickable links I don't know.
>
> I do know printing a full url with the %s specifier or entering a url
> and clicking enter just gives you the plain text url. Obviously, not all
> GUI consoles are enabled recognize and make clickable links from
> correctly formatted urls.
>
> I was hoping there was some functionality in python to make clickable
> links. Could be a package, if the core language doesn't have it.

Unfortunately there is no such thing as an application- independent "clickable
link".

Excel can make clickable links, because it only has to deal with a single suite
of related applications: Excel, Word, and the rest of Microsoft Office.
Likewise LibreOffice.

But Python has to deal with an infinite number of potential or hypothetical
consoles, and there is no standard for "clickable links" that all, or even
most, consoles understand.

Python can't force the console to treat something as a clickable link, if the
console has no capacity for clickable links. Nor can Python predict what format
the console uses to recognise a link.

The best you can do is to experiment with a couple of simple formats and see
which, if any, your console understands:

# HTML
<a href="http://www.example.com">Example.</a>

# URL in angle brackets
Example <http://www.example.com>

# URL alone
http://www.example.com

# I can't remember what these are called
<url:http://www.example.com>

# Markup
[Example](http://www.example.com)

# Rest
`Example <http://www.example.com>`_





--
Steve
â ŁCheer up,â Ř they said, â Łthings could be worse.â Ř So I cheered up, and
sure
enough, things got worse.

Deborah Swanson

unread,
Jan 6, 2017, 1:15:19 AM1/6/17
to
Grant Edwards wrote, on January 03, 2017 3:13 PM
>
> On 2017-01-03, Deborah Swanson <pyt...@deborahswanson.net> wrote:
>
> > I'm sorry, I should have said a GUI console because I
> wouldn't expect
> > a text-based console to produce clickable links.
>
> What's a "GUI console"?
>
> --
> Grant Edwards grant.b.edwards Yow! I
> want you to MEMORIZE
> at the
> collected poems of
> gmail.com EDNA ST
> VINCENT MILLAY

Dan Strohl

unread,
Jan 6, 2017, 1:15:20 AM1/6/17
to
Keeping mind how this all works...

Python is providing the data, the console/terminal/app handles how that data is
displayed. There is no specification for text output to be hyperlinked (that
I know about at least), so while some apps may handle specific coding to tell
them that "this text should be a hyperlink", most will either parse the text
looking for hyper-linkable string, or more commonly, just output the text as
text.

If you control the app that is displaying info to the user (such as using QT or
another GUI tool), or generating html yourself, you can control if a text
string is a hyperlink or not, and what to do with the hyperlink when clicked.

So, if you want clickable links, you would need to find a console/terminal that
supported them.

Some references:
https://opensource.com/life/15/11/top-open-source-terminal-emulators (see
Gnome)
http://unix.stackexchange.com/questions/63417/is-there-a-terminal-app-that-allo
ws-filenames-to-be-clickable
http://superuser.com/questions/654116/configure-os-x-terminal-to-detect-urls-an
d-make-them-clickable
http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/url-launching.html


-----Original Message-----
From: Python-list [mailto:python-list-bounces+d.strohl=f5....@python.org] On
Behalf Of Deborah Swanson
Sent: Tuesday, January 03, 2017 1:35 PM To: 'Devin Jeanpierre'
<jeanpi...@gmail.com>
Cc: 'comp.lang.python' <pytho...@python.org>
Subject: RE: Re: Clickable hyperlinks

Devin Jeanpierre wrote, on January 03, 2017 12:57 PM

>Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The
way to
>make an URL clickable is to use a terminal that makes URLs clickable,
and
>print the URL:
>
>
>print("%s: %s" % (description, url))
>
>
>
>
>-- Devin

I'm sorry, I should have said a GUI console because I wouldn't expect a
text-based console to produce clickable links. But it appears that a simple GUI
console can't do it either. I have 3 GUI consoles and in all 3, when I ask:

print("%s: %s" % ("python.org list",
"https://mail.python.org/mailman/listinfo/python-list"))

I get:
python.org list: https://mail.python.org/mailman/listinfo/python-list

(Outlook made the url clickable here, the python GUI consoles just output plain
text.)

Pretty clearly the output is plain text because your format parameters are %s.
Maybe the trick, if there is one, is in a format parameter for urls.



On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson <pyt...@deborahswanson.net>
wrote:

Excel has a formula:

=HYPERLINK(url,description)

that will put a clickable link into a cell.

Does python have an equivalent function? Probably the most common use for it
would be output to the console, similar to a print statement, but clickable.

--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list

Michael Torrie

unread,
Jan 6, 2017, 1:15:22 AM1/6/17
to
On 01/03/2017 04:32 PM, Deborah Swanson wrote:
> The GUI consoles I have are in Pycharm, the IDLE that comes with
> Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when
> I open them, so they're capable of opening links, but whether that means
> their output space is capable of handling clickable links I don't know.

Hmm I still don't understand what you mean by "GUI console." Just because a
program asks for internet access does not mean anything about whether or not
your python program can display a clickable link in a console window. Besides
that, a clickable link would probably ask the OS to open it with the default
application, not cause your program to access the internet.

The standard out pipe (where stuff goes when you call print() or write to
sys.stdout) from a python program usually goes to a console or a terminal
emulator. PyCharm and IDLE both provide windows to display this output (they
emulate a terminal). But maybe you're misunderstanding what this actually is.
These windows just display a stream of bytes that come out of your program.
Certain escape codes can be emitted that can instruct the console or terminal
emulator to do things like set text color or display text at a certain
position. But there's certainly no special way to instruct the terminal
emulator to make a section of text into a hyperlink. Maybe if hyperlinks had
existed years ago when terminal escape codes were being defined we'd have a
"hyperlink" code that all consoles and terminals would understand. A few years
ago I saw some proposals to add an escape code to the ANSI scheme that would
encode hyperlinks, but nothing ever came of it because, honestly, it would be
too much hassle to roll this out to ever terminal emulator out there (to say
nothing of real terminals).

> I do know printing a full url with the %s specifier or entering a url
> and clicking enter just gives you the plain text url. Obviously, not all
> GUI consoles are enabled recognize and make clickable links from
> correctly formatted urls.

On my Linux machine, the terminal emulators I've used all make a regular url
printed out into a clickable link (or at least a right-clickable link). This
is just something they try to do with all things that look like urls.
Sometimes it's helpful, often it's annoying.

> I was hoping there was some functionality in python to make clickable
> links. Could be a package, if the core language doesn't have it.

Deborah Swanson

unread,
Jan 6, 2017, 1:15:24 AM1/6/17
to
Erik wrote, on January 03, 2017 3:30 PM
> To: pytho...@python.org
> Subject: Re: Clickable hyperlinks
>
> Hi.
>
> On 03/01/17 19:46, Deborah Swanson wrote:
> > Excel has a formula:
>
> When you start a new topic on the list, could you please write a new
> message rather than replying to an existing message and changing the
> title/subject?
>
> For those reading the list in a threaded email client, this
> message is
> shown as a continuation of your "Cleaning up conditionals"
> thread, and
> that whole thread in turn shows up as a continuation of the "mentor
> training python Romania with certification" discussion (which you had
> presumably "reply"ed to originally) ...
>
> Thanks. E.

Certainly. I've been on many other lists before (but none since about 2011),
and no one complained of or even mentioned this problem. But if it's a problem
with some email readers now, I can easily just start a new message. Just being
lazy and following old habits, I guess. ;)

Deborah Swanson

unread,
Jan 6, 2017, 1:15:25 AM1/6/17
to

Tim Chase

unread,
Jan 6, 2017, 1:15:34 AM1/6/17
to
Depends on what you're outputting. In your context, you're creating content
(and metadata) for a cell in an Excel workbook.

If that's the case you might have to use something like the xlwt module to
create an Excel-style worksheet and adjust the properties of the cell to
include the hyperlink property.

Or you can write out a .csv file with a hyperlink in a cell, which I believe
Excel can interpret as a hyperlink.

Or write an HTML document with the corresponding HTML <a> tag in it.

Or you can just print it to stdout as normal as some terminals detect them and
auto-linkify them.

But you have to specify where you want this link to appear to know how to solve
the problem.

-tkc

Grant Edwards

unread,
Jan 6, 2017, 1:15:34 AM1/6/17
to
On 2017-01-03, Deborah Swanson <pyt...@deborahswanson.net> wrote:

> I'm sorry, I should have said a GUI console because I wouldn't expect a
> text-based console to produce clickable links.

What's a "GUI console"?

--
Grant Edwards grant.b.edwards Yow! I want you to MEMORIZE
at the collected poems of
gmail.com EDNA ST VINCENT MILLAY
... BACKWARDS!!

Deborah Swanson

unread,
Jan 6, 2017, 1:15:34 AM1/6/17
to
Devin Jeanpierre wrote, on January 03, 2017 12:57 PM

>Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The
way to
>make an URL clickable is to use a terminal that makes URLs clickable,
and
>print the URL:
>
>
>print("%s: %s" % (description, url))
>
>
>
>
>-- Devin

I'm sorry, I should have said a GUI console because I wouldn't expect a
text-based console to produce clickable links. But it appears that a simple GUI
console can't do it either. I have 3 GUI consoles and in all 3, when I ask:

print("%s: %s" % ("python.org list",
"https://mail.python.org/mailman/listinfo/python-list"))

I get:
python.org list: https://mail.python.org/mailman/listinfo/python-list

(Outlook made the url clickable here, the python GUI consoles just output plain
text.)

Pretty clearly the output is plain text because your format parameters are %s.
Maybe the trick, if there is one, is in a format parameter for urls.



On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:

Excel has a formula:

=HYPERLINK(url,description)

that will put a clickable link into a cell.

Does python have an equivalent function? Probably the most common use for it
would be output to the console, similar to a print statement, but clickable.

--
https://mail.python.org/mailman/listinfo/python-list

Dan Strohl

unread,
Jan 6, 2017, 1:15:34 AM1/6/17
to
The best bet (unless you know that you are outputting to a specific place, like
html or excel) is to always include the "https://" or "http://" since most of
the consoles / terminals that support clickable links are parsing them based on
"seeing" the initial "http://". If your output just looks like
"mysite.com/coolpage.html", many systems will simply ignore them.

At one point I had made a class that you could pass the link to, and then you
could request different output based on your needs... so basically something
like:

>> url = url_class("mysite.com/coolpage.html")
>> print(url)
"http://mysite.com/coolpage.html")
>> print(url.plain)
"mysite.com/coolpage.html"
>> print(url.html('My Site"))
'<a href="http://mysite.com/coolpage.html">My Site</a>'

(or whatever... I think I actually just sub-classed url.parse or something)




-----Original Message-----
From: Python-list [mailto:python-list-bounces+d.strohl=f5....@python.org] On
Behalf Of Devin Jeanpierre
Sent: Tuesday, January 03, 2017 12:57 PM To: pyt...@deborahswanson.net
Cc: comp.lang.python <pytho...@python.org>
Subject: Re: Clickable hyperlinks

Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to
make an URL clickable is to use a terminal that makes URLs clickable, and print
the URL:

print("%s: %s" % (description, url))

-- Devin

On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson <pyt...@deborahswanson.net>
wrote:

> Excel has a formula:
>
> =HYPERLINK(url,description)
>
> that will put a clickable link into a cell.
>
> Does python have an equivalent function? Probably the most common use
> for it would be output to the console, similar to a print statement,
> but clickable.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list

Chris Angelico

unread,
Jan 6, 2017, 1:20:28 AM1/6/17
to
On Thu, Jan 5, 2017 at 9:58 AM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
> Chris Angelico wrote, on January 04, 2017 4:16 AM
>> This uses the 'webbrowser' module, which knows about a number
>> of different ways to open a browser, and will attempt them
>> all. So if you can figure out the UI part of things, actually
>> making the link pop up in a browser isn't too hard; for
>> instance, if you're doing OAuth at the command line and need
>> the user to go and authenticate, you can simply
>> webbrowser.open("http://......./") and it'll DTRT.
>>
>
> Thank you, thank you! Finally, at least one person on this list knows
> about something (anything) in the python world that is internet aware.
> It's also occurred to me that Beautifulsoup downloads data from a url,
> so that code must have access to some kind of an internet engine too.

We've been all talking at cross purposes a bit in this thread. Most of us
thought you were talking about the *user interface* of a clickable link, but if
you're talking about the *mechanics* of HTTP downloads, Python has excellent
facilities. I'd recommend checking out the third-party 'requests' module on
PyPI.

> I googled antigravity and found a number of interesting links.
>
> The History of Python: import antigravity
> http://python-history.blogspot.com/2010/06/import-antigravity.html
>
> Among other things, it was added to Python 3 in 2010, so it's been
> around a little while. And a comment mentions that "The antigravity
> module is also included in Python 2.7."
>
> And a reddit poster tells us that "if you type 'import antigravity' into
> a Python command line your default browser opens the XKCD comic 'Python'
> in a tab."
> https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type
> _import_antigravity_into_a_python/
>
> An "import antigravity" video at
> https://www.youtube.com/watch?v=_V0V6Rk6Fp4

Hehe, yeah. It's a big joke that started because XKCD mentioned the language.
But actually, the source code for antigravity.py itself isn't significant; all
it does is call on the webbrowser module:

https://docs.python.org/3/library/webbrowser.html

> Yes, I'd gotten as far as figuring out that you don't need a clickable
> link. Code that opens a url in a browse would do the job just fine. Or

D'Arcy Cain

unread,
Jan 6, 2017, 1:20:38 AM1/6/17
to
Deborah - please trim your quoted text.

On 2017-01-04 04:32 AM, Deborah Swanson wrote:
> Thanks, Steven. Yes, of course if you want to print strings you must
> enclose them in quotes. I think you learn that in Week 1 of any
> introductory course on Python.

Closer to minute one. When I investigated Python years ago the first thing I
learned was;

print "Hello, world"

> But we aren't trying to print strings here, the point is to produce
> clickable links. I didn't enclose them with quotes because I didn't see
> any point in printing plain text when I wanted clickable links. I

I'm not sure what your links are composed of but mine all look like sequences
of characters or "strings." It sounds like you are trying to make URL a first
class type like strings. integers, floats, etc. I can't think of any language
that treats URLs as first class objects. Even HTML needs quotes:

<A HREF="http://...">Go here</A>

> actually didn't understand why you thought I should print them, but it

You want to output them to something. That often involves printing them to a
particular handler.

> never would have occurred to me that you wanted me to print out a bunch
> of silly plain text strings, apparently just for the heck of it.

Is that really what you got from his message?

> At this point, if I pursue this any farther, it will be to look into how
> Firefox takes webpage titles and urls out of its sqlite database and
> makes objects you can click on to open the webpages. That's the basic
> technology I'd need to find a way to talk (write) python into doing.

I can assure you that FF prints the string at some point. It may wrap it in
HTML tags first but printing is what it does. Also, the URLs are stored as
strings. SQLite has no URL type. If it did then it would still store it as a
string somewhere. PostGreSQL would let you create a URL type if you wanted but
you would still need to wrap it in quotes
(single in this case) when you created the entry.

> If it's even worth it. On a practical level it's not worth it, way too
> much work for the teensy advantage of having it to use. It might be some
> giggles to figure out how to do it and maybe I will sometime just for
> funsies.

In all the messages in this thread I still don't understand what this "teensy
advantage" is supposed to be. Do you want to be able to do this:

make_web_link(http://...)

instead of:

make_web_link("http://...")

Deborah Swanson

unread,
Jan 6, 2017, 1:20:45 AM1/6/17
to
Steve D'Aprano wrote, on January 04, 2017 2:20 AM
>
> On Wed, 4 Jan 2017 03:46 pm, Deborah Swanson wrote:
>
> > As I've mentioned in other posts on this thread, I'm now
> thinking that
> > I need to write a class to do this, and find out how
> Firefox and url
> > aware terminals in Linux do it. There must be a way.
>
>
> A GUI application can interpret text any way it chooses.
> Firefox takes a HTML file and renders it, using whatever GUI
> library it chooses. That GUI library understands that text like:
>
> <b>Hello World!</b>
>
> should be shown in bold face, and text like:
>
> <a href="http://www.example.com">Example</a>
>
> should be shown as the word "Example" underlined and in some
> colour, and when you click on it the browser will navigate to
> the URL given. Firefox can do this because it controls the
> environment it runs in.
>
> Same for Excel, which also controls the environment it runs in.
>
> That's *not* the case for Python, which is at the mercy of
> whatever console or terminal application it is running in.
>
> However, you can use Python to write GUI applications. Then it becomes
> *your* responsibility to create the window, populate it with
> any buttons or text or scroll bars you want, and you can
> choose to interpret text any way you like -- including as
> clickable Hyperlinks.
>
> The bottom line is, there is no "a way" to do this. There are
> a thousand, ten thousand, ways to do it. Every web browser,
> every terminal, every URL-aware application, can choose its
> own way to do it. There's no one single way that works
> everywhere, but if you are working in a console or terminal,
> just printing the URL is likely to be interpreted by the
> console as a clickable link:
>
> print("http://www.example.com")
>
>
>
>
> --
> Steve
> "Cheer up," they said, "things could be worse." So I cheered
> up, and sure enough, things got worse.

I can appreciate all that and pretty much knew most of it already. At this
point I'm not so much concerned with how to put characters on a white space
that are clickable, we've pretty much established at least a couple dozen
messages ago that there's nothing for python to get hold of there because of
the vast number of places people might want to put clickable links. (I actually
read every message on a thread that interests me enough to participate.)

I think the entry point to this problem is to find out how to connect to the
internet when you click that link. Then I think the nuts and bolts of what
symbols to put where for a particular target would fall into place.

Gilmeh Serda

unread,
Jan 6, 2017, 1:20:49 AM1/6/17
to
On Tue, 03 Jan 2017 11:46:16 -0800, Deborah Swanson wrote:

> Does python have an equivalent function? Probably the most common use
> for it would be output to the console, similar to a print statement, but
> clickable.

Write it as HTML code save to temp file and call the browser which loads the
file.

--
Gilmeh

Steve D'Aprano

unread,
Jan 6, 2017, 1:20:51 AM1/6/17
to
â ŁCheer up,â Ř they said, â Łthings could be worse.â Ř So I cheered up, and

Erik

unread,
Jan 6, 2017, 1:20:51 AM1/6/17
to
Hi.

On 03/01/17 19:46, Deborah Swanson wrote:
> Excel has a formula:

Steven D'Aprano

unread,
Jan 6, 2017, 1:20:52 AM1/6/17
to
On Wednesday 04 January 2017 15:46, Deborah Swanson wrote:

> Steven D'Aprano wrote, on January 03, 2017 8:04 PM
[...]
>> Of course you have to put quotes around them to enter them in
>> your source code.
>> We don't expect this to work:
>>
>> print(Hello World!)
>>
>>
>> you have to use a string literal with quotes:
>>
>> print('Hello World!')
>>
>>
>> Same for all of the above.

> I didn't try printing them before, but I just did. Got:
>
>>>> print([Example](http://www.example.com)
>
> SyntaxError: invalid syntax (arrow pointing at the colon)

You missed the part where I said you have to put them in quotes.

Like any other string in Python, you have to use quotation marks around it for
Python to understand it as a string. None of these things will work:

print( Hello World! )

print( What do you want to do today? )

print( 3 2 1 blast off )

print( http://www.example.com )


This isn't specific to print. This won't work either:

message = Hello World!

In *all* of these cases, you have to tell Python you're dealing with a string,
and you do that with quotation marks:

message = "Hello World!"
print( 'What do you want to do today?' ) count_down = '3 2 1 blast off'
url = 'http://www.example.com'

Dennis Lee Bieber

unread,
Jan 6, 2017, 1:21:00 AM1/6/17
to
On Tue, 3 Jan 2017 20:46:31 -0800, "Deborah Swanson"
<pyt...@deborahswanson.net> declaimed the following:

>
>I didn't try printing them before, but I just did. Got:
>
>>>> print([Example](http://www.example.com)
>
>SyntaxError: invalid syntax (arrow pointing at the colon)
>

As I mentioned to someone else earlier...

Count your parentheses... You need a ) for each (

AND you need " (or ') around the strings.

As you entered it, you have invoked a print operation, passing it:

A list containing a reference to a (undefined) variable "Example", and then you
attempt call that list as a function passing it some unrecognized keyword
"http" with a colon that Python normally uses indicate the start of a code
block; said block being "//www.example.com".

Try

print("[Example](http://www.example.com)")

--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

Terry Reedy

unread,
Jan 6, 2017, 1:21:01 AM1/6/17
to
On 1/4/2017 4:32 AM, Deborah Swanson wrote:

> My original question was whether python had anything to provide this
> functionality, and the answer appears to be a resounding NO!!!

I would say 'Yes, but with user effort'.

To have a string interpreted as a clickable link, you send the string to
software capable of creating a clickable link, plus the information
'this is a clickable link'*. There are two ways to tag a string as a
link. One is to use markup around the url in the string itself.
'<url>' and html are example. Python provides multiple to make this
easy. The other is to tag the string with a separate argument. Python provides
tkinter, which wraps tk Text widgets, which have a powerful tag system. One
can define a Link tag that will a) cause text to be displayed, for instance,
blue and underlined and b) cause clicks on the text to generate a web request.
One could then use
mytext.insert('insert', 'http://www.example.com', Link)
Browser must do something similar when they encounter when they encounter html
link tags.

* If the software directly recognizes a bare url such as
'http://www.example.com' as a link, without further indication, then it
should have a way to disable conversion to a clickable link.

--
Terry Jan Reedy

Dan Sommers

unread,
Jan 6, 2017, 1:21:01 AM1/6/17
to
On Wed, 04 Jan 2017 16:40:00 +1100, Steven D'Aprano wrote:

> On Wednesday 04 January 2017 15:46, Deborah Swanson wrote:
>
>> Steven D'Aprano wrote, on January 03, 2017 8:04 PM
> [...]
>>> Of course you have to put quotes around them to enter them in
>>> your source code.
>>> We don't expect this to work:
>>>
>>> print(Hello World!)
>>>
>>> you have to use a string literal with quotes:
>>>
>>> print('Hello World!')
>>>
>>> Same for all of the above.
>
>> I didn't try printing them before, but I just did. Got:
>>
>>>>> print([Example](http://www.example.com)
>>
>> SyntaxError: invalid syntax (arrow pointing at the colon)
>
> You missed the part where I said you have to put them in quotes.

ObPython: "When I've got these antlers on I am dictating and when I take them
off I am not dictating." :-)

Deborah Swanson

unread,
Jan 6, 2017, 1:21:05 AM1/6/17
to
Chris Angelico wrote, on January 04, 2017 4:16 AM
>
> On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson
> <pyt...@deborahswanson.net> wrote:
> > I'm quite well aware by now that there is no one-sentence
> answer to my
> > original question, if there's any coherent answer at all.
> Them's the
> > breaks. Live with it or live without it, it doesn't care.
>
> Yeah, there's no simple answer; however, you'll find that
> Python on many platforms is entirely capable of popping a URL
> up in the user's default browser. Check this out:
>
> >>> import antigravity
>
> This uses the 'webbrowser' module, which knows about a number
> of different ways to open a browser, and will attempt them
> all. So if you can figure out the UI part of things, actually
> making the link pop up in a browser isn't too hard; for
> instance, if you're doing OAuth at the command line and need
> the user to go and authenticate, you can simply
> webbrowser.open("http://......./") and it'll DTRT.
>

Thank you, thank you! Finally, at least one person on this list knows about
something (anything) in the python world that is internet aware. It's also
occurred to me that Beautifulsoup downloads data from a url, so that code must
have access to some kind of an internet engine too.

I googled antigravity and found a number of interesting links.

The History of Python: import antigravity
http://python-history.blogspot.com/2010/06/import-antigravity.html

Among other things, it was added to Python 3 in 2010, so it's been around a
little while. And a comment mentions that "The antigravity module is also
included in Python 2.7."

And a reddit poster tells us that "if you type 'import antigravity' into a
Python command line your default browser opens the XKCD comic 'Python' in a
tab."
https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type
_import_antigravity_into_a_python/

An "import antigravity" video at
https://www.youtube.com/watch?v=_V0V6Rk6Fp4

And its page in the Package Index:
https://pypi.python.org/pypi/antigravity/0.1, with a Page Not Found Error for
the Home Page. So it doesn't look like there's any alternative but to download
it and look at the code.

Yes, I'd gotten as far as figuring out that you don't need a clickable link.
Code that opens a url in a browse would do the job just fine. Or the
webbrowser.open("http://......./") in a Linux terminal you suggest. (I just
have to get my Linux machine up and running again to try it.)

All in all, given that clickable urls in a console is a non-starter, this hits
the nail on the head. Many thanks again!

Deborah

Deborah Swanson

unread,
Jan 6, 2017, 1:21:07 AM1/6/17
to
Chris Angelico wrote, on January 04, 2017 4:16 AM
>
> Yeah, there's no simple answer; however, you'll find that
> Python on many platforms is entirely capable of popping a URL
> up in the user's default browser. Check this out:
>
> >>> import antigravity

I downloaded the code from the Package Index, but there really wasn't much in
it. This is the entire .py file:

STRIP_URL = "http://xkcd.com/353/"

def start():
return STRIP_URL

And setup.py is equally disappointing: from distutils.core import setup

setup(
name='antigravity',
version='0.1',
description='A really simple module that allow everyone to do
"import antigravity"',
author='Fabien Schwob',
author_email='antig...@x-phuture.com',
url='http://fabien.schwob.org/antigravity/',
packages=['antigravity'],
)

> This uses the 'webbrowser' module, which knows about a number
> of different ways to open a browser, and will attempt them
> all. So if you can figure out the UI part of things, actually
> making the link pop up in a browser isn't too hard; for
> instance, if you're doing OAuth at the command line and need
> the user to go and authenticate, you can simply
> webbrowser.open("http://......./") and it'll DTRT.
>
> ChrisA

All the action of antigravity must be done by the import statement. When import
opens a module that immediately returns a url, it must have a mechanism to open
it in a browser.

It would be very easy to do the same thing with my own .py and import it into
another .py.

Or, take a look at import's code and figure out how it opens a url in a
browser. I imagine it's the 'webbrowser' module you mention. If it tries
several methods, just pick one that will work for you.

Or, take a look at this Index of Packages Matching 'webbrowser' (~50 packages)
https://pypi.python.org/pypi?%3Aaction=search&term=webbrowser&submit=sea
rch

D'Arcy was right, there's lots in python that's internet aware, though that
wasn't the question I knew to ask.

Grant Edwards

unread,
Jan 6, 2017, 1:21:08 AM1/6/17
to
On 2017-01-03, Deborah Swanson <pyt...@deborahswanson.net> wrote:
> Grant Edwards wrote, on January 03, 2017 3:13 PM
>>
>> On 2017-01-03, Deborah Swanson <pyt...@deborahswanson.net> wrote:
>>
>> > I'm sorry, I should have said a GUI console because I
>> wouldn't expect
>> > a text-based console to produce clickable links.
>>
>> What's a "GUI console"?

> The GUI consoles I have are in Pycharm, the IDLE that comes with
> Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when
> I open them, so they're capable of opening links, but whether that means
> their output space is capable of handling clickable links I don't know.

Thanks, that's a bit clearer. For those of us from the Unix world "console"
means something else.

> I do know printing a full url with the %s specifier or entering a url
> and clicking enter just gives you the plain text url. Obviously, not all
> GUI consoles are enabled recognize and make clickable links from
> correctly formatted urls.
>
> I was hoping there was some functionality in python to make clickable
> links. Could be a package, if the core language doesn't have it.

There is no definition for what a "clickable link" is unless you're sending
HTML to a web browser. That means there's no way to create funcationality to
make one.

--
Grant Edwards grant.b.edwards Yow! I'm not an Iranian!!
at I voted for Dianne
gmail.com Feinstein!!

D'Arcy Cain

unread,
Jan 6, 2017, 1:21:08 AM1/6/17
to
On 2017-01-04 05:58 PM, Deborah Swanson wrote:
>> the user to go and authenticate, you can simply
>> webbrowser.open("http://......./") and it'll DTRT.
>
> Thank you, thank you! Finally, at least one person on this list knows
> about something (anything) in the python world that is internet aware.

Lots of things in Python are Internet aware. That's not the question you asked
though.

> It's also occurred to me that Beautifulsoup downloads data from a url,
> so that code must have access to some kind of an internet engine too.

Nope. You have to feed it HTML. You either need to generate that or capture
it from somewhere.

Deborah Swanson

unread,
Jan 6, 2017, 1:21:10 AM1/6/17
to
Steven D'Aprano wrote, on January 03, 2017 9:40 PM
>
> On Wednesday 04 January 2017 15:46, Deborah Swanson wrote:
>
> > Steven D'Aprano wrote, on January 03, 2017 8:04 PM
> [...]
> >> Of course you have to put quotes around them to enter them in your
> >> source code. We don't expect this to work:
> >>
> >> print(Hello World!)
> >>
> >>
> >> you have to use a string literal with quotes:
> >>
> >> print('Hello World!')
> >>
> >>
> >> Same for all of the above.
>
> > I didn't try printing them before, but I just did. Got:
> >
> >>>> print([Example](http://www.example.com)
> >
> > SyntaxError: invalid syntax (arrow pointing at the colon)
>
> You missed the part where I said you have to put them in quotes.
>
> Like any other string in Python, you have to use quotation
> marks around it for
> Python to understand it as a string. None of these things will work:
>
> print( Hello World! )
>
> print( What do you want to do today? )
>
> print( 3 2 1 blast off )
>
> print( http://www.example.com )
>
>
> This isn't specific to print. This won't work either:
>
> message = Hello World!
>
> In *all* of these cases, you have to tell Python you're
> dealing with a string,
> and you do that with quotation marks:
>
> message = "Hello World!"
> print( 'What do you want to do today?' )
> count_down = '3 2 1 blast off'
> url = 'http://www.example.com'
>
>
>
>
> --
> Steven

Thanks, Steven. Yes, of course if you want to print strings you must enclose
them in quotes. I think you learn that in Week 1 of any introductory course on
Python.

But we aren't trying to print strings here, the point is to produce clickable
links. I didn't enclose them with quotes because I didn't see any point in
printing plain text when I wanted clickable links. I actually didn't understand
why you thought I should print them, but it never would have occurred to me
that you wanted me to print out a bunch of silly plain text strings, apparently
just for the heck of it.

At this point, if I pursue this any farther, it will be to look into how
Firefox takes webpage titles and urls out of its sqlite database and makes
objects you can click on to open the webpages. That's the basic technology I'd
need to find a way to talk (write) python into doing.

If it's even worth it. On a practical level it's not worth it, way too much
work for the teensy advantage of having it to use. It might be some giggles to
figure out how to do it and maybe I will sometime just for funsies.

My original question was whether python had anything to provide this
functionality, and the answer appears to be a resounding NO!!!

Answer received, and thanks to all who contributed.

Michael Torrie

unread,
Jan 6, 2017, 1:21:11 AM1/6/17
to
On 01/03/2017 08:28 PM, Deborah Swanson wrote:
> I think you're making this too complicated. I meant a console in a GUI
> application.

Ahh. Well, a "console in a GUI application" is whatever you make it[1]. There's
no single "GUI console" hence my confusion and the confusion expressed by the
other poster. I was under the impression you are talking about printing
something to standard out with, for example, print(). Is this so, or are you
using a GUI toolkit to construct your application. What GUI toolkit are you
using? As I said, in Qt or GTK there are various ways to display hyperlinks.
For example, Qt lets you place a hyperlink in a form, or inside of a text
entry/display widget.

I still get the impression that you're working with standard out, using
print(). If so, then no, there's not going to be a way to force the OS to make
the output clickable, at least on Windows.

> Not true. Pycharm uses links in it's console output, they just aren't
> internet links. They link back to lines of code being referred to.

I think the problem here is the terminology with specific meaning in Windows
and Linux. I'm referring to either the Win32 console window (which is where
cmd.exe runs), or a terminal emulator in Linux, which is where you can interact
with the bash shell and run command-line programs. When people normally run
python apps that are not graphical, they normally do it from the Windows
console (via cmd.exe) or in Linux from Bash running in a terminal emulator.
Graphical apps do their own thing as far as displaying data and making windows
with clickable links in them.

[1] PyCharm and IDLE make "console" windows that are really normal GUI windows
and they direct the output from Python apps there. They may also choose to
display clickable links for things like errors. But once the app is run outside
of PyCharm, the output of the app would go to either a Windows console window,
or a terminal in Linux. If you wanted your app to make it's own window and
display clickable links, you're back to looking at a GUI toolkit (which is what
PyCharm and IDLE are built with) like Qt, GTK, Tk, wxWidgets, or something
else.

D'Arcy Cain

unread,
Jan 6, 2017, 1:21:14 AM1/6/17
to
On 2017-01-04 07:07 PM, Deborah Swanson wrote:
> D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM
>> In all the messages in this thread I still don't understand what this
>> "teensy advantage" is supposed to be. Do you want to be able
>> to do this:
>>
>> make_web_link(http://...)
>>
>> instead of:
>>
>> make_web_link("http://...")

[...]

> Is make_web_link("http://...") valid python code? That's exactly the

It's just a made up name. My point was that the first was syntactically
incorrect and the second, while not a real method, was at least parseable.

I think I saw someone else mention this but it bears repeating. When you ask a
question make sure you are presenting the problem and not your solution to a
secret problem. Always tell us the actual problem you are trying to solve.
You will get much better answers.

Think of it this way. You drop a ring down a drain. You can ask two
questions, "How do I remove a drain trap?" or "How do I recover a ring that I
dropped down the drain?" If you ask the first question you will get lots of
advice on tools and buckets, etc. People will assume that the drain is
blocked. Ask the second question and someone might mention a magnet and a
piece of string.

Rodrigo Bistolfi

unread,
Jan 6, 2017, 1:21:16 AM1/6/17
to
2017-01-04 7:39 GMT-03:00 Steve D'Aprano <steve+...@pearwood.info>:

> On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote:
>
> Aside: you've actually raised a fascinating question. I wonder whether
> there
> are any programming languages that understand URLs as native data types, so
> that *source code* starting with http:// etc is understood in the same way
> that source code starting with [ is seen as a list or { as a dict?
> ...
>

Some Smalltalk implementations have something that comes close:

st> 'https://python.org' asUrl retrieveContents

`asUrl` would be a string method returning a URL instance, which also has a
convenient method `retrieveContents` wrapping an http client. Not hard to do
with Python, I think this could be an interesting exercise for a learner.

Deborah Swanson

unread,
Jan 6, 2017, 1:21:23 AM1/6/17
to
D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM
>
> Deborah - please trim your quoted text.

Yes, I will. Some lists want to have it all to review in one message, some want
it trimmed to just the lines you are responding to. I was just waiting to see
what this list wants.

> On 2017-01-04 04:32 AM, Deborah Swanson wrote:
<snip>

> > But we aren't trying to print strings here, the point is to produce
> > clickable links. I didn't enclose them with quotes because I didn't
> > see any point in printing plain text when I wanted
> clickable links. I
>
> I'm not sure what your links are composed of but mine all look like
> sequences of characters or "strings." It sounds like you are
> trying to
> make URL a first class type like strings. integers, floats, etc. I
> can't think of any language that treats URLs as first class objects.
> Even HTML needs quotes:
>
> <A HREF="http://...">Go here</A>

It seemed reasonable that you might be able to print urls, which is why I tried
the experiment with all of Steven's suggested formats. But I was highly
skeptical that any would work without some kind of modifiers to a bare print
statement.

> > actually didn't understand why you thought I should print
> them, but it
>
> You want to output them to something. That often involves
> printing them
> to a particular handler.

Yes, that's one of the things I would expect if we could print them.

> > never would have occurred to me that you wanted me to print out a
> > bunch of silly plain text strings, apparently just for the
> heck of it.
>
> Is that really what you got from his message?

Please forgive me, and I hope Steven forgives me too, but I was sick to death
of all the beating on a dead horse (using Python to make clickable links in a
console, any console). I'd taken heart when he first suggested his print
experiment, because it was a plausible approach. But I lost my temper when he
upbraided me in this message for failing to enclose my strings in quotes, in a
most patronizing kind of way, when printing out plain text was absolutely
nowhere on the progress toward a solution scale. I've been quite impressed with
Steven's knowledge and talent, and after fending off the throng of unseeing
naysayers all afternoon, it was just a little too much. I really should have
closed my email reader hours before I read and replied to this message.
Shoulda, coulda, woulda.

<snip>

> I can assure you that FF prints the string at some point. It
> may wrap
> it in HTML tags first but printing is what it does. Also,
> the URLs are
> stored as strings. SQLite has no URL type. If it did then it would
> still store it as a string somewhere. PostGreSQL would let
> you create a
> URL type if you wanted but you would still need to wrap it in quotes
> (single in this case) when you created the entry.

I have no doubt that some variant of printing is involved. Transporting urls to
the internet is an output process. FF's sqlite implementation does store urls
as a text field in at least 2 tables. I would be interested in how FF takes
those text urls and opens web pages with them, although I've learned and
figured out just today some ways that Python can also do it. Turns out
clickable links were a red herring.

If Steven's original suggestion included anything but a bare print statement,
like the use of a special specifier or linking the print statement to some
module, the use of quoted strings would have at least been worthy of
consideration. But we all know what print("http://www.wherever.com") would
print, and it would be utterly worthless for the purpose at hand. Trying the
print statement without the quotes was a least a possibility, if there was any
awareness in the print code of urls and what to do with them. That was the
whole point of this fishing expedition, as I saw it. To see if there was any
undocumented or narrowly known-of features in the print code.

<snip>
> In all the messages in this thread I still don't understand what this
> "teensy advantage" is supposed to be. Do you want to be able
> to do this:
>
> make_web_link(http://...)
>
> instead of:
>
> make_web_link("http://...")
>
> --
> D'Arcy J.M. Cain
> System Administrator, Vex.Net
> http://www.Vex.Net/ IM:da...@Vex.Net
> VoIP: sip:da...@Vex.Net

You probably didn't see my oneliner on the "why do it" part in the swarm of
messages on this thread yesterday. In it I mentioned that the use would be to
open urls in the data I'm working with while I'm debugging the code that uses
them. I want to see what pages they open, without having to leave my IDE.
(Obviously I'd have to open another .py file, but that would be easier and
quicker than the alternatives.) I never intended my original question to be any
more than a frivolous toss out into the sea, to see if anyone knew an answer. I
was flat out astonished when it blew up into the mini-monster that it did.

Is make_web_link("http://...") valid python code? That's exactly the kind of
answer I was looking for, and I will try it (or look it up if it needs
something imported) as soon as I send this off. Thank you.

It's possible you caught just the tail end of a hot mess without seeing all the
irrational vitriol and nonsense that led up to this message. Lucky you.

Deborah

Deborah Swanson

unread,
Jan 6, 2017, 1:21:23 AM1/6/17
to
Steve D'Aprano wrote, on January 04, 2017 2:09 AM
>
> On Wed, 4 Jan 2017 08:00 pm, Deborah Swanson wrote:
>
> [speaking of threading emails]
>
> > I suppose. Times change of course, which always suits some and not
> > others. Personally, I think putting messages that have different
> > titles all in one thread is a bad design, but as I've said
> a couple of
> > times now I intend to comply with the new rules. But compliance
> > doesn't imply agreement. I prefer the old system, which ordered
> > threads by titles, but there's obviously no pleasing
> everyone on this
> > issue.
>
> Indeed :-)
>
> However, as far as I am aware, the use of threading by
> following the In-Reply-To and References header lines goes
> back to at least 1982, which makes them pretty old, and
> certainly pre-dates Gmail and Outlook by many years. They're
> also official standards which ALL email programs are supposed
> to follow, so if Gmail and Outlook fail to follow the
> standard, well, I wouldn't be surprised. I don't absolutely
> know for a fact that threading in this way is older than
> threading by subject line, but I'm fairly confident that what
> you are calling the "new rules" are actually the much older rules.
>
> Are there any old-timers here who were using email prior to
> 1982 that would care to comment?
>
>
> Here's a discussion by Jamie Zawinski, who wrote Netscape
> Navigator, before it became Mozila and Firefox, so he knows
> what he's talking about:
>
https://www.jwz.org/doc/threading.html


The concept here is not so much that people start a new topic and change the
subject, but that sometimes the topic just naturally evolves to the point that
a change in subject is sensible. Take this thread for example: the topic has
drifted from "Clickable hyperlinks" to talking about email threading. I should
be able to change the subject without breaking the
thread:

Clickable hyperlinks
+- RE: Clickable hyperlinks
ü +- Re: RE: Clickable hyperlinks
ü L- RE: Clickable hyperlinks
ü L- Threading [was Re: Clickable hyperlinks]
ü L- Re: Threading
+- RE: Clickable hyperlinks
L- Re: Clickable hyperlinks


Adding a "Re:" or "RE" to the subject doesn't change the thread, and neither
does changing the subject line in a more substantial way.

Of course, I can always *choose* to sort by subject line, or date. Personally I
hardly ever sort by thread, so it is no skin off my nose what you do. But when
you hit "Reply" to a message, you inherit the "Reference" and "In-Reply-To"
headers from the previous message, so at least some people will see it threaded
in an existing thread rather than starting a brand new one.




--
Steve
"Cheer up," they said, "things could be worse." So I cheered up, and sure
enough, things got worse.

Interesting history lesson. I actually wouldn't know which came first, since
I've only been online since 1992 (offline computing since 1972). I do know that
I've been on a lot of mailing lists like this one since 1992 (remember
Fidonet?), some with actively posting members in the thousands. And I have
never heard anyone whine and complain about threading issues that had anything
to do with the message title until yesterday.

So it goes. I'll repeat again that I'll comply with the rules of this group
that are brand spanking new to me, and I've been around a corner or two.

Steve D'Aprano

unread,
Jan 6, 2017, 1:21:25 AM1/6/17
to
On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote:

> Thanks, Steven. Yes, of course if you want to print strings you must
> enclose them in quotes. I think you learn that in Week 1 of any
> introductory course on Python.
>
> But we aren't trying to print strings here, the point is to produce
> clickable links. I didn't enclose them with quotes because I didn't see
> any point in printing plain text when I wanted clickable links. I
> actually didn't understand why you thought I should print them, but it
> never would have occurred to me that you wanted me to print out a bunch
> of silly plain text strings, apparently just for the heck of it.

What we have here is a failure to communicate :-)

I apologise for ruffling your feathers, but its difficult to judge another
person's level of knowledge. In someways you're obviously quite knowledgeable
about Python, but in other ways you're still learning (as we all are!) and I'm
afraid I may have misjudged exactly what your level of knowledge was. Sorry
about that.

I'm not suggesting that you print "silly plain text strings" just for the heck
of it. You've asked how to get a clickable link using Python. There is only one
way I know of to get a clickable link using Python:

Write out a link as plain text to another application which then interprets the
plain text as a clickable link.

You *might* be able to get clickable links in Python by writing an entire GUI
application, using (say) the tkinter library, or one of the third-party
GUI libraries like wxPython, kivy, pyqt, or others, but I haven't a clue
how. But even there, your links will start off as text, which means you will
still need to surround them with quotes to make them strings.


Aside: you've actually raised a fascinating question. I wonder whether there
are any programming languages that understand URLs as native data types, so
that *source code* starting with http:// etc is understood in the same way that
source code starting with [ is seen as a list or { as a dict?


But back to your problem: short of writing you own GUI application, in which
case you can do *anything you like*, you can:

- write out a HTML file containing the URLs you want, in <a href= ... </a>
tags, then open the HTML file in a web browser and let the web browser
interpret the HTML tags as clickable links;


- write out an Excel spreadsheet, using whatever format Excel expects, open
the spreadsheet in Excel, and let Excel interpret the mystery format as a
clickable link;

- print the URL to the console, and see if the console is smart enough to
interpret it as a clickable link.


I'm sorry that I can't give you a one-sentence answer "just use such-and-such a
function, that does exactly what you want in a platform-independent manner" :-(




--
Steve
â ŁCheer up,â Ř they said, â Łthings could be worse.â Ř So I cheered up, and

Chris Angelico

unread,
Jan 6, 2017, 1:21:26 AM1/6/17
to
On Thu, Jan 5, 2017 at 2:24 PM, D'Arcy Cain <da...@vex.net> wrote:
> Think of it this way. You drop a ring down a drain. You can ask two
> questions, "How do I remove a drain trap?" or "How do I recover a ring that
> I dropped down the drain?" If you ask the first question you will get lots
> of advice on tools and buckets, etc. People will assume that the drain is
> blocked. Ask the second question and someone might mention a magnet and a
> piece of string.

... and then you follow up with "it's a gold ring, magnet won't touch it", and
we'll go off on a tangent about whether the ring is sufficiently plain that it
might be the One Ring, and shouldn't you destroy it instead of retrieving
it.... because that's what we do here
:D

ChrisA

Steven D'Aprano

unread,
Jan 6, 2017, 1:21:26 AM1/6/17
to
On Wednesday 04 January 2017 14:04, Deborah Swanson wrote:

> Steve D'Aprano wrote, on January 03, 2017 4:56 PM
[...]
>> Python can't force the console to treat something as a
>> clickable link, if the console has no capacity for clickable
>> links. Nor can Python predict what format the console uses to
>> recognise a link.
>>
>> The best you can do is to experiment with a couple of simple
>> formats and see which, if any, your console understands:
>>
>> # HTML
>> <a href="http://www.example.com">Example.</a>
>>
>> # URL in angle brackets
>> Example <http://www.example.com>
>>
>> # URL alone
>> http://www.example.com
>>
>> # I can't remember what these are called
>> <url:http://www.example.com>
>>
>> # Markup
>> [Example](http://www.example.com)
>>
>> # Rest
>> `Example <http://www.example.com>`_
[...]

> I tried all of your examples in IDLE, and they all get syntax errors.
> I'd expect the same from PyCharm, though I didn't try it.

Syntax errors? How can you get syntax errors from *output* text?

The above are just text, no different from:

Hello World!


Of course you have to put quotes around them to enter them in your source code.
We don't expect this to work:

print(Hello World!)


you have to use a string literal with quotes:

print('Hello World!')


Same for all of the above.

py> print('<a href="http://www.example.com">Example.</a>')
<a href="http://www.example.com">Example.</a>


That's the Python interactive interpreter (with a custom prompt, I prefer "py>"
rather than the default ">>>"). Or I can do this from the operating system's
shell prompt:

steve@runes:~$ python -c "print 'http://www.example.com'"
http://www.example.com


If I do this in GNOME Terminal 2.30.2, the URL http... is a clickable link. But
that's specific to the terminal. Other terminals may or may not recognise it.

Rustom Mody

unread,
Jan 6, 2017, 1:21:30 AM1/6/17
to
This thread does lead to the question: Is the Url type in python less
first-class than it could be?

In scheme I could point to something like this
https://docs.racket-lang.org/net/url.html

Is there something equivalent in python?

Deborah Swanson

unread,
Jan 6, 2017, 1:21:31 AM1/6/17
to
Michael Torrie wrote, on January 03, 2017 8:05 PM
>
> On 01/03/2017 08:46 PM, Deborah Swanson wrote:
> > Actually it is, or at least it doesn't happen in all email readers.
> > Mine, for instance, never breaks up threads.
>
> Mine doesn't either, which illustrates the issue. This
> message, for example appears under a long thread that started
> out life as "mentor training python Romania with
> certification" and then changed to "Cleaning up conditionals"
> and then changed to "Clickable hyperlinks." All in one
> thread. My client doesn't break them up because they all tie
> together via the message-id header.
>
> And most of us would not like our client to break a thread
> just because the subject changes. Often in long conversations
> there are twists and turns in the discussion and sometimes
> side-paths are explored, and the subject often is changed to
> reflect this. With a truly threaded email reader (one that
> shows a tree of messages, not just chronological order), this
> works out very well. So if a discussion has a natural
> evolution into various other topics, it is often considered
> okay to just change the subject but continue the thread.
> Other times, it's better to start a new thread. Where that
> line is is hard to say!
>
> > I did say in the message you're replying to that I will try to
> > remember to start new threads with brand new messages.
> (Even though I
> > think pipermail's behavior is a bug, that's what many
> people read the
> > list
> > from.)
>
> Sounds good.
>
> I don't know of anyone that reads on the pipermail archive,
> except in response to web searches. Most people use clients
> of some kind, NNTP or email. And those that group messages
> according to message-id (most clients except for ones that
> try to be smart like Gmail web or Outlook) will show all the
> topics I mentioned before as one giant thread, which is by
> design (that's what message-id headers are for).

Michael Torrie

unread,
Jan 6, 2017, 1:21:32 AM1/6/17
to
On 01/04/2017 03:58 PM, Deborah Swanson wrote:
> Thank you, thank you! Finally, at least one person on this list knows
> about something (anything) in the python world that is internet aware.
> It's also occurred to me that Beautifulsoup downloads data from a url,
> so that code must have access to some kind of an internet engine too.

Except that you never mentioned anything about this in your posts before. It
seemed to me you were asking about printing out clickable hyperlinks with
python. Calling the OS to launch a browser to view a url is a different beast
which is why no one mentioned it before.

If you don't tell us what you're actually trying to do (your end goal), things
are more frustrating for everyone. If you had said early on you just want to
be able to send the user to a particular url in a web browser, what Chris
suggested would have been said a long time ago, rather than focusing on console
output which is what you were asking about and focusing attention on.

Just so you know, BeautifulSoup does not do any internet access itself; it's
only an HTML parser. You have to fetch web pages using something like python's
urllib and then feed the data to BeautifulSoup. urllib is not a web browser
though. It can pretend to be a browser as far as the server is concerned but
it knows nothing about javascript or rendering. It just retrieves bytes which
you can then feed to BeautifulSoup or some other parser.

> Yes, I'd gotten as far as figuring out that you don't need a clickable
> link. Code that opens a url in a browse would do the job just fine.

Good! I just wish you would have mentioned this much earlier as your end goal.

> Or the webbrowser.open("http://......./") in a Linux terminal you suggest.
> (I just have to get my Linux machine up and running again to try it.)

The webbrowser module does not require console or terminal output. It will work
on Windows or Linux if I'm not mistaken. It asks the OS to launch the default
browser and load the indicated url.

Chris Angelico

unread,
Jan 6, 2017, 1:21:34 AM1/6/17
to
On Thu, Jan 5, 2017 at 3:19 PM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
> I downloaded the code from the Package Index, but there really wasn't
> much in it. This is the entire .py file:

Ehh, wrong file. Try the one in the standard library:

https://github.com/python/cpython/blob/master/Lib/antigravity.py
https://github.com/python/cpython/blob/master/Lib/webbrowser.py

Or you can look in your installed Python - "import webbrowser;
print(webbrowser.__file__)" will tell you where.

ChrisA

Steve D'Aprano

unread,
Jan 6, 2017, 1:21:36 AM1/6/17
to
On Wed, 4 Jan 2017 08:00 pm, Deborah Swanson wrote:

[speaking of threading emails]

> I suppose. Times change of course, which always suits some and not
> others. Personally, I think putting messages that have different titles
> all in one thread is a bad design, but as I've said a couple of times
> now I intend to comply with the new rules. But compliance doesn't imply
> agreement. I prefer the old system, which ordered threads by titles, but
> there's obviously no pleasing everyone on this issue.

Indeed :-)

However, as far as I am aware, the use of threading by following the
In-Reply-To and References header lines goes back to at least 1982, which makes
them pretty old, and certainly pre-dates Gmail and Outlook by many years.
They're also official standards which ALL email programs are supposed to
follow, so if Gmail and Outlook fail to follow the standard, well, I wouldn't
be surprised. I don't absolutely know for a fact that threading in this way is
older than threading by subject line, but I'm fairly confident that what you
are calling the "new rules" are actually the much older rules.

Are there any old-timers here who were using email prior to 1982 that would
care to comment?


Here's a discussion by Jamie Zawinski, who wrote Netscape Navigator, before it
became Mozila and Firefox, so he knows what he's talking about:

https://www.jwz.org/doc/threading.html


The concept here is not so much that people start a new topic and change the
subject, but that sometimes the topic just naturally evolves to the point that
a change in subject is sensible. Take this thread for example: the topic has
drifted from "Clickable hyperlinks" to talking about email threading. I should
be able to change the subject without breaking the thread:

Clickable hyperlinks
âö£âö RE: Clickable hyperlinks
âöé âö£âö Re: RE: Clickable hyperlinks
âöé âööâö RE: Clickable hyperlinks
âöé âööâö Threading [was Re: Clickable hyperlinks]
âöé âööâö Re: Threading
âö£âö RE: Clickable hyperlinks
âööâö Re: Clickable hyperlinks


Adding a "Re:" or "RE" to the subject doesn't change the thread, and neither
does changing the subject line in a more substantial way.

Of course, I can always *choose* to sort by subject line, or date. Personally I
hardly ever sort by thread, so it is no skin off my nose what you do. But when
you hit "Reply" to a message, you inherit the "Reference" and "In-Reply-To"
headers from the previous message, so at least some people will see it threaded
in an existing thread rather than starting a brand new one.




--
Steve
â £Cheer up,â Ø they said, â £things could be worse.â Ø So I cheered up, and

Chris Angelico

unread,
Jan 6, 2017, 1:21:39 AM1/6/17
to
On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
> I'm quite well aware by now that there is no one-sentence answer to my
> original question, if there's any coherent answer at all. Them's the
> breaks. Live with it or live without it, it doesn't care.

Yeah, there's no simple answer; however, you'll find that Python on many
platforms is entirely capable of popping a URL up in the user's default
browser. Check this out:

>>> import antigravity

This uses the 'webbrowser' module, which knows about a number of different ways
to open a browser, and will attempt them all. So if you can figure out the UI
part of things, actually making the link pop up in a browser isn't too hard;
for instance, if you're doing OAuth at the command line and need the user to go
and authenticate, you can simply webbrowser.open("http://......./") and it'll
DTRT.

ChrisA

Dennis Lee Bieber

unread,
Jan 6, 2017, 1:21:39 AM1/6/17
to
On Wed, 4 Jan 2017 14:58:42 -0800, "Deborah Swanson"
<pyt...@deborahswanson.net> declaimed the following:

>Thank you, thank you! Finally, at least one person on this list knows
>about something (anything) in the python world that is internet aware.
>It's also occurred to me that Beautifulsoup downloads data from a url,
>so that code must have access to some kind of an internet engine too.
>

Uhm... There is a big difference between "clickable links" and
"internet aware".

Look at the Library reference manual. There is a section on "Internet
Data Handling", and another on "Internet Protocols and Support". For just
retrieving things identified by a URL, there are both urllib and urllib2; and
for more specific there is httplib, ftplib, poplib, etc.


Clickable links is a user interface matter -- and as mentioned numerous
times, depends upon the features of the interface, not of Python (unless you
are writing a full-fledged GUI application in which case you have to tag
entities as clickable and handle the user clicking on that entity, followed by
actually using one of the above library routines to fetch the contents at the
clickable's target)

D'Arcy Cain

unread,
Jan 6, 2017, 1:21:40 AM1/6/17
to
On 2017-01-04 08:44 AM, Rodrigo Bistolfi wrote:
> 2017-01-04 7:39 GMT-03:00 Steve D'Aprano <steve+...@pearwood.info>:
>> Aside: you've actually raised a fascinating question. I wonder whether
>> there
>> are any programming languages that understand URLs as native data types, so
>> that *source code* starting with http:// etc is understood in the same way
>> that source code starting with [ is seen as a list or { as a dict?
>
> Some Smalltalk implementations have something that comes close:
>
> st> 'https://python.org' asUrl retrieveContents

But notice that even here the URL has to be defined as a string. To be a first
class object you would need to do something like this:

url = https://python.org/

The only time you would see that is in config files and languages like shell
where everything is a string so no quotes necessary. They are implied.

> `asUrl` would be a string method returning a URL instance, which also has a
> convenient method `retrieveContents` wrapping an http client. Not hard to
> do with Python, I think this could be an interesting exercise for a learner.

Sure but the issue is, what to do with it. In any case, it's still just a
wrapper around various string methods. You still need to give the class a
string to create the object.

Paul Rudin

unread,
Jan 6, 2017, 1:21:45 AM1/6/17
to
"Deborah Swanson" <pyt...@deborahswanson.net> writes:

>
> I didn't try printing them before, but I just did. Got:
>
>>>> print([Example](http://www.example.com)
>
> SyntaxError: invalid syntax (arrow pointing at the colon)
>

With respect, if you typed that at python then it's probably a good idea to
take a step back and work through the excellent tutorial.

https://docs.python.org/3/tutorial/

boB Stepp

unread,
Jan 6, 2017, 1:21:46 AM1/6/17
to
On Tue, Jan 3, 2017 at 10:46 PM, Deborah Swanson
<pyt...@deborahswanson.net> wrote:
>

>
> I didn't try printing them before, but I just did. Got:
>
> >>> print([Example](http://www.example.com)
>
> SyntaxError: invalid syntax (arrow pointing at the colon)

As Steve had said, you need to put everything inside quotes. Also, you are
missing a matching paren. Thus:

py3: print("[Example](http://www.example.com)")
[Example](http://www.example.com)

As to whether anything will be "clickable" or not, what has already been said
about different types of terminals applies.

--
boB

It is loading more messages.
0 new messages