Caffe2 - Python API
A deep learning, cross platform ML framework
tutorials_to_script_converter.py
1 import os
2 from os import listdir
3 from subprocess import check_call
4 
5 
6 def convert_notebook(notebook_path):
7  # Generate .py file from a notebook
8  notebook_dir, notebook_file_name = os.path.split(notebook_path)
9  check_call(
10  ['jupyter', 'nbconvert', '--to', 'script', notebook_file_name],
11  cwd=notebook_dir,
12  )
13  py_name = os.path.splitext(notebook_file_name)[0] + ".py"
14  full_py_name = os.path.join(notebook_dir, py_name)
15 
16  # Create py_gen/ dir if it doesn't exist and move the file there
17  new_dir = os.path.join(notebook_dir, 'py_gen')
18  if not os.path.exists(new_dir):
19  os.makedirs(new_dir)
20  new_py_location = os.path.join(new_dir, py_name)
21  os.rename(full_py_name, new_py_location)
22  full_py_name = new_py_location
23 
24  with open(full_py_name, 'r') as f:
25  data = f.read()
26  lines = data.split('\n')
27  good_lines = []
28  for line in lines:
29  if ("get_ipython().magic" not in line
30  and "get_ipython().run_line_magic" not in line):
31  good_lines.append(line)
32  # Update the file with do not edit preamble
33  with open(full_py_name, 'w') as f:
34  f.write("#########################################################\n")
35  f.write("#\n")
36  f.write("# DO NOT EDIT THIS FILE. IT IS GENERATED AUTOMATICALLY. #\n")
37  f.write("# PLEASE LOOK INTO THE README FOR MORE INFORMATION. #\n")
38  f.write("#\n")
39  f.write("#########################################################\n")
40  f.write("\n")
41 
42  for line in good_lines:
43  f.write(line + '\n')
44 
45 
46 def main():
47  tutorials_folder = os.path.dirname(os.path.realpath(__file__))
48  print("tutorials_folder: ", tutorials_folder)
49  files = [
50  os.path.join(tutorials_folder, f)
51  for f in listdir(tutorials_folder)
52  if os.path.isfile(os.path.join(tutorials_folder, f))
53  and f.endswith('ipynb')
54  ]
55  for f in files:
56  convert_notebook(f)
57 
58 
59 if __name__ == '__main__':
60  main()