File size: 1,166 Bytes
77b5632
 
 
15480d0
 
 
77b5632
 
 
 
 
 
 
 
 
 
 
e551c67
15480d0
77b5632
 
 
 
 
15480d0
77b5632
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
import argparse

#below command for conversion 
# -> python -u "/home/demonhunter/projects/device_detector/converter_update.py" 'tips.ipynb' 

parser = argparse.ArgumentParser(description='Convert .ipynb file to .py file')
parser.add_argument('input_file', help='Path to the input .ipynb file')
args = parser.parse_args()
file = args.input_file

code = json.load(open(file))
filename = file.split(".")[0]
py_file = open(f"{filename}.py", "w+")

for cell in code['cells']:
    if cell['cell_type'] == 'code':
        if cell['source'] and (("#| export" in cell['source'][0]) or("#|export" in cell['source'][0]) ):
            py_file.write("# %%\n")       #you can comment out this line
            for line in cell['source'][1:]:
                py_file.write(line)
        py_file.write("\n")
    elif cell['cell_type'] == 'markdown':
        py_file.write("\n")
        py_file.write("# %%\n")         #you can comment out this line
        for line in cell['source']:
            if line and line[0] == "#":
                py_file.write(line)
            else:
                py_file.write("#* " + line)
        py_file.write("\n")

py_file.close()