[Python] Documenting a script step by step with Sphinx

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am documenting a python library with Sphinx.

    I have a couple of simple examples which I'd like to document in a narrative way, something like this:

    #: Import necessary package and define :meth:`make_grid`
    import numpy as np

    def make_grid(a,b):
    """
    Make a grid for constant by piece functions
    """
    x = np.linspace(0,np.pi)
    xmid = (x[:-1]+x[1:])/2
    h = x[1:]-x[:-1]
    return xmid,h

    #: Interpolate a function
    xmid,h = make_grid(0,np.pi)
    y = np.sin(xmid)

    #: Calculate its integral
    I = np.sum(y*h)
    print ("Result %g" % I )


    I would like to generate the corresponding documentation, something like :

    [​IMG]

    Is there any automated way to do so? This would allow me not to duplicate the example script in the documentation. It seems to me this was the object of this old question but in my hands viewcode extension doesn't interpret comments, it just produces an html page with quoted code.

    Continue reading...

Compartilhe esta Página