blob: 23211fa9e6452b1289a343e35bd85309591d5e16 [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
Raymond Hettinger469271d2011-01-27 20:38:46 +00009**Source code:** :source:`Lib/webbrowser.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
13The :mod:`webbrowser` module provides a high-level interface to allow displaying
14Web-based documents to users. Under most circumstances, simply calling the
Georg Brandl502d9a52009-07-26 15:02:41 +000015:func:`.open` function from this module will do the right thing.
Georg Brandl116aa622007-08-15 14:28:22 +000016
17Under Unix, graphical browsers are preferred under X11, but text-mode browsers
18will be used if graphical browsers are not available or an X11 display isn't
19available. If text-mode browsers are used, the calling process will block until
20the user exits the browser.
21
22If the environment variable :envvar:`BROWSER` exists, it is interpreted to
Ezio Melotti579c29b2009-12-24 03:23:23 +000023override the platform default list of browsers, as a :data:`os.pathsep`-separated
24list of browsers to try in order. When the value of a list part contains the
25string ``%s``, then it is interpreted as a literal browser command line to be
26used with the argument URL substituted for ``%s``; if the part does not contain
Georg Brandl8a1e4c42009-05-25 21:13:36 +000027``%s``, it is simply interpreted as the name of the browser to launch. [1]_
Georg Brandl116aa622007-08-15 14:28:22 +000028
29For non-Unix platforms, or when a remote browser is available on Unix, the
30controlling process will not wait for the user to finish with the browser, but
31allow the remote browser to maintain its own windows on the display. If remote
32browsers are not available on Unix, the controlling process will launch a new
33browser and wait.
34
35The script :program:`webbrowser` can be used as a command-line interface for the
36module. It accepts an URL as the argument. It accepts the following optional
Éric Araujo713d3032010-11-18 16:38:46 +000037parameters: ``-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 Brandl116aa622007-08-15 14:28:22 +000039naturally, mutually exclusive.
40
41The following exception is defined:
42
43
44.. exception:: Error
45
46 Exception raised when a browser control error occurs.
47
48The following functions are defined:
49
50
Georg Brandl7f01a132009-09-16 15:58:14 +000051.. function:: open(url, new=0, autoraise=True)
Georg Brandl116aa622007-08-15 14:28:22 +000052
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +000053 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 Brandl116aa622007-08-15 14:28:22 +000059
Benjamin Petersond23f8222009-04-05 19:13:16 +000060 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 Brandl116aa622007-08-15 14:28:22 +000064
65.. function:: open_new(url)
66
67 Open *url* in a new window of the default browser, if possible, otherwise, open
68 *url* in the only browser window.
69
Georg Brandl116aa622007-08-15 14:28:22 +000070.. function:: open_new_tab(url)
71
72 Open *url* in a new page ("tab") of the default browser, if possible, otherwise
73 equivalent to :func:`open_new`.
74
Georg Brandl116aa622007-08-15 14:28:22 +000075
Georg Brandl7f01a132009-09-16 15:58:14 +000076.. function:: get(using=None)
Georg Brandl116aa622007-08-15 14:28:22 +000077
Georg Brandl7f01a132009-09-16 15:58:14 +000078 Return a controller object for the browser type *using*. If *using* is
79 ``None``, return a controller for a default browser appropriate to the
80 caller's environment.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
Georg Brandl7f01a132009-09-16 15:58:14 +000083.. function:: register(name, constructor, instance=None)
Georg Brandl116aa622007-08-15 14:28:22 +000084
85 Register the browser type *name*. Once a browser type is registered, the
86 :func:`get` function can return a controller for that browser type. If
87 *instance* is not provided, or is ``None``, *constructor* will be called without
88 parameters to create an instance when needed. If *instance* is provided,
89 *constructor* will never be called, and may be ``None``.
90
91 This entry point is only useful if you plan to either set the :envvar:`BROWSER`
92 variable or call :func:`get` with a nonempty argument matching the name of a
93 handler you declare.
94
95A number of browser types are predefined. This table gives the type names that
96may be passed to the :func:`get` function and the corresponding instantiations
97for the controller classes, all defined in this module.
98
Senthil Kumaranc9095992011-12-21 22:31:36 +080099+------------------------+-----------------------------------------+-------+
100| Type Name | Class Name | Notes |
101+========================+=========================================+=======+
102| ``'mozilla'`` | :class:`Mozilla('mozilla')` | |
103+------------------------+-----------------------------------------+-------+
104| ``'firefox'`` | :class:`Mozilla('mozilla')` | |
105+------------------------+-----------------------------------------+-------+
106| ``'netscape'`` | :class:`Mozilla('netscape')` | |
107+------------------------+-----------------------------------------+-------+
108| ``'galeon'`` | :class:`Galeon('galeon')` | |
109+------------------------+-----------------------------------------+-------+
110| ``'epiphany'`` | :class:`Galeon('epiphany')` | |
111+------------------------+-----------------------------------------+-------+
112| ``'skipstone'`` | :class:`BackgroundBrowser('skipstone')` | |
113+------------------------+-----------------------------------------+-------+
114| ``'kfmclient'`` | :class:`Konqueror()` | \(1) |
115+------------------------+-----------------------------------------+-------+
116| ``'konqueror'`` | :class:`Konqueror()` | \(1) |
117+------------------------+-----------------------------------------+-------+
118| ``'kfm'`` | :class:`Konqueror()` | \(1) |
119+------------------------+-----------------------------------------+-------+
120| ``'mosaic'`` | :class:`BackgroundBrowser('mosaic')` | |
121+------------------------+-----------------------------------------+-------+
122| ``'opera'`` | :class:`Opera()` | |
123+------------------------+-----------------------------------------+-------+
124| ``'grail'`` | :class:`Grail()` | |
125+------------------------+-----------------------------------------+-------+
126| ``'links'`` | :class:`GenericBrowser('links')` | |
127+------------------------+-----------------------------------------+-------+
128| ``'elinks'`` | :class:`Elinks('elinks')` | |
129+------------------------+-----------------------------------------+-------+
130| ``'lynx'`` | :class:`GenericBrowser('lynx')` | |
131+------------------------+-----------------------------------------+-------+
132| ``'w3m'`` | :class:`GenericBrowser('w3m')` | |
133+------------------------+-----------------------------------------+-------+
134| ``'windows-default'`` | :class:`WindowsDefault` | \(2) |
135+------------------------+-----------------------------------------+-------+
136| ``'internet-config'`` | :class:`InternetConfig` | \(3) |
137+------------------------+-----------------------------------------+-------+
138| ``'macosx'`` | :class:`MacOSX('default')` | \(4) |
139+------------------------+-----------------------------------------+-------+
Sandro Tosidc60f942012-03-31 17:44:33 +0200140| ``'safari'`` | :class:`MacOSX('safari')` | \(4) |
141+------------------------+-----------------------------------------+-------+
Senthil Kumaranc9095992011-12-21 22:31:36 +0800142| ``'google-chrome'`` | :class:`Chrome('google-chrome')` | |
143+------------------------+-----------------------------------------+-------+
144| ``'chrome'`` | :class:`Chrome('chrome')` | |
145+------------------------+-----------------------------------------+-------+
146| ``'chromium'`` | :class:`Chromium('chromium')` | |
147+------------------------+-----------------------------------------+-------+
148| ``'chromium-browser'`` | :class:`Chromium('chromium-browser')` | |
149+------------------------+-----------------------------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151Notes:
152
153(1)
154 "Konqueror" is the file manager for the KDE desktop environment for Unix, and
155 only makes sense to use if KDE is running. Some way of reliably detecting KDE
156 would be nice; the :envvar:`KDEDIR` variable is not sufficient. Note also that
157 the name "kfm" is used even when using the :program:`konqueror` command with KDE
158 2 --- the implementation selects the best strategy for running Konqueror.
159
160(2)
161 Only on Windows platforms.
162
163(3)
Georg Brandlc575c902008-09-13 17:46:05 +0000164 Only on Mac OS platforms; requires the standard MacPython :mod:`ic` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166(4)
Georg Brandlc575c902008-09-13 17:46:05 +0000167 Only on Mac OS X platform.
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Ezio Melotti25d1bdc2012-01-18 02:32:07 +0200169.. versionadded:: 3.3
170 Support for Chrome/Chromium has been added.
171
Georg Brandl116aa622007-08-15 14:28:22 +0000172Here are some simple examples::
173
Ezio Melotti25d1bdc2012-01-18 02:32:07 +0200174 url = 'http://docs.python.org/'
Georg Brandl116aa622007-08-15 14:28:22 +0000175
Georg Brandl48310cd2009-01-03 21:18:54 +0000176 # Open URL in a new tab, if a browser window is already open.
Ezio Melotti25d1bdc2012-01-18 02:32:07 +0200177 webbrowser.open_new_tab(url)
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179 # Open URL in new window, raising the window if possible.
180 webbrowser.open_new(url)
181
182
183.. _browser-controllers:
184
185Browser Controller Objects
186--------------------------
187
Benjamin Peterson5879d412009-03-30 14:51:56 +0000188Browser controllers provide these methods which parallel three of the
189module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191
Georg Brandl7f01a132009-09-16 15:58:14 +0000192.. method:: controller.open(url, new=0, autoraise=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194 Display *url* using the browser handled by this controller. If *new* is 1, a new
195 browser window is opened if possible. If *new* is 2, a new browser page ("tab")
196 is opened if possible.
197
198
199.. method:: controller.open_new(url)
200
201 Open *url* in a new window of the browser handled by this controller, if
202 possible, otherwise, open *url* in the only browser window. Alias
203 :func:`open_new`.
204
205
206.. method:: controller.open_new_tab(url)
207
208 Open *url* in a new page ("tab") of the browser handled by this controller, if
209 possible, otherwise equivalent to :func:`open_new`.
Georg Brandl8a1e4c42009-05-25 21:13:36 +0000210
211
212.. rubric:: Footnotes
213
214.. [1] Executables named here without a full path will be searched in the
215 directories given in the :envvar:`PATH` environment variable.