0

I have a code that you can select a jupyter notebook file(by panel) then this code put every cells in seperate python files, for example cell number 1 in 1.py cell number 2 in 2.py and..., after that with pdoc command we create document for each python file(in fact each cell) and then by creating new cells at the end of that jupyter notebook, we add generated document on those cells, below is the for loop to iterate every cell and call related functions. the issue is that for some cells that contains for instance just test() or some annotation like @property, pdoc command doesn't have result and we got an error like 2.py connot import and then process getting stop and remaining cells which already put in separate python file can not be documented. How to fix this so that if for example cell 2(2.py) has an error, it goes to the next cell or file(cell3 or 3.py) and does not stop the for loop completely.

for file in files.value:
        source = file
        dest = add_suffix(source, " documented")
        dest1 = "/home/dqm/pydev/notebook_copy"
        shutil.copy(source,dest)
        document_note = shutil.copy(dest,dest1)
        os.remove(dest)
        notebook = get_notebook(document_note)
        for filename, code in get_code_cells_content(notebook['cells']):
            save_as_python_file(filename, code)
            print('documentation starting')
            #!pdoc --html ./'{filename}'.py -o ./notebook_copy/document
            
            res = Popen(["pdoc", "--html", f"./{filename}.py", "-o", "./notebook_copy/document"], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate() # template
            status.value += res[0].decode("utf-8") + "\n"
            status.value += res[1] .decode("utf-8") + "\n"
            
            #!pdoc --output-dir=./notebook_copy/document ./'{filename}'.py
            
            res = Popen(["pdoc", "--output-dir=./notebook_copy/document", f"./{filename}.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate() # template
            status.value += res[0].decode("utf-8") + "\n"
            status.value += res[1] .decode("utf-8") + "\n"
            
            create_new_cell(document_note,filename)
            os.remove(f'{filename}.py')
            remove("./notebook_copy/document")
            display(pn.widgets.StaticText(name='documented', value='succesfully'))
        pass # call here the documentation creator

Thanks in advance

I treid to put try except but it didn't work

Samira
  • 1
  • 1
  • I think nbdev and quarto already do what you are trying to write. See [comment exchange in response to 'Generate documentation for python functions in a jupyter notebook using docstrings'](https://stackoverflow.com/questions/75604314/generate-documentation-for-python-functions-in-a-jupyter-notebook-using-docstrin/75606006#comment133409037_75606006). Hiran wrote, "Thank you very much. Used Quarto for the task and it worked". – Wayne Mar 06 '23 at 21:42
  • @Wane Thank you for the comment, actually this is a task for me to document in this way, I've added try and except in pdoc command and it goes through all the cells but because of some errors like: ": AttributeError: partially initialized module \'pandas\' has no attribute \'tslib\' (most likely due to a circular import)\" there is no result for documentation and in the end I just have a name of module in documentation file without function documentation – Samira Mar 08 '23 at 10:12
  • Sure. A big part of why I added it as a comment was for others following in your footsteps. It may help someone else ending up here while looking into how to use notebooks to make code documentation to know there is a more current and mature project that handles this without need for customization. (And speaking of customization... since you are customizing your solution anyway, you could customize handing of those errors to fix them to a better standard than the first pass with pdoc does.) – Wayne Mar 08 '23 at 13:44

0 Answers0