0

How can one convert functions in a directory of ipynb files into individual .py files? This would make it easy to import after tests in a monolithic jupyter notebook.

soerface
  • 6,417
  • 6
  • 29
  • 50
reservoirinvest
  • 1,463
  • 2
  • 16
  • 32

3 Answers3

1

Here is the code for that....

# program to convert functions in cells to indivdiual .py files
# the first line of the cell containing function to be converted should have
# filename.py

import json
from os import listdir

fs = listdir() # get the filenames

# make an exclusion list
magics = %lsmagic
magic_words = [s for s in str(magics).split(' ') if s not in '' if '%' in s[0]]
magic_words = [s for s in magic_words if s != '%']
exclude = ['util.startLoop()'] + magic_words

# Ignore ipynb files beginning with '_'
ipyfilelist = [f for f in fs if f[-6:].upper() == '.IPYNB' if f[0] != '_']

code_cells = []
for file in ipyfilelist:
    with open(file) as datafile:
        code = json.load(datafile)
        code_cells.append([cell['source'] for cell in code['cells'] if cell['cell_type'] == 'code'])

codes = [cell for cells in code_cells for cell in cells if cell]

code_dict = {pycode[0][2:-1]:pycode for pycode in codes if pycode[0][-4:] == '.py\n'}

for k, v in code_dict.items():
    with open(k, 'w') as f:
        for line in v:
            if not any(word in line for word in exclude):
                f.write(line)
reservoirinvest
  • 1,463
  • 2
  • 16
  • 32
0

Here is a more verbose approach but options to capture only function definitions that start with keyword def

    import sys,json
#credit https://stackoverflow.com/questions/17077494/how-do-i-convert-a-ipython-notebook-into-a-python-file-via-commandline?rq=1
def convert_ipynb(in_file,out_file,options='functions'):
    """converts an ipynb file with option for only writing function definitions
    """
    f = open(in_file,'r')  
    j = json.load(f);f.close()
    of = open(out_file, 'w')  
    function_block=False #flag for function definition block of lines
    if j["nbformat"] >=4:
            for i,cell in enumerate(j["cells"]):
                    of.write("#cell "+str(i)+"\n")
                    #print ("#cell "+str(i)+"\n")
                    function_block=False
                    for line in cell["source"]:
                        if 'functions' in options: #filter and write only function definitons
                            if (line.startswith('def'))  : #toggle function block flag and write to file
                               # print(line)
                                function_block=True
                                of.write(line)
                                continue #skip rest of this loop as def keyword detected
                            if (not (line.startswith ((' ','/t','#')))): #any char other than space,tab,# ends block
                                function_block=False
                            if  function_block :  #if function block is true write line
                                of.write(line)        
                               
                        else:#functions filtering string not in options so just save all lines
                            of.write(line)
                    of.write('\n\n')
    else:
            for i,cell in enumerate(j["worksheets"][0]["cells"]):
                    of.write("#cell "+str(i)+"\n")
                    for line in cell["input"]:
                            of.write(line)
                    of.write('\n\n')

    of.close()
user15420598
  • 107
  • 7
0

You can also use magic writefile. For documentation see magic ipython.

For instance if you execute the following cell:

%%writefile my_function.py
def my_function(x):
    return x + 2

You will obtain

Writing my_function.py

And the file my_function.py will be in your current working directory.

You can also save the whole notebook selecting File/Download as/Python (py)

Hermes Morales
  • 593
  • 6
  • 17