2000-07-07 00:36:12 -03:00
|
|
|
\section{\module{webbrowser} ---
|
|
|
|
Convenient Web-browser controller}
|
|
|
|
|
|
|
|
\declaremodule{standard}{webbrowser}
|
|
|
|
\modulesynopsis{Easy-to-use controller for Web browsers.}
|
|
|
|
\moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
|
|
|
|
\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
|
|
|
|
|
2005-10-03 11:16:44 -03:00
|
|
|
The \module{webbrowser} module provides a high-level interface to
|
|
|
|
allow displaying Web-based documents to users. Under most
|
2001-01-23 09:49:44 -04:00
|
|
|
circumstances, simply calling the \function{open()} function from this
|
|
|
|
module will do the right thing.
|
2000-07-07 00:36:12 -03:00
|
|
|
|
2005-10-04 00:31:01 -03:00
|
|
|
Under \UNIX{}, graphical browsers are preferred under X11, but text-mode
|
2000-10-02 00:42:43 -03:00
|
|
|
browsers will be used if graphical browsers are not available or an X11
|
2000-07-07 00:36:12 -03:00
|
|
|
display isn't available. If text-mode browsers are used, the calling
|
|
|
|
process will block until the user exits the browser.
|
|
|
|
|
2005-10-03 11:16:44 -03:00
|
|
|
If the environment variable \envvar{BROWSER} exists, it
|
2001-01-23 09:49:44 -04:00
|
|
|
is interpreted to override the platform default list of browsers, as a
|
2005-10-03 11:16:44 -03:00
|
|
|
os.pathsep-separated list of browsers to try in order. When the value of
|
2006-01-20 17:03:35 -04:00
|
|
|
a list part contains the string \code{\%s}, then it is
|
|
|
|
interpreted as a literal browser command line to be used with the argument URL
|
|
|
|
substituted for \code{\%s}; if the part does not contain
|
2001-01-23 09:16:32 -04:00
|
|
|
\code{\%s}, it is simply interpreted as the name of the browser to
|
|
|
|
launch.
|
|
|
|
|
2005-10-03 11:16:44 -03:00
|
|
|
For non-\UNIX{} platforms, or when a remote browser is available on
|
2005-10-04 00:31:01 -03:00
|
|
|
\UNIX{}, the controlling process will not wait for the user to finish
|
2005-10-03 11:16:44 -03:00
|
|
|
with the browser, but allow the remote browser to maintain its own
|
2005-10-04 00:31:01 -03:00
|
|
|
windows on the display. If remote browsers are not available on \UNIX{},
|
2005-10-03 11:16:44 -03:00
|
|
|
the controlling process will launch a new browser and wait.
|
|
|
|
|
|
|
|
The script \program{webbrowser} can be used as a command-line interface
|
|
|
|
for the module. It accepts an URL as the argument. It accepts the following
|
|
|
|
optional parameters: \programopt{-n} opens the URL in a new browser window,
|
|
|
|
if possible; \programopt{-t} opens the URL in a new browser page ("tab"). The
|
|
|
|
options are, naturally, mutually exclusive.
|
2000-07-07 00:36:12 -03:00
|
|
|
|
|
|
|
The following exception is defined:
|
|
|
|
|
|
|
|
\begin{excdesc}{Error}
|
|
|
|
Exception raised when a browser control error occurs.
|
|
|
|
\end{excdesc}
|
|
|
|
|
|
|
|
The following functions are defined:
|
|
|
|
|
2005-10-04 00:31:01 -03:00
|
|
|
\begin{funcdesc}{open}{url\optional{, new=0\optional{, autoraise=1}}}
|
2005-10-03 11:16:44 -03:00
|
|
|
Display \var{url} using the default browser. If \var{new} is 0, the
|
|
|
|
\var{url} is opened in the same browser window. If \var{new} is 1,
|
|
|
|
a new browser window is opened if possible. If \var{new} is 2,
|
|
|
|
a new browser page ("tab") is opened if possible. If \var{autoraise} is
|
2001-01-23 09:49:44 -04:00
|
|
|
true, the window is raised if possible (note that under many window
|
|
|
|
managers this will occur regardless of the setting of this variable).
|
2006-01-20 17:03:35 -04:00
|
|
|
\versionchanged[\var{new} can now be 2]{2.5}
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2005-10-04 00:31:01 -03:00
|
|
|
\begin{funcdesc}{open_new}{url}
|
2000-07-07 00:36:12 -03:00
|
|
|
Open \var{url} in a new window of the default browser, if possible,
|
2005-10-04 00:31:01 -03:00
|
|
|
otherwise, open \var{url} in the only browser window.
|
2005-10-03 11:16:44 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{open_new_tab}{url}
|
|
|
|
Open \var{url} in a new page ("tab") of the default browser, if possible,
|
2005-10-04 00:31:01 -03:00
|
|
|
otherwise equivalent to \function{open_new}.
|
|
|
|
\versionadded{2.5}
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{get}{\optional{name}}
|
2001-01-23 09:16:32 -04:00
|
|
|
Return a controller object for the browser type \var{name}. If
|
|
|
|
\var{name} is empty, return a controller for a default browser
|
2001-01-23 09:22:28 -04:00
|
|
|
appropriate to the caller's environment.
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-10-23 12:41:13 -03:00
|
|
|
\begin{funcdesc}{register}{name, constructor\optional{, instance}}
|
2000-07-07 00:36:12 -03:00
|
|
|
Register the browser type \var{name}. Once a browser type is
|
|
|
|
registered, the \function{get()} function can return a controller
|
|
|
|
for that browser type. If \var{instance} is not provided, or is
|
|
|
|
\code{None}, \var{constructor} will be called without parameters to
|
|
|
|
create an instance when needed. If \var{instance} is provided,
|
|
|
|
\var{constructor} will never be called, and may be \code{None}.
|
2001-01-23 09:16:32 -04:00
|
|
|
|
|
|
|
This entry point is only useful if you plan to either set the
|
|
|
|
\envvar{BROWSER} variable or call \function{get} with a nonempty
|
2005-10-03 11:16:44 -03:00
|
|
|
argument matching the name of a handler you declare.
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2001-01-23 09:49:44 -04:00
|
|
|
A number of browser types are predefined. This table gives the type
|
|
|
|
names that may be passed to the \function{get()} function and the
|
|
|
|
corresponding instantiations for the controller classes, all defined
|
|
|
|
in this module.
|
2000-07-07 00:36:12 -03:00
|
|
|
|
|
|
|
\begin{tableiii}{l|l|c}{code}{Type Name}{Class Name}{Notes}
|
2005-10-03 11:16:44 -03:00
|
|
|
\lineiii{'mozilla'}{\class{Mozilla('mozilla')}}{}
|
|
|
|
\lineiii{'firefox'}{\class{Mozilla('mozilla')}}{}
|
|
|
|
\lineiii{'netscape'}{\class{Mozilla('netscape')}}{}
|
|
|
|
\lineiii{'galeon'}{\class{Galeon('galeon')}}{}
|
|
|
|
\lineiii{'epiphany'}{\class{Galeon('epiphany')}}{}
|
2006-01-20 17:03:35 -04:00
|
|
|
\lineiii{'skipstone'}{\class{BackgroundBrowser('skipstone')}}{}
|
|
|
|
\lineiii{'kfmclient'}{\class{Konqueror()}}{(1)}
|
2005-10-03 11:16:44 -03:00
|
|
|
\lineiii{'konqueror'}{\class{Konqueror()}}{(1)}
|
2001-01-23 09:49:44 -04:00
|
|
|
\lineiii{'kfm'}{\class{Konqueror()}}{(1)}
|
2006-01-20 17:03:35 -04:00
|
|
|
\lineiii{'mosaic'}{\class{BackgroundBrowser('mosaic')}}{}
|
2005-10-03 11:16:44 -03:00
|
|
|
\lineiii{'opera'}{\class{Opera()}}{}
|
2001-01-23 09:49:44 -04:00
|
|
|
\lineiii{'grail'}{\class{Grail()}}{}
|
2006-01-20 17:03:35 -04:00
|
|
|
\lineiii{'links'}{\class{GenericBrowser('links')}}{}
|
2005-10-03 11:16:44 -03:00
|
|
|
\lineiii{'elinks'}{\class{Elinks('elinks')}}{}
|
2006-01-20 17:03:35 -04:00
|
|
|
\lineiii{'lynx'}{\class{GenericBrowser('lynx')}}{}
|
|
|
|
\lineiii{'w3m'}{\class{GenericBrowser('w3m')}}{}
|
2000-07-07 14:08:40 -03:00
|
|
|
\lineiii{'windows-default'}{\class{WindowsDefault}}{(2)}
|
|
|
|
\lineiii{'internet-config'}{\class{InternetConfig}}{(3)}
|
2005-10-03 11:16:44 -03:00
|
|
|
\lineiii{'macosx'}{\class{MacOSX('default')}}{(4)}
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{tableiii}
|
|
|
|
|
|
|
|
\noindent
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
\begin{description}
|
|
|
|
\item[(1)]
|
2001-01-23 09:49:44 -04:00
|
|
|
``Konqueror'' is the file manager for the KDE desktop environment for
|
2005-10-04 00:31:01 -03:00
|
|
|
\UNIX{}, and only makes sense to use if KDE is running. Some way of
|
2000-10-02 00:42:43 -03:00
|
|
|
reliably detecting KDE would be nice; the \envvar{KDEDIR} variable is
|
2001-03-26 12:17:21 -04:00
|
|
|
not sufficient. Note also that the name ``kfm'' is used even when
|
|
|
|
using the \program{konqueror} command with KDE 2 --- the
|
|
|
|
implementation selects the best strategy for running Konqueror.
|
2000-07-07 14:08:40 -03:00
|
|
|
|
|
|
|
\item[(2)]
|
2005-10-03 11:16:44 -03:00
|
|
|
Only on Windows platforms.
|
2000-07-07 00:36:12 -03:00
|
|
|
|
2000-07-07 14:08:40 -03:00
|
|
|
\item[(3)]
|
2000-07-07 00:36:12 -03:00
|
|
|
Only on MacOS platforms; requires the standard MacPython \module{ic}
|
|
|
|
module, described in the \citetitle[../mac/module-ic.html]{Macintosh
|
|
|
|
Library Modules} manual.
|
2005-10-03 11:16:44 -03:00
|
|
|
|
|
|
|
\item[(4)]
|
|
|
|
Only on MacOS X platform.
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{description}
|
|
|
|
|
2006-07-28 09:32:43 -03:00
|
|
|
Here are some simple examples:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
url = 'http://www.python.org'
|
|
|
|
|
|
|
|
# Open URL in a new tab, if a browser window is already open.
|
|
|
|
webbrowser.open_new_tab(url + '/doc')
|
|
|
|
|
|
|
|
# Open URL in new window, raising the window if possible.
|
|
|
|
webbrowser.open_new(url)
|
|
|
|
\end{verbatim}
|
|
|
|
|
2000-07-07 00:36:12 -03:00
|
|
|
|
|
|
|
\subsection{Browser Controller Objects \label{browser-controllers}}
|
|
|
|
|
|
|
|
Browser controllers provide two methods which parallel two of the
|
|
|
|
module-level convenience functions:
|
|
|
|
|
2005-10-04 00:31:01 -03:00
|
|
|
\begin{funcdesc}{open}{url\optional{, new\optional{, autoraise=1}}}
|
2005-10-03 11:16:44 -03:00
|
|
|
Display \var{url} using the browser handled by this controller.
|
|
|
|
If \var{new} is 1, a new browser window is opened if possible.
|
|
|
|
If \var{new} is 2, a new browser page ("tab") is opened if possible.
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2005-10-04 00:31:01 -03:00
|
|
|
\begin{funcdesc}{open_new}{url}
|
2000-07-07 00:36:12 -03:00
|
|
|
Open \var{url} in a new window of the browser handled by this
|
|
|
|
controller, if possible, otherwise, open \var{url} in the only
|
2005-10-03 11:16:44 -03:00
|
|
|
browser window. Alias \function{open_new}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{open_new_tab}{url}
|
|
|
|
Open \var{url} in a new page ("tab") of the browser handled by this
|
2005-10-04 00:31:01 -03:00
|
|
|
controller, if possible, otherwise equivalent to \function{open_new}.
|
|
|
|
\versionadded{2.5}
|
2000-07-07 00:36:12 -03:00
|
|
|
\end{funcdesc}
|