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