blob: 20c091399f3bf7048d19803472e4f780f843c05e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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
9
10The :mod:`webbrowser` module provides a high-level interface to allow displaying
11Web-based documents to users. Under most circumstances, simply calling the
Georg Brandl502d9a52009-07-26 15:02:41 +000012:func:`.open` function from this module will do the right thing.
Georg Brandl116aa622007-08-15 14:28:22 +000013
14Under Unix, graphical browsers are preferred under X11, but text-mode browsers
15will be used if graphical browsers are not available or an X11 display isn't
16available. If text-mode browsers are used, the calling process will block until
17the user exits the browser.
18
19If the environment variable :envvar:`BROWSER` exists, it is interpreted to
Ezio Melotti579c29b2009-12-24 03:23:23 +000020override the platform default list of browsers, as a :data:`os.pathsep`-separated
21list of browsers to try in order. When the value of a list part contains the
22string ``%s``, then it is interpreted as a literal browser command line to be
23used with the argument URL substituted for ``%s``; if the part does not contain
Georg Brandl8a1e4c42009-05-25 21:13:36 +000024``%s``, it is simply interpreted as the name of the browser to launch. [1]_
Georg Brandl116aa622007-08-15 14:28:22 +000025
26For non-Unix platforms, or when a remote browser is available on Unix, the
27controlling process will not wait for the user to finish with the browser, but
28allow the remote browser to maintain its own windows on the display. If remote
29browsers are not available on Unix, the controlling process will launch a new
30browser and wait.
31
32The script :program:`webbrowser` can be used as a command-line interface for the
33module. It accepts an URL as the argument. It accepts the following optional
Éric Araujo713d3032010-11-18 16:38:46 +000034parameters: ``-n`` opens the URL in a new browser window, if possible;
35``-t`` opens the URL in a new browser page ("tab"). The options are,
Georg Brandl116aa622007-08-15 14:28:22 +000036naturally, mutually exclusive.
37
38The following exception is defined:
39
40
41.. exception:: Error
42
43 Exception raised when a browser control error occurs.
44
45The following functions are defined:
46
47
Georg Brandl7f01a132009-09-16 15:58:14 +000048.. function:: open(url, new=0, autoraise=True)
Georg Brandl116aa622007-08-15 14:28:22 +000049
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +000050 Display *url* using the default browser. If *new* is 0, the *url* is opened
51 in the same browser window if possible. If *new* is 1, a new browser window
52 is opened if possible. If *new* is 2, a new browser page ("tab") is opened
53 if possible. If *autoraise* is ``True``, the window is raised if possible
54 (note that under many window managers this will occur regardless of the
55 setting of this variable).
Georg Brandl116aa622007-08-15 14:28:22 +000056
Benjamin Petersond23f8222009-04-05 19:13:16 +000057 Note that on some platforms, trying to open a filename using this function,
58 may work and start the operating system's associated program. However, this
59 is neither supported nor portable.
60
Georg Brandl116aa622007-08-15 14:28:22 +000061
62.. function:: open_new(url)
63
64 Open *url* in a new window of the default browser, if possible, otherwise, open
65 *url* in the only browser window.
66
Georg Brandl116aa622007-08-15 14:28:22 +000067.. function:: open_new_tab(url)
68
69 Open *url* in a new page ("tab") of the default browser, if possible, otherwise
70 equivalent to :func:`open_new`.
71
Georg Brandl116aa622007-08-15 14:28:22 +000072
Georg Brandl7f01a132009-09-16 15:58:14 +000073.. function:: get(using=None)
Georg Brandl116aa622007-08-15 14:28:22 +000074
Georg Brandl7f01a132009-09-16 15:58:14 +000075 Return a controller object for the browser type *using*. If *using* is
76 ``None``, return a controller for a default browser appropriate to the
77 caller's environment.
Georg Brandl116aa622007-08-15 14:28:22 +000078
79
Georg Brandl7f01a132009-09-16 15:58:14 +000080.. function:: register(name, constructor, instance=None)
Georg Brandl116aa622007-08-15 14:28:22 +000081
82 Register the browser type *name*. Once a browser type is registered, the
83 :func:`get` function can return a controller for that browser type. If
84 *instance* is not provided, or is ``None``, *constructor* will be called without
85 parameters to create an instance when needed. If *instance* is provided,
86 *constructor* will never be called, and may be ``None``.
87
88 This entry point is only useful if you plan to either set the :envvar:`BROWSER`
89 variable or call :func:`get` with a nonempty argument matching the name of a
90 handler you declare.
91
92A number of browser types are predefined. This table gives the type names that
93may be passed to the :func:`get` function and the corresponding instantiations
94for the controller classes, all defined in this module.
95
96+-----------------------+-----------------------------------------+-------+
97| Type Name | Class Name | Notes |
98+=======================+=========================================+=======+
99| ``'mozilla'`` | :class:`Mozilla('mozilla')` | |
100+-----------------------+-----------------------------------------+-------+
101| ``'firefox'`` | :class:`Mozilla('mozilla')` | |
102+-----------------------+-----------------------------------------+-------+
103| ``'netscape'`` | :class:`Mozilla('netscape')` | |
104+-----------------------+-----------------------------------------+-------+
105| ``'galeon'`` | :class:`Galeon('galeon')` | |
106+-----------------------+-----------------------------------------+-------+
107| ``'epiphany'`` | :class:`Galeon('epiphany')` | |
108+-----------------------+-----------------------------------------+-------+
109| ``'skipstone'`` | :class:`BackgroundBrowser('skipstone')` | |
110+-----------------------+-----------------------------------------+-------+
111| ``'kfmclient'`` | :class:`Konqueror()` | \(1) |
112+-----------------------+-----------------------------------------+-------+
113| ``'konqueror'`` | :class:`Konqueror()` | \(1) |
114+-----------------------+-----------------------------------------+-------+
115| ``'kfm'`` | :class:`Konqueror()` | \(1) |
116+-----------------------+-----------------------------------------+-------+
117| ``'mosaic'`` | :class:`BackgroundBrowser('mosaic')` | |
118+-----------------------+-----------------------------------------+-------+
119| ``'opera'`` | :class:`Opera()` | |
120+-----------------------+-----------------------------------------+-------+
121| ``'grail'`` | :class:`Grail()` | |
122+-----------------------+-----------------------------------------+-------+
123| ``'links'`` | :class:`GenericBrowser('links')` | |
124+-----------------------+-----------------------------------------+-------+
125| ``'elinks'`` | :class:`Elinks('elinks')` | |
126+-----------------------+-----------------------------------------+-------+
127| ``'lynx'`` | :class:`GenericBrowser('lynx')` | |
128+-----------------------+-----------------------------------------+-------+
129| ``'w3m'`` | :class:`GenericBrowser('w3m')` | |
130+-----------------------+-----------------------------------------+-------+
131| ``'windows-default'`` | :class:`WindowsDefault` | \(2) |
132+-----------------------+-----------------------------------------+-------+
133| ``'internet-config'`` | :class:`InternetConfig` | \(3) |
134+-----------------------+-----------------------------------------+-------+
135| ``'macosx'`` | :class:`MacOSX('default')` | \(4) |
136+-----------------------+-----------------------------------------+-------+
137
138Notes:
139
140(1)
141 "Konqueror" is the file manager for the KDE desktop environment for Unix, and
142 only makes sense to use if KDE is running. Some way of reliably detecting KDE
143 would be nice; the :envvar:`KDEDIR` variable is not sufficient. Note also that
144 the name "kfm" is used even when using the :program:`konqueror` command with KDE
145 2 --- the implementation selects the best strategy for running Konqueror.
146
147(2)
148 Only on Windows platforms.
149
150(3)
Georg Brandlc575c902008-09-13 17:46:05 +0000151 Only on Mac OS platforms; requires the standard MacPython :mod:`ic` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153(4)
Georg Brandlc575c902008-09-13 17:46:05 +0000154 Only on Mac OS X platform.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156Here are some simple examples::
157
Ezio Melotti579c29b2009-12-24 03:23:23 +0000158 url = 'http://www.python.org/'
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Georg Brandl48310cd2009-01-03 21:18:54 +0000160 # Open URL in a new tab, if a browser window is already open.
Ezio Melotti579c29b2009-12-24 03:23:23 +0000161 webbrowser.open_new_tab(url + 'doc/')
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163 # Open URL in new window, raising the window if possible.
164 webbrowser.open_new(url)
165
166
167.. _browser-controllers:
168
169Browser Controller Objects
170--------------------------
171
Benjamin Peterson5879d412009-03-30 14:51:56 +0000172Browser controllers provide these methods which parallel three of the
173module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000174
175
Georg Brandl7f01a132009-09-16 15:58:14 +0000176.. method:: controller.open(url, new=0, autoraise=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000177
178 Display *url* using the browser handled by this controller. If *new* is 1, a new
179 browser window is opened if possible. If *new* is 2, a new browser page ("tab")
180 is opened if possible.
181
182
183.. method:: controller.open_new(url)
184
185 Open *url* in a new window of the browser handled by this controller, if
186 possible, otherwise, open *url* in the only browser window. Alias
187 :func:`open_new`.
188
189
190.. method:: controller.open_new_tab(url)
191
192 Open *url* in a new page ("tab") of the browser handled by this controller, if
193 possible, otherwise equivalent to :func:`open_new`.
Georg Brandl8a1e4c42009-05-25 21:13:36 +0000194
195
196.. rubric:: Footnotes
197
198.. [1] Executables named here without a full path will be searched in the
199 directories given in the :envvar:`PATH` environment variable.