Clickable hyperlinks

Deborah Swanson python at deborahswanson.net
Wed Jan 4 21:29:00 EST 2017


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




More information about the Python-list mailing list