Computational notebooks are popular tools for data scientists, especially for exploratory data analysis. This iterative process can lead to messy notebooks. As the amount of code grows, many developers struggle to keep track of their experimentation, resulting in confusion about how results were obtained. We propose using program slicing to help developers identify the code statements that may be relevant to the variables at a particular point in the program. Once the program slice is isolated, developers can use it to debug a fault in their code, or they may want to extract the slice and copy it to a new, clean notebook.
Check out the demo here.
- Create the environment from the
environment.ymlfile. - Activate the environment.
- Run JupyterLab.
conda env create -f environment.yml
conda activate python-program-slicing
jupyter labTo create an execution log from your Jupyter Notebook execution, use the following custom IPython Magics.
fromIPython.core.magicimportregister_cell_magic@register_cell_magicdefwrite_and_run(line, cell):
"""Write the contents of a cell to a file, but only if the execution succeeds without error. -a is an optional parameter, which opens the file in append mode. e.g., %%write_and_run -a execution_log.py """argz=line.split()
file=argz[-1]
mode='w'iflen(argz) ==2andargz[0] =='-a':
mode='a'result=get_ipython().run_cell(cell)
ifresult.error_in_execisNone:
withopen(file, mode) asf:
f.write(cell)Then, at the beginning of every cell, use the magics command.
%%write_and_run-aexecution_log.pyfromSlicerHelpersimport*# print execution log from your Jupyter notebookprint_execution_log('execution_log.py')
# generate backwards slice generate_backwards_slice('execution_log.py', 13)