1 | \section{\module{webbrowser} ---
|
---|
2 | Convenient Web-browser controller}
|
---|
3 |
|
---|
4 | \declaremodule{standard}{webbrowser}
|
---|
5 | \modulesynopsis{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 | The \module{webbrowser} module provides a high-level interface to
|
---|
10 | allow displaying Web-based documents to users. Under most
|
---|
11 | circumstances, simply calling the \function{open()} function from this
|
---|
12 | module will do the right thing.
|
---|
13 |
|
---|
14 | Under \UNIX{}, graphical browsers are preferred under X11, but text-mode
|
---|
15 | browsers will be used if graphical browsers are not available or an X11
|
---|
16 | display isn't available. If text-mode browsers are used, the calling
|
---|
17 | process will block until the user exits the browser.
|
---|
18 |
|
---|
19 | If the environment variable \envvar{BROWSER} exists, it
|
---|
20 | is interpreted to override the platform default list of browsers, as a
|
---|
21 | os.pathsep-separated list of browsers to try in order. When the value of
|
---|
22 | a list part contains the string \code{\%s}, then it is
|
---|
23 | interpreted as a literal browser command line to be used with the argument URL
|
---|
24 | substituted for \code{\%s}; if the part does not contain
|
---|
25 | \code{\%s}, it is simply interpreted as the name of the browser to
|
---|
26 | launch.
|
---|
27 |
|
---|
28 | For non-\UNIX{} platforms, or when a remote browser is available on
|
---|
29 | \UNIX{}, the controlling process will not wait for the user to finish
|
---|
30 | with the browser, but allow the remote browser to maintain its own
|
---|
31 | windows on the display. If remote browsers are not available on \UNIX{},
|
---|
32 | the controlling process will launch a new browser and wait.
|
---|
33 |
|
---|
34 | The script \program{webbrowser} can be used as a command-line interface
|
---|
35 | for the module. It accepts an URL as the argument. It accepts the following
|
---|
36 | optional parameters: \programopt{-n} opens the URL in a new browser window,
|
---|
37 | if possible; \programopt{-t} opens the URL in a new browser page ("tab"). The
|
---|
38 | options are, naturally, mutually exclusive.
|
---|
39 |
|
---|
40 | The following exception is defined:
|
---|
41 |
|
---|
42 | \begin{excdesc}{Error}
|
---|
43 | Exception raised when a browser control error occurs.
|
---|
44 | \end{excdesc}
|
---|
45 |
|
---|
46 | The following functions are defined:
|
---|
47 |
|
---|
48 | \begin{funcdesc}{open}{url\optional{, new=0\optional{, autoraise=1}}}
|
---|
49 | Display \var{url} using the default browser. If \var{new} is 0, the
|
---|
50 | \var{url} is opened in the same browser window. If \var{new} is 1,
|
---|
51 | a new browser window is opened if possible. If \var{new} is 2,
|
---|
52 | a new browser page ("tab") is opened if possible. If \var{autoraise} is
|
---|
53 | true, the window is raised if possible (note that under many window
|
---|
54 | managers this will occur regardless of the setting of this variable).
|
---|
55 | \versionchanged[\var{new} can now be 2]{2.5}
|
---|
56 | \end{funcdesc}
|
---|
57 |
|
---|
58 | \begin{funcdesc}{open_new}{url}
|
---|
59 | Open \var{url} in a new window of the default browser, if possible,
|
---|
60 | otherwise, open \var{url} in the only browser window.
|
---|
61 | \end{funcdesc}
|
---|
62 |
|
---|
63 | \begin{funcdesc}{open_new_tab}{url}
|
---|
64 | Open \var{url} in a new page ("tab") of the default browser, if possible,
|
---|
65 | otherwise equivalent to \function{open_new}.
|
---|
66 | \versionadded{2.5}
|
---|
67 | \end{funcdesc}
|
---|
68 |
|
---|
69 | \begin{funcdesc}{get}{\optional{name}}
|
---|
70 | Return a controller object for the browser type \var{name}. If
|
---|
71 | \var{name} is empty, return a controller for a default browser
|
---|
72 | appropriate to the caller's environment.
|
---|
73 | \end{funcdesc}
|
---|
74 |
|
---|
75 | \begin{funcdesc}{register}{name, constructor\optional{, instance}}
|
---|
76 | Register the browser type \var{name}. Once a browser type is
|
---|
77 | registered, the \function{get()} function can return a controller
|
---|
78 | for that browser type. If \var{instance} is not provided, or is
|
---|
79 | \code{None}, \var{constructor} will be called without parameters to
|
---|
80 | create an instance when needed. If \var{instance} is provided,
|
---|
81 | \var{constructor} will never be called, and may be \code{None}.
|
---|
82 |
|
---|
83 | This entry point is only useful if you plan to either set the
|
---|
84 | \envvar{BROWSER} variable or call \function{get} with a nonempty
|
---|
85 | argument matching the name of a handler you declare.
|
---|
86 | \end{funcdesc}
|
---|
87 |
|
---|
88 | A number of browser types are predefined. This table gives the type
|
---|
89 | names that may be passed to the \function{get()} function and the
|
---|
90 | corresponding instantiations for the controller classes, all defined
|
---|
91 | in this module.
|
---|
92 |
|
---|
93 | \begin{tableiii}{l|l|c}{code}{Type Name}{Class Name}{Notes}
|
---|
94 | \lineiii{'mozilla'}{\class{Mozilla('mozilla')}}{}
|
---|
95 | \lineiii{'firefox'}{\class{Mozilla('mozilla')}}{}
|
---|
96 | \lineiii{'netscape'}{\class{Mozilla('netscape')}}{}
|
---|
97 | \lineiii{'galeon'}{\class{Galeon('galeon')}}{}
|
---|
98 | \lineiii{'epiphany'}{\class{Galeon('epiphany')}}{}
|
---|
99 | \lineiii{'skipstone'}{\class{BackgroundBrowser('skipstone')}}{}
|
---|
100 | \lineiii{'kfmclient'}{\class{Konqueror()}}{(1)}
|
---|
101 | \lineiii{'konqueror'}{\class{Konqueror()}}{(1)}
|
---|
102 | \lineiii{'kfm'}{\class{Konqueror()}}{(1)}
|
---|
103 | \lineiii{'mosaic'}{\class{BackgroundBrowser('mosaic')}}{}
|
---|
104 | \lineiii{'opera'}{\class{Opera()}}{}
|
---|
105 | \lineiii{'grail'}{\class{Grail()}}{}
|
---|
106 | \lineiii{'links'}{\class{GenericBrowser('links')}}{}
|
---|
107 | \lineiii{'elinks'}{\class{Elinks('elinks')}}{}
|
---|
108 | \lineiii{'lynx'}{\class{GenericBrowser('lynx')}}{}
|
---|
109 | \lineiii{'w3m'}{\class{GenericBrowser('w3m')}}{}
|
---|
110 | \lineiii{'windows-default'}{\class{WindowsDefault}}{(2)}
|
---|
111 | \lineiii{'internet-config'}{\class{InternetConfig}}{(3)}
|
---|
112 | \lineiii{'macosx'}{\class{MacOSX('default')}}{(4)}
|
---|
113 | \end{tableiii}
|
---|
114 |
|
---|
115 | \noindent
|
---|
116 | Notes:
|
---|
117 |
|
---|
118 | \begin{description}
|
---|
119 | \item[(1)]
|
---|
120 | ``Konqueror'' is the file manager for the KDE desktop environment for
|
---|
121 | \UNIX{}, and only makes sense to use if KDE is running. Some way of
|
---|
122 | reliably detecting KDE would be nice; the \envvar{KDEDIR} variable is
|
---|
123 | not sufficient. Note also that the name ``kfm'' is used even when
|
---|
124 | using the \program{konqueror} command with KDE 2 --- the
|
---|
125 | implementation selects the best strategy for running Konqueror.
|
---|
126 |
|
---|
127 | \item[(2)]
|
---|
128 | Only on Windows platforms.
|
---|
129 |
|
---|
130 | \item[(3)]
|
---|
131 | Only on MacOS platforms; requires the standard MacPython \module{ic}
|
---|
132 | module, described in the \citetitle[../mac/module-ic.html]{Macintosh
|
---|
133 | Library Modules} manual.
|
---|
134 |
|
---|
135 | \item[(4)]
|
---|
136 | Only on MacOS X platform.
|
---|
137 | \end{description}
|
---|
138 |
|
---|
139 | Here are some simple examples:
|
---|
140 |
|
---|
141 | \begin{verbatim}
|
---|
142 | url = 'http://www.python.org'
|
---|
143 |
|
---|
144 | # Open URL in a new tab, if a browser window is already open.
|
---|
145 | webbrowser.open_new_tab(url + '/doc')
|
---|
146 |
|
---|
147 | # Open URL in new window, raising the window if possible.
|
---|
148 | webbrowser.open_new(url)
|
---|
149 | \end{verbatim}
|
---|
150 |
|
---|
151 |
|
---|
152 | \subsection{Browser Controller Objects \label{browser-controllers}}
|
---|
153 |
|
---|
154 | Browser controllers provide two methods which parallel two of the
|
---|
155 | module-level convenience functions:
|
---|
156 |
|
---|
157 | \begin{funcdesc}{open}{url\optional{, new\optional{, autoraise=1}}}
|
---|
158 | Display \var{url} using the browser handled by this controller.
|
---|
159 | If \var{new} is 1, a new browser window is opened if possible.
|
---|
160 | If \var{new} is 2, a new browser page ("tab") is opened if possible.
|
---|
161 | \end{funcdesc}
|
---|
162 |
|
---|
163 | \begin{funcdesc}{open_new}{url}
|
---|
164 | Open \var{url} in a new window of the browser handled by this
|
---|
165 | controller, if possible, otherwise, open \var{url} in the only
|
---|
166 | browser window. Alias \function{open_new}.
|
---|
167 | \end{funcdesc}
|
---|
168 |
|
---|
169 | \begin{funcdesc}{open_new_tab}{url}
|
---|
170 | Open \var{url} in a new page ("tab") of the browser handled by this
|
---|
171 | controller, if possible, otherwise equivalent to \function{open_new}.
|
---|
172 | \versionadded{2.5}
|
---|
173 | \end{funcdesc}
|
---|