| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`webbrowser` --- Convenient Web-browser controller | 
 | 2 | ======================================================= | 
 | 3 |  | 
 | 4 | .. module:: webbrowser | 
 | 5 |    :synopsis: Easy-to-use controller for Web browsers. | 
 | 6 | .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org> | 
 | 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> | 
 | 8 |  | 
| Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 9 | **Source code:** :source:`Lib/webbrowser.py` | 
 | 10 |  | 
 | 11 | -------------- | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 12 |  | 
 | 13 | The :mod:`webbrowser` module provides a high-level interface to allow displaying | 
 | 14 | Web-based documents to users. Under most circumstances, simply calling the | 
| Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 15 | :func:`.open` function from this module will do the right thing. | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 16 |  | 
 | 17 | Under Unix, graphical browsers are preferred under X11, but text-mode browsers | 
 | 18 | will be used if graphical browsers are not available or an X11 display isn't | 
 | 19 | available.  If text-mode browsers are used, the calling process will block until | 
 | 20 | the user exits the browser. | 
 | 21 |  | 
 | 22 | If the environment variable :envvar:`BROWSER` exists, it is interpreted to | 
| Ezio Melotti | 9c236bf | 2009-12-24 02:54:53 +0000 | [diff] [blame] | 23 | override the platform default list of browsers, as a :data:`os.pathsep`-separated | 
 | 24 | list of browsers to try in order.  When the value of a list part contains the | 
 | 25 | string ``%s``, then it is  interpreted as a literal browser command line to be | 
 | 26 | used with the argument URL substituted for ``%s``; if the part does not contain | 
| Georg Brandl | 903953c | 2009-05-17 08:55:00 +0000 | [diff] [blame] | 27 | ``%s``, it is simply interpreted as the name of the browser to launch. [1]_ | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 28 |  | 
 | 29 | For non-Unix platforms, or when a remote browser is available on Unix, the | 
 | 30 | controlling process will not wait for the user to finish with the browser, but | 
 | 31 | allow the remote browser to maintain its own windows on the display.  If remote | 
 | 32 | browsers are not available on Unix, the controlling process will launch a new | 
 | 33 | browser and wait. | 
 | 34 |  | 
 | 35 | The script :program:`webbrowser` can be used as a command-line interface for the | 
 | 36 | module. It accepts an URL as the argument. It accepts the following optional | 
| Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 37 | parameters: ``-n`` opens the URL in a new browser window, if possible; | 
 | 38 | ``-t`` opens the URL in a new browser page ("tab"). The options are, | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 39 | naturally, mutually exclusive. | 
 | 40 |  | 
 | 41 | The following exception is defined: | 
 | 42 |  | 
 | 43 |  | 
 | 44 | .. exception:: Error | 
 | 45 |  | 
 | 46 |    Exception raised when a browser control error occurs. | 
 | 47 |  | 
 | 48 | The following functions are defined: | 
 | 49 |  | 
 | 50 |  | 
| Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame^] | 51 | .. function:: open(url, new=0, autoraise=True) | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 52 |  | 
| Benjamin Peterson | 617a558 | 2009-07-23 14:25:31 +0000 | [diff] [blame] | 53 |    Display *url* using the default browser. If *new* is 0, the *url* is opened | 
 | 54 |    in the same browser window if possible.  If *new* is 1, a new browser window | 
 | 55 |    is opened if possible.  If *new* is 2, a new browser page ("tab") is opened | 
 | 56 |    if possible.  If *autoraise* is ``True``, the window is raised if possible | 
 | 57 |    (note that under many window managers this will occur regardless of the | 
 | 58 |    setting of this variable). | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 59 |  | 
