blob: e8f47562e1774fe7bfd8bc509f27bddac80e9f81 [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 Brandlc5605df2009-08-13 08:26:44 +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 Melottibbea7dc2009-12-24 03:26:13 +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
34parameters: :option:`-n` opens the URL in a new browser window, if possible;
35:option:`-t` opens the URL in a new browser page ("tab"). The options are,
36naturally, 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 Brandlb044b2a2009-09-16 16:05:59 +000048.. function:: open(url, new=0, autoraise=True)
Georg Brandl116aa622007-08-15 14:28:22 +000049
Georg Brandlc5605df2009-08-13 08:26:44 +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
67
68.. function:: open_new_tab(url)
69
70 Open *url* in a new page ("tab") of the default browser, if possible, otherwise
71 equivalent to :func:`open_new`.
72
Georg Brandl116aa622007-08-15 14:28:22 +000073
Georg Brandlb044b2a2009-09-16 16:05:59 +000074.. function:: get(using=None)
Georg Brandl116aa622007-08-15 14:28:22 +000075
Georg Brandlb044b2a2009-09-16 16:05:59 +000076 Return a controller object for the browser type *using*. If *using* is
77 ``None``, return a controller for a default browser appropriate to the
78 caller's environment.
Georg Brandl116aa622007-08-15 14:28:22 +000079
80
Georg Brandlb044b2a2009-09-16 16:05:59 +000081.. function:: register(name, constructor, instance=None)
Georg Brandl116aa622007-08-15 14:28:22 +000082
83 Register the browser type *name*. Once a browser type is registered, the
84 :func:`get` function can return a controller for that browser type. If
85 *instance* is not provided, or is ``None``, *constructor* will be called without
86 parameters to create an instance when needed. If *instance* is provided,
87 *constructor* will never be called, and may be ``None``.
88
89 This entry point is only useful if you plan to either set the :envvar:`BROWSER`
90 variable or call :func:`get` with a nonempty argument matching the name of a
91 handler you declare.
92
93A number of browser types are predefined. This table gives the type names that
94may be passed to the :func:`get` function and the corresponding instantiations
95for the controller classes, all defined in this module.
96
97+-----------------------+-----------------------------------------+-------+
98| Type Name | Class Name | Notes |
99+=======================+=========================================+=======+
100| ``'mozilla'`` | :class:`Mozilla('mozilla')` | |
101+-----------------------+-----------------------------------------+-------+
102| ``'firefox'`` | :class:`Mozilla('mozilla')` | |
103+-----------------------+-----------------------------------------+-------+
104| ``'netscape'`` | :class:`Mozilla('netscape')` | |
105+-----------------------+-----------------------------------------+-------+
106| ``'galeon'`` | :class:`Galeon('galeon')` | |
107+-----------------------+-----------------------------------------+-------+
108| ``'epiphany'`` | :class:`Galeon('epiphany')` | |
109+-----------------------+-----------------------------------------+-------+
110| ``'skipstone'`` | :class:`BackgroundBrowser('skipstone')` | |
111+-----------------------+-----------------------------------------+-------+
112| ``'kfmclient'`` | :class:`Konqueror()` | \(1) |
113+-----------------------+-----------------------------------------+-------+
114| ``'konqueror'`` | :class:`Konqueror()` | \(1) |
115+-----------------------+-----------------------------------------+-------+
116| ``'kfm'`` | :class:`Konqueror()` | \(1) |
117+-----------------------+-----------------------------------------+-------+
118| ``'mosaic'`` | :class:`BackgroundBrowser('mosaic')` | |
119+-----------------------+-----------------------------------------+-------+
120| ``'opera'`` | :class:`Opera()` | |
121+-----------------------+-----------------------------------------+-------+
122| ``'grail'`` | :class:`Grail()` | |
123+-----------------------+-----------------------------------------+-------+
124| ``'links'`` | :class:`GenericBrowser('links')` | |
125+-----------------------+-----------------------------------------+-------+
126| ``'elinks'`` | :class:`Elinks('elinks')` | |
127+-----------------------+-----------------------------------------+-------+
128| ``'lynx'`` | :class:`GenericBrowser('lynx')` | |
129+-----------------------+-----------------------------------------+-------+
130| ``'w3m'`` | :class:`GenericBrowser('w3m')` | |
131+-----------------------+-----------------------------------------+-------+
132| ``'windows-default'`` | :class:`WindowsDefault` | \(2) |
133+-----------------------+-----------------------------------------+-------+
134| ``'internet-config'`` | :class:`InternetConfig` | \(3) |
135+-----------------------+-----------------------------------------+-------+
136| ``'macosx'`` | :class:`MacOSX('default')` | \(4) |
137+-----------------------+-----------------------------------------+-------+
138
139Notes:
140
141(1)
142 "Konqueror" is the file manager for the KDE desktop environment for Unix, and
143 only makes sense to use if KDE is running. Some way of reliably detecting KDE
144 would be nice; the :envvar:`KDEDIR` variable is not sufficient. Note also that
145 the name "kfm" is used even when using the :program:`konqueror` command with KDE
146 2 --- the implementation selects the best strategy for running Konqueror.
147
148(2)
149 Only on Windows platforms.
150
151(3)
Georg Brandlc575c902008-09-13 17:46:05 +0000152 Only on Mac OS platforms; requires the standard MacPython :mod:`ic` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154(4)
Georg Brandlc575c902008-09-13 17:46:05 +0000155 Only on Mac OS X platform.
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157Here are some simple examples::
158
Ezio Melottibbea7dc2009-12-24 03:26:13 +0000159 url = 'http://www.python.org/'
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandl48310cd2009-01-03 21:18:54 +0000161 # Open URL in a new tab, if a browser window is already open.
Ezio Melottibbea7dc2009-12-24 03:26:13 +0000162 webbrowser.open_new_tab(url + 'doc/')
Georg Brandl116aa622007-08-15 14:28:22 +0000163
164 # Open URL in new window, raising the window if possible.
165 webbrowser.open_new(url)
166
167
168.. _browser-controllers:
169
170Browser Controller Objects
171--------------------------
172
Benjamin Peterson5879d412009-03-30 14:51:56 +0000173Browser controllers provide these methods which parallel three of the
174module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176
Georg Brandlb044b2a2009-09-16 16:05:59 +0000177.. method:: controller.open(url, new=0, autoraise=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179 Display *url* using the browser handled by this controller. If *new* is 1, a new
180 browser window is opened if possible. If *new* is 2, a new browser page ("tab")
181 is opened if possible.
182
183
184.. method:: controller.open_new(url)
185
186 Open *url* in a new window of the browser handled by this controller, if
187 possible, otherwise, open *url* in the only browser window. Alias
188 :func:`open_new`.
189
190
191.. method:: controller.open_new_tab(url)
192
193 Open *url* in a new page ("tab") of the browser handled by this controller, if
194 possible, otherwise equivalent to :func:`open_new`.
Georg Brandl8a1e4c42009-05-25 21:13:36 +0000195
196
197.. rubric:: Footnotes
198
199.. [1] Executables named here without a full path will be searched in the
200 directories given in the :envvar:`PATH` environment variable.