简体   繁体   中英

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

I have prepared a simple code that creates an html page from a Python (*.py, or *.pyc) script using the pdoc module. The code is the following:

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()

I have prepared several html pages with the documentation and would like to create one page with links to all html pages I've created. In other words, I would like to create something like the main pdoc documentation page .

Is it possible to make the main page using the pdoc module?

This is where I've got so far:

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()

This code creates directories in html pages according to tree structure in source package.

With the latest version of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM