I would like to find the code type cells of the particular jupyter notebook and when I find this cell then create new cell after that and copy the google docstring documentation of that cell to the new cell, I did it as follows: with the following code I found the code cells of the "document.ipynb" notebook and saved every cells in the python file.
import json
PATH_TO_NOTEBOOK = "document.ipynb"
def get_notebook(notebook_path):
with open(notebook_path, 'r', encoding='utf-8') as notebook:
return json.load(notebook)
def is_code_cell(cell):
return cell['cell_type'] == "code"
def get_source_from_code_cell(cell):
return ''.join(cell['source'])
def save_as_python_file(filename, code):
with open(f'{filename}.py', 'w', encoding='utf-8') as f:
f.write(code)
def get_code_cells_content(notebook_cells):
yield from (
(i, get_source_from_code_cell(current_cell))
for i, current_cell in enumerate(notebook_cells, 1)
if is_code_cell(current_cell)
)
then with the '!pdoc --html ./*.py -o ./doc' command I created the html files for documentation. then I tried to add the new cell in the source notebook "document.ipynb" after code cell with the following function
def create_new_cell(contents):
from IPython.core.getipython import get_ipython
shell = get_ipython()
payload = dict(
source='set_next_input',
text=contents,
replace=False,
)
shell.payload_manager.write_payload(payload, single=False)
notebook = get_notebook(PATH_TO_NOTEBOOK)
for filename, code in get_code_cells_content(notebook['cells']):
save_as_python_file(filename, code)
!pdoc --html ./*.py -o ./doc
create_new_cell('%load ./doc/*.html ')```
but the new cell was created in the current jupyter notebook not in the
"document.ipynb" how can I add new cell in the source notebook and then
load the html file on that. is there another way that could be more
efficient to create html document for each cell?