blob: 74e12a2c58718a3a235dfd8baab2991b59b74d11 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl9fa61bb2009-07-26 14:19:57 +000013:func:`.open` function from this module will do the right thing.
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
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
Ezio Melotti9c236bf2009-12-24 02:54:53 +000021override the platform default list of browsers, as a :data:`os.pathsep`-separated
22list of browsers to try in order. When the value of a list part contains the
23string ``%s``, then it is interpreted as a literal browser command line to be
24used with the argument URL substituted for ``%s``; if the part does not contain
Georg Brandl903953c2009-05-17 08:55:00 +000025``%s``, it is simply interpreted as the name of the browser to launch. [1]_
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Éric Araujoa8132ec2010-12-16 03:53:53 +000035parameters: ``-n`` opens the URL in a new browser window, if possible;
36``-t`` opens the URL in a new browser page ("tab"). The options are,
Georg Brandl8ec7f652007-08-15 14:28:01 +000037naturally, 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
Benjamin Peterson617a5582009-07-23 14:25:31 +000049.. function:: open(url[, new=0[, autoraise=True]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
Benjamin Peterson617a5582009-07-23 14:25:31 +000051 Display *url* using the default browser. If *new* is 0, the *url* is opened
52 in the same browser window if possible. If *new* is 1, a new browser window
53 is opened if possible. If *new* is 2, a new browser page ("tab") is opened
54 if possible. If *autoraise* is ``True``, the window is raised if possible
55 (note that under many window managers this will occur regardless of the
56 setting of this variable).
Georg Brandl8ec7f652007-08-15 14:28:01 +000057
Georg Brandl29b36302009-04-04 13:45:49 +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 Brandl8ec7f652007-08-15 14:28:01 +000062 .. versionchanged:: 2.5
63 *new* can now be 2.
64
65
66.. function:: open_new(url)
67
68 Open *url* in a new window of the default browser, if possible, otherwise, open
69 *url* in the only browser window.
70
Georg Brandl8ec7f652007-08-15 14:28:01 +000071.. function:: open_new_tab(url)
72
73 Open *url* in a new page ("tab") of the default browser, if possible, otherwise
74 equivalent to :func:`open_new`.
75
76 .. versionadded:: 2.5
77
78
79.. function:: get([name])
80
81 Return a controller object for the browser type *name*. If *name* is empty,
82 return a controller for a default browser appropriate to the caller's
83 environment.
84
85
86.. function:: register(name, constructor[, instance])
87
88 Register the browser type *name*. Once a browser type is registered, the
89 :func:`get` function can return a controller for that browser type. If
90 *instance* is not provided, or is ``None``, *constructor* will be called without
91 parameters to create an instance when needed. If *instance* is provided,
92 *constructor* will never be called, and may be ``None``.
93
94 This entry point is only useful if you plan to either set the :envvar:`BROWSER`
95 variable or call :func:`get` with a nonempty argument matching the name of a
96 handler you declare.
97
98A number of browser types are predefined. This table gives the type names that
99may be passed to the :func:`get` function and the corresponding instantiations
100for the controller classes, all defined in this module.
101
102+-----------------------+-----------------------------------------+-------+
103| Type Name | Class Name | Notes |
104+=======================+=========================================+=======+
105| ``'mozilla'`` | :class:`Mozilla('mozilla')` | |
106+-----------------------+-----------------------------------------+-------+
107| ``'firefox'`` | :class:`Mozilla('mozilla')` | |
108+-----------------------+-----------------------------------------+-------+
109| ``'netscape'`` | :class:`Mozilla('netscape')` | |
110+-----------------------+-----------------------------------------+-------+
111| ``'galeon'`` | :class:`Galeon('galeon')` | |
112+-----------------------+-----------------------------------------+-------+
113| ``'epiphany'`` | :class:`Galeon('epiphany')` | |
114+-----------------------+-----------------------------------------+-------+
115| ``'skipstone'`` | :class:`BackgroundBrowser('skipstone')` | |
116+-----------------------+-----------------------------------------+-------+
117| ``'kfmclient'`` | :class:`Konqueror()` | \(1) |
118+-----------------------+-----------------------------------------+-------+
119| ``'konqueror'`` | :class:`Konqueror()` | \(1) |
120+-----------------------+-----------------------------------------+-------+
121| ``'kfm'`` | :class:`Konqueror()` | \(1) |
122+-----------------------+-----------------------------------------+-------+
123| ``'mosaic'`` | :class:`BackgroundBrowser('mosaic')` | |
124+-----------------------+-----------------------------------------+-------+
125| ``'opera'`` | :class:`Opera()` | |
126+-----------------------+-----------------------------------------+-------+
127| ``'grail'`` | :class:`Grail()` | |
128+-----------------------+-----------------------------------------+-------+
129| ``'links'`` | :class:`GenericBrowser('links')` | |
130+-----------------------+-----------------------------------------+-------+
131| ``'elinks'`` | :class:`Elinks('elinks')` | |
132+-----------------------+-----------------------------------------+-------+
133| ``'lynx'`` | :class:`GenericBrowser('lynx')` | |
134+-----------------------+-----------------------------------------+-------+
135| ``'w3m'`` | :class:`GenericBrowser('w3m')` | |
136+-----------------------+-----------------------------------------+-------+
137| ``'windows-default'`` | :class:`WindowsDefault` | \(2) |
138+-----------------------+-----------------------------------------+-------+
139| ``'internet-config'`` | :class:`InternetConfig` | \(3) |
140+-----------------------+-----------------------------------------+-------+
141| ``'macosx'`` | :class:`MacOSX('default')` | \(4) |
142+-----------------------+-----------------------------------------+-------+
143
144Notes:
145
146(1)
147 "Konqueror" is the file manager for the KDE desktop environment for Unix, and
148 only makes sense to use if KDE is running. Some way of reliably detecting KDE
149 would be nice; the :envvar:`KDEDIR` variable is not sufficient. Note also that
150 the name "kfm" is used even when using the :program:`konqueror` command with KDE
151 2 --- the implementation selects the best strategy for running Konqueror.
152
153(2)
154 Only on Windows platforms.
155
156(3)
Georg Brandl9af94982008-09-13 17:41:16 +0000157 Only on Mac OS platforms; requires the standard MacPython :mod:`ic` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000158
159(4)
Georg Brandl9af94982008-09-13 17:41:16 +0000160 Only on Mac OS X platform.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000161
162Here are some simple examples::
163
Ezio Melotti9c236bf2009-12-24 02:54:53 +0000164 url = 'http://www.python.org/'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000166 # Open URL in a new tab, if a browser window is already open.
Ezio Melotti9c236bf2009-12-24 02:54:53 +0000167 webbrowser.open_new_tab(url + 'doc/')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168
169 # Open URL in new window, raising the window if possible.
170 webbrowser.open_new(url)
171
172
173.. _browser-controllers:
174
175Browser Controller Objects
176--------------------------
177
Benjamin Petersonac0580e2009-03-30 02:49:32 +0000178Browser controllers provide these methods which parallel three of the
179module-level convenience functions:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000180
181
Ezio Melotti9c236bf2009-12-24 02:54:53 +0000182.. method:: controller.open(url[, new=0[, autoraise=True]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000183
184 Display *url* using the browser handled by this controller. If *new* is 1, a new
185 browser window is opened if possible. If *new* is 2, a new browser page ("tab")
186 is opened if possible.
187
188
189.. method:: controller.open_new(url)
190
191 Open *url* in a new window of the browser handled by this controller, if
192 possible, otherwise, open *url* in the only browser window. Alias
193 :func:`open_new`.
194
195
196.. method:: controller.open_new_tab(url)
197
198 Open *url* in a new page ("tab") of the browser handled by this controller, if
199 possible, otherwise equivalent to :func:`open_new`.
200
201 .. versionadded:: 2.5
202
Georg Brandl903953c2009-05-17 08:55:00 +0000203
204.. rubric:: Footnotes
205
206.. [1] Executables named here without a full path will be searched in the
207 directories given in the :envvar:`PATH` environment variable.