[Tutor] Webbrowser

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Sep 7 23:59:24 EDT 2003



On Sun, 7 Sep 2003, Andrei wrote:

> The error:
>
>    File "/usr/lib/python2.2/webbrowser.py", line 38, in get
>      raise Error("could not locate runnable browser")
> webbrowser.Error: could not locate runnable browser
>
> I think the code works under Gnome (it's some time ago that I tested it)
> and it also works under Windows.

Hi Andrei,

Hmmm... the webbrowser module does the following checks on a Unix
environment:


## Within webbrowser.py:

    if os.environ.get("DISPLAY"):
        if _iscommand("netscape") or _iscommand("mozilla"):
            if _iscommand("mozilla"):
                register("mozilla", None, Netscape("mozilla"))
            if _iscommand("netscape"):
                register("netscape", None, Netscape("netscape"))
        if _iscommand("mosaic"):
            register("mosaic", None, GenericBrowser
                                         ("mosaic %s >/dev/null &"))
        if _iscommand("kfm") or _iscommand("konqueror"):
            register("kfm", Konqueror, Konqueror())
        if _iscommand("grail"):
            register("grail", Grail, None)



> Any idea how I could solve this?

According to the code above, it should have been able to find KDE's
Konqueror browser fine, as long as:

    1.  os.environ.get('DISPLAY') is some kind of true value, and
    2.  kfm or konqueror can be found by the system.

So you may want to check those first.




> On a sidenote: is there any support for browsers which have tabs (as in:
> open in a new tab)?

Good question!  I'm not sure how to do this platform-independently yet,
but Mozilla does support it:

    http://www.mozilla.org/docs/command-line-args.html

through the 'new-tab' option.  To get this to work, we'd probably have to
modify the 'Netscape' handler within webbrowser.py.  Here's one way we
might be able to do it:

###
class Netscape:
    """Launcher class for Netscape browsers.
    dyoo: Modified to let windows to be opened in tabs.
    """
    def __init__(self, name):
        self.name = name
        self.basename = os.path.basename(name)

    def _remote(self, action, autoraise):
        raise_opt = ("-noraise", "-raise")[autoraise]
        cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name,
                                                      raise_opt,
                                                      action)
        rc = os.system(cmd)
        if rc:
            import time
            os.system("%s &" % self.name)
            time.sleep(PROCESS_CREATION_DELAY)
            rc = os.system(cmd)
        return not rc

    def open(self, url, new=0, autoraise=1):
        if new == 'tab':
            self._remote("openURL(%s, new-tab)"%url, autoraise)
        if new:
            self._remote("openURL(%s, new-window)"%url, autoraise)
        else:
            self._remote("openURL(%s)" % url, autoraise)

    def open_new(self, url):
        self.open(url, 1)
###


This modification augments the 'new' keyword parameter so that it can take
in 'tab', in which case it'll try to open using the 'new-tab' option:

###
import webbrowser_modified
webbrowser_modified.open('http://python.org', new='tab')
###


Can't test this on my end, since I'm running on Safari.  Can someone check
to see if this works?  If this were polished up, it might make a nice
submission to Sourceforge... *grin*


Good luck to you!




More information about the Tutor mailing list