使用 pdoc 模块创建一个包含软件包列表的 html 页面?

Create a html page with a list of packages using pdoc module?

我准备了一个简单的代码,它使用 pdoc 模块从 Python(*.py 或 *.pyc)脚本创建一个 html 页面。代码如下:

def make_pdoc():
    import pdoc
    import sys
    from os import path
    libpath = r'C:\path\to\file'

    if path.exists(libpath) and libpath not in sys.path:
        sys.path.append(libpath)
    pdoc.import_path.append(libpath)

    mod = pdoc.import_module('my_script')
    doc = pdoc.Module(mod, allsubmodules=True)
    string = doc.html(external_links=True)
    with open('doc.html', 'w') as html_file:
        html_file.write(string.encode('utf-8'))

if __name__ == '__main__':
    make_pdoc()

我已经准备了几个 html 页面的文档,并想创建一个页面,其中包含指向我创建的所有 html 页面的链接。换句话说,我想创建类似 main pdoc documentation page.

的东西

是否可以使用pdoc模块制作主页?

这是我到目前为止的进展:

def make_pdoc():
    import pdoc
    import sys
    from os import path, makedirs
    libpath = 'C:\path\to\file\'
    if path.exists(libpath):
        sys.path.append(libpath)
    pdoc.import_path.append(libpath)

    mod = pdoc.import_module('package-name-here')
    doc = pdoc.Module(mod, allsubmodules=True)
    string = doc.html(external_links=True)
    # Package level
    with open(doc.name + '/_doc/index.html', 'w') as html_file:
        html_file.write(string.encode('utf-8'))

    # Sublevel 1
    for submodule in doc.submodules():
        string = submodule.html(external_links=True)
        if submodule.is_package():
            exte = '/index.html'
        else:
            exte = '.m.html'
        dpath = (submodule.name.split('.')[0] + '/_doc/' +
                 submodule.name.split('.')[-1]) + '/'
        if not path.exists(dpath):
            makedirs(dpath)
        with open(dpath + exte, 'w') as html_file:
            html_file.write(string.encode('utf-8'))
        # Sublevel 2
        if submodule.submodules():
            for subsubmodule in submodule.submodules():
                print subsubmodule.name
                string = subsubmodule.html(external_links=True)
                if subsubmodule.is_package():
                    exte = '.html'
                else:
                    exte = '.m.html'
                with open(subsubmodule.name.split('.')[0] + '/_doc/' +
                          subsubmodule.name.split('.')[1] + '/' +
                          subsubmodule.name.split('.')[-1] +
                          exte, 'w') as html_file:
                    html_file.write(string.encode('utf-8'))

if __name__ == '__main__':
    make_pdoc()

此代码根据源码包中的树结构在html页中创建目录。

使用最新版本pdoc3 0.5.0, you can follow the example from documentation

import pdoc

files = ['a.py', './b/']  # Can be modules or paths
modules = [pdoc.Module(mod) for mod in files]
pdoc.link_inheritance()

def recursive_htmls(mod):
    yield mod.name, mod.html()
    for submod in mod.submodules():
        yield from recursive_htmls(submod)

for mod in modules:
    for module_name, html in recursive_htmls(mod):
        ...  # Save html to file