-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequirements.py
More file actions
40 lines (33 loc) · 1.15 KB
/
requirements.py
File metadata and controls
40 lines (33 loc) · 1.15 KB
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
35
36
37
38
39
40
from nbconvert import PythonExporter
import nbformat
import glob
import os
nb_files = glob.glob(f'{os.getcwd()}/**/*.ipynb',
recursive=True)
py_files = glob.glob(f'{os.getcwd()}/**/*.py',
recursive=True)
def convert_nb(file_name):
export_path = f'{os.getcwd()}/{os.path.split(file_name)[1][:-5]}py'
print(export_path)
with open(file_name, encoding="utf8") as fn:
nb = nbformat.read(fn, as_version=4)
exporter = PythonExporter()
# source is a tuple of python source code
# meta contains metadata
source, meta = exporter.from_notebook_node(nb)
with open(export_path, 'w+', encoding="utf8") as fh:
fh.writelines(source)
def delete_NB_py(file_name):
os.remove(f'{os.getcwd()}/{os.path.split(file_name)[1][:-5]}py')
for file in nb_files:
convert_nb(file)
import importlib
maybe_pipreq = importlib.util.find_spec("pipreqs")
if maybe_pipreq is None:
import subprocess
import sys
subprocess.call([sys.executable, '-m', 'pip', 'install', "pipreqs"])
os.system("pipreqs --force --encoding utf8")
for file in nb_files:
if file not in py_files:
delete_NB_py(file)