| Georg Brandl | 29b3630 | 2009-04-04 13:45:49 +0000 | [diff] [blame] | 60 |    Note that on some platforms, trying to open a filename using this function, | 
 | 61 |    may work and start the operating system's associated program.  However, this | 
 | 62 |    is neither supported nor portable. | 
 | 63 |  | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 64 |    .. versionchanged:: 2.5 | 
 | 65 |       *new* can now be 2. | 
 | 66 |  | 
 | 67 |  | 
 | 68 | .. function:: open_new(url) | 
 | 69 |  | 
 | 70 |    Open *url* in a new window of the default browser, if possible, otherwise, open | 
 | 71 |    *url* in the only browser window. | 
 | 72 |  | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 73 | .. function:: open_new_tab(url) | 
 | 74 |  | 
 | 75 |    Open *url* in a new page ("tab") of the default browser, if possible, otherwise | 
 | 76 |    equivalent to :func:`open_new`. | 
 | 77 |  | 
 | 78 |    .. versionadded:: 2.5 | 
 | 79 |  | 
 | 80 |  | 
 | 81 | .. function:: get([name]) | 
 | 82 |  | 
 | 83 |    Return a controller object for the browser type *name*.  If *name* is empty, | 
 | 84 |    return a controller for a default browser appropriate to the caller's | 
 | 85 |    environment. | 
 | 86 |  | 
 | 87 |  | 
 | 88 | .. function:: register(name, constructor[, instance]) | 
 | 89 |  | 
 | 90 |    Register the browser type *name*.  Once a browser type is registered, the | 
 | 91 |    :func:`get` function can return a controller for that browser type.  If | 
 | 92 |    *instance* is not provided, or is ``None``, *constructor* will be called without | 
 | 93 |    parameters to create an instance when needed.  If *instance* is provided, | 
 | 94 |    *constructor* will never be called, and may be ``None``. | 
 | 95 |  | 
 | 96 |    This entry point is only useful if you plan to either set the :envvar:`BROWSER` | 
 | 97 |    variable or call :func:`get` with a nonempty argument matching the name of a | 
 | 98 |    handler you declare. | 
 | 99 |  | 
 | 100 | A number of browser types are predefined.  This table gives the type names that | 
 | 101 | may be passed to the :func:`get` function and the corresponding instantiations | 
 | 102 | for the controller classes, all defined in this module. | 
 | 103 |  | 
 | 104 | +-----------------------+-----------------------------------------+-------+ | 
 | 105 | | Type Name             | Class Name                              | Notes | | 
 | 106 | +=======================+=========================================+=======+ | 
 | 107 | | ``'mozilla'``         | :class:`Mozilla('mozilla')`             |       | | 
 | 108 | +-----------------------+-----------------------------------------+-------+ | 
 | 109 | | ``'firefox'``         | :class:`Mozilla('mozilla')`             |       | | 
 | 110 | +-----------------------+-----------------------------------------+-------+ | 
 | 111 | | ``'netscape'``        | :class:`Mozilla('netscape')`            |       | | 
 | 112 | +-----------------------+-----------------------------------------+-------+ | 
 | 113 | | ``'galeon'``          | :class:`Galeon('galeon')`               |       | | 
 | 114 | +-----------------------+-----------------------------------------+-------+ | 
 | 115 | | ``'epiphany'``        | :class:`Galeon('epiphany')`             |       | | 
 | 116 | +-----------------------+-----------------------------------------+-------+ | 
 | 117 | | ``'skipstone'``       | :class:`BackgroundBrowser('skipstone')` |       | | 
 | 118 | +-----------------------+-----------------------------------------+-------+ | 
 | 119 | | ``'kfmclient'``       | :class:`Konqueror()`                    | \(1)  | | 
 | 120 | +-----------------------+-----------------------------------------+-------+ | 
 | 121 | | ``'konqueror'``       | :class:`Konqueror()`                    | \(1)  | | 
 | 122 | +-----------------------+-----------------------------------------+-------+ | 
 | 123 | | ``'kfm'``             | :class:`Konqueror()`                    | \(1)  | | 
 | 124 | +-----------------------+-----------------------------------------+-------+ | 
 | 125 | | ``'mosaic'``          | :class:`BackgroundBrowser('mosaic')`    |       | | 
 | 126 | +-----------------------+-----------------------------------------+-------+ | 
 | 127 | | ``'opera'``           | :class:`Opera()`                        |       | | 
 | 128 | +-----------------------+-----------------------------------------+-------+ | 
 | 129 | | ``'grail'``           | :class:`Grail()`                        |       | | 
 | 130 | +-----------------------+-----------------------------------------+-------+ | 
 | 131 | | ``'links'``           | :class:`GenericBrowser('links')`        |       | | 
 | 132 | +-----------------------+-----------------------------------------+-------+ | 
 | 133 | | ``'elinks'``          | :class:`Elinks('elinks')`               |       | | 
 | 134 | +-----------------------+-----------------------------------------+-------+ | 
 | 135 | | ``'lynx'``            | :class:`GenericBrowser('lynx')`         |       | | 
 | 136 | +-----------------------+-----------------------------------------+-------+ | 
 | 137 | | ``'w3m'``             | :class:`GenericBrowser('w3m')`          |       | | 
 | 138 | +-----------------------+-----------------------------------------+-------+ | 
 | 139 | | ``'windows-default'`` | :class:`WindowsDefault`                 | \(2)  | | 
 | 140 | +-----------------------+-----------------------------------------+-------+ | 
