/Doc/library/webbrowser.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 208 lines · 148 code · 60 blank · 0 comment · 0 complexity · 34b82fc5ef5bc61b39367fa0fdc56559 MD5 · raw file

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