Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 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 very high-level interface to |
| 10 | allow displaying Web-based documents to users. The controller objects |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 11 | are easy to use and are platform-independent. Under most |
| 12 | circumstances, simply calling the \function{open()} function from this |
| 13 | module will do the right thing. |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 14 | |
| 15 | Under \UNIX, graphical browsers are preferred under X11, but text-mode |
Fred Drake | f6791f3 | 2000-10-02 03:42:43 +0000 | [diff] [blame] | 16 | browsers will be used if graphical browsers are not available or an X11 |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 17 | display isn't available. If text-mode browsers are used, the calling |
| 18 | process will block until the user exits the browser. |
| 19 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 20 | Under \UNIX, if the environment variable \envvar{BROWSER} exists, it |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 21 | is interpreted to override the platform default list of browsers, as a |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 22 | colon-separated list of browsers to try in order. When the value of |
| 23 | a list part contains the string \code{\%s}, then it is interpreted as |
| 24 | a literal browser command line to be used with the argument URL |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 25 | substituted for the \code{\%s}; if the part does not contain |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 26 | \code{\%s}, it is simply interpreted as the name of the browser to |
| 27 | launch. |
| 28 | |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 29 | For non-\UNIX{} platforms, or when X11 browsers are available on |
| 30 | \UNIX, the controlling process will not wait for the user to finish |
| 31 | with the browser, but allow the browser to maintain its own window on |
| 32 | the display. |
| 33 | |
| 34 | The following exception is defined: |
| 35 | |
| 36 | \begin{excdesc}{Error} |
| 37 | Exception raised when a browser control error occurs. |
| 38 | \end{excdesc} |
| 39 | |
| 40 | The following functions are defined: |
| 41 | |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 42 | \begin{funcdesc}{open}{url\optional{, new=0}\optional{, autoraise=1}} |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 43 | Display \var{url} using the default browser. If \var{new} is true, |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 44 | a new browser window is opened if possible. If \var{autoraise} is |
| 45 | true, the window is raised if possible (note that under many window |
| 46 | managers this will occur regardless of the setting of this variable). |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 47 | \end{funcdesc} |
| 48 | |
| 49 | \begin{funcdesc}{open_new}{url} |
| 50 | Open \var{url} in a new window of the default browser, if possible, |
Fred Drake | 53d285a | 2001-07-19 03:49:33 +0000 | [diff] [blame^] | 51 | otherwise, open \var{url} in the only browser window. |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 52 | \end{funcdesc} |
| 53 | |
| 54 | \begin{funcdesc}{get}{\optional{name}} |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 55 | Return a controller object for the browser type \var{name}. If |
| 56 | \var{name} is empty, return a controller for a default browser |
Eric S. Raymond | aeb5532 | 2001-01-23 13:22:28 +0000 | [diff] [blame] | 57 | appropriate to the caller's environment. |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 58 | \end{funcdesc} |
| 59 | |
Fred Drake | 246f65f | 2000-10-23 15:41:13 +0000 | [diff] [blame] | 60 | \begin{funcdesc}{register}{name, constructor\optional{, instance}} |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 61 | Register the browser type \var{name}. Once a browser type is |
| 62 | registered, the \function{get()} function can return a controller |
| 63 | for that browser type. If \var{instance} is not provided, or is |
| 64 | \code{None}, \var{constructor} will be called without parameters to |
| 65 | create an instance when needed. If \var{instance} is provided, |
| 66 | \var{constructor} will never be called, and may be \code{None}. |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 67 | |
| 68 | This entry point is only useful if you plan to either set the |
| 69 | \envvar{BROWSER} variable or call \function{get} with a nonempty |
| 70 | argument matching the name of a handler you declare. |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 71 | \end{funcdesc} |
| 72 | |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 73 | A number of browser types are predefined. This table gives the type |
| 74 | names that may be passed to the \function{get()} function and the |
| 75 | corresponding instantiations for the controller classes, all defined |
| 76 | in this module. |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 77 | |
| 78 | \begin{tableiii}{l|l|c}{code}{Type Name}{Class Name}{Notes} |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 79 | \lineiii{'mozilla'}{\class{Netscape('mozilla')}}{} |
| 80 | \lineiii{'netscape'}{\class{Netscape('netscape')}}{} |
Barry Warsaw | 21f37e1 | 2001-01-25 00:38:15 +0000 | [diff] [blame] | 81 | \lineiii{'mosaic'}{\class{GenericBrowser('mosaic \%s \&')}}{} |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 82 | \lineiii{'kfm'}{\class{Konqueror()}}{(1)} |
| 83 | \lineiii{'grail'}{\class{Grail()}}{} |
| 84 | \lineiii{'links'}{\class{GenericBrowser('links \%s')}}{} |
| 85 | \lineiii{'lynx'}{\class{GenericBrowser('lynx \%s')}}{} |
| 86 | \lineiii{'w3m'}{\class{GenericBrowser('w3m \%s')}}{} |
Fred Drake | c826ecb | 2000-07-07 17:08:40 +0000 | [diff] [blame] | 87 | \lineiii{'windows-default'}{\class{WindowsDefault}}{(2)} |
| 88 | \lineiii{'internet-config'}{\class{InternetConfig}}{(3)} |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 89 | \end{tableiii} |
| 90 | |
| 91 | \noindent |
| 92 | Notes: |
| 93 | |
| 94 | \begin{description} |
| 95 | \item[(1)] |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 96 | ``Konqueror'' is the file manager for the KDE desktop environment for |
Fred Drake | f6791f3 | 2000-10-02 03:42:43 +0000 | [diff] [blame] | 97 | UNIX, and only makes sense to use if KDE is running. Some way of |
| 98 | reliably detecting KDE would be nice; the \envvar{KDEDIR} variable is |
Fred Drake | 21e036c | 2001-03-26 16:17:21 +0000 | [diff] [blame] | 99 | not sufficient. Note also that the name ``kfm'' is used even when |
| 100 | using the \program{konqueror} command with KDE 2 --- the |
| 101 | implementation selects the best strategy for running Konqueror. |
Fred Drake | c826ecb | 2000-07-07 17:08:40 +0000 | [diff] [blame] | 102 | |
| 103 | \item[(2)] |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 104 | Only on Windows platforms; requires the common |
| 105 | extension modules \module{win32api} and \module{win32con}. |
| 106 | |
Fred Drake | c826ecb | 2000-07-07 17:08:40 +0000 | [diff] [blame] | 107 | \item[(3)] |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 108 | Only on MacOS platforms; requires the standard MacPython \module{ic} |
| 109 | module, described in the \citetitle[../mac/module-ic.html]{Macintosh |
| 110 | Library Modules} manual. |
| 111 | \end{description} |
| 112 | |
| 113 | |
| 114 | \subsection{Browser Controller Objects \label{browser-controllers}} |
| 115 | |
| 116 | Browser controllers provide two methods which parallel two of the |
| 117 | module-level convenience functions: |
| 118 | |
| 119 | \begin{funcdesc}{open}{url\optional{, new}} |
| 120 | Display \var{url} using the browser handled by this controller. If |
| 121 | \var{new} is true, a new browser window is opened if possible. |
| 122 | \end{funcdesc} |
| 123 | |
| 124 | \begin{funcdesc}{open_new}{url} |
| 125 | Open \var{url} in a new window of the browser handled by this |
| 126 | controller, if possible, otherwise, open \var{url} in the only |
Fred Drake | 53d285a | 2001-07-19 03:49:33 +0000 | [diff] [blame^] | 127 | browser window. |
Fred Drake | e4dbb86 | 2000-07-07 03:36:12 +0000 | [diff] [blame] | 128 | \end{funcdesc} |