| Ned Deily | 9b5952d | 2012-04-18 12:48:09 -0700 | [diff] [blame] | 141 | | ``'macosx'``          | :class:`MacOSX('default')`              | \(3)  | | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 142 | +-----------------------+-----------------------------------------+-------+ | 
| Ned Deily | 9b5952d | 2012-04-18 12:48:09 -0700 | [diff] [blame] | 143 | | ``'safari'``          | :class:`MacOSX('safari')`               | \(3)  | | 
| Sandro Tosi | 04cd095 | 2012-03-31 17:22:47 +0200 | [diff] [blame] | 144 | +-----------------------+-----------------------------------------+-------+ | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 145 |  | 
 | 146 | Notes: | 
 | 147 |  | 
 | 148 | (1) | 
 | 149 |    "Konqueror" is the file manager for the KDE desktop environment for Unix, and | 
 | 150 |    only makes sense to use if KDE is running.  Some way of reliably detecting KDE | 
 | 151 |    would be nice; the :envvar:`KDEDIR` variable is not sufficient.  Note also that | 
 | 152 |    the name "kfm" is used even when using the :program:`konqueror` command with KDE | 
 | 153 |    2 --- the implementation selects the best strategy for running Konqueror. | 
 | 154 |  | 
 | 155 | (2) | 
 | 156 |    Only on Windows platforms. | 
 | 157 |  | 
 | 158 | (3) | 
| Georg Brandl | 9af9498 | 2008-09-13 17:41:16 +0000 | [diff] [blame] | 159 |    Only on Mac OS X platform. | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 160 |  | 
 | 161 | Here are some simple examples:: | 
 | 162 |  | 
| Ezio Melotti | 9c236bf | 2009-12-24 02:54:53 +0000 | [diff] [blame] | 163 |    url = 'http://www.python.org/' | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 164 |  | 
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 165 |    # Open URL in a new tab, if a browser window is already open. | 
| Ezio Melotti | 9c236bf | 2009-12-24 02:54:53 +0000 | [diff] [blame] | 166 |    webbrowser.open_new_tab(url + 'doc/') | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 167 |  | 
 | 168 |    # Open URL in new window, raising the window if possible. | 
 | 169 |    webbrowser.open_new(url) | 
 | 170 |  | 
 | 171 |  | 
 | 172 | .. _browser-controllers: | 
 | 173 |  | 
 | 174 | Browser Controller Objects | 
 | 175 | -------------------------- | 
 | 176 |  | 
| Benjamin Peterson | ac0580e | 2009-03-30 02:49:32 +0000 | [diff] [blame] | 177 | Browser controllers provide these methods which parallel three of the | 
 | 178 | module-level convenience functions: | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 179 |  | 
 | 180 |  | 
| Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame^] | 181 | .. method:: controller.open(url, new=0, autoraise=True) | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 182 |  | 
 | 183 |    Display *url* using the browser handled by this controller. If *new* is 1, a new | 
 | 184 |    browser window is opened if possible. If *new* is 2, a new browser page ("tab") | 
 | 185 |    is opened if possible. | 
 | 186 |  | 
 | 187 |  | 
 | 188 | .. method:: controller.open_new(url) | 
 | 189 |  | 
 | 190 |    Open *url* in a new window of the browser handled by this controller, if | 
 | 191 |    possible, otherwise, open *url* in the only browser window.  Alias | 
 | 192 |    :func:`open_new`. | 
 | 193 |  | 
 | 194 |  | 
 | 195 | .. method:: controller.open_new_tab(url) | 
 | 196 |  | 
 | 197 |    Open *url* in a new page ("tab") of the browser handled by this controller, if | 
 | 198 |    possible, otherwise equivalent to :func:`open_new`. | 
 | 199 |  | 
 | 200 |    .. versionadded:: 2.5 | 
 | 201 |  | 
| Georg Brandl | 903953c | 2009-05-17 08:55:00 +0000 | [diff] [blame] | 202 |  | 
 | 203 | .. rubric:: Footnotes | 
 | 204 |  | 
 | 205 | .. [1] Executables named here without a full path will be searched in the | 
 | 206 |        directories given in the :envvar:`PATH` environment variable. |