3

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?

Maximilian Hils
  • 6,309
  • 3
  • 27
  • 46
Premysl Vorac
  • 473
  • 6
  • 16

2 Answers2

3

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.

Premysl Vorac
  • 473
  • 6
  • 16
  • 1
    I've slightly cleaned, provided command line parameters, and updated to python 3 https://gist.github.com/benman1/e2e507f61856b96217f611f8c22bb474 – ben26941 May 14 '20 at 12:15
0

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
K3---rnc
  • 6,717
  • 3
  • 31
  • 46