pydoc を使用して html ファイルを作成し、デフォルトのブラウザーに表示するモジュール名を含む文字列を受け取る短い関数を作成しました。
Web サーバーをセットアップするのではなく、ファイルを Web ブラウザーにロードするだけです。
import subprocess
import os
import tempfile
import sys
import time
def show_help(module):
""" Create and show HTML page of module documentation
Pass module as a string
"""
# Create temporary directory to store documentation in
with tempfile.TemporaryDirectory() as temp_folder:
orignal_cwd = os.getcwd()
# Change working directory to temporary folder
os.chdir(temp_folder)
# Create HTML page of doc
subprocess.call(["pydoc", "-w", module], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
filepath = os.path.join(temp_folder, module + ".html")
# Reset working directory
os.chdir(orignal_cwd)
# Open default program to view HTML files
if sys.platform.startswith('darwin'):
subprocess.call(('open', filepath), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
elif os.name == 'nt':
os.startfile(filepath)
elif os.name == 'posix':
subprocess.call(('xdg-open', filepath), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Sleep for 2 seconds so browser can open before deleting file
time.sleep(2)
Linuxでpython3.4でテストしましたが、正常に動作しますが、Windowsではチェックしていません。
これをモジュールとしてインタープリターにインポートしてから、「 module nameshow_help("module name")
」が文字列としてのモジュール名であるドキュメントを表示するために使用できます。など。show_help("os")