Skip to content

Commit 98bb2af

Browse files
authored
Add files via upload
1 parent 416ae23 commit 98bb2af

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

note2py.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
convert all notebooks to python files in a given directory
3+
4+
Given the following files,
5+
6+
/folder/file.ipynb
7+
/folder/file.ipynb
8+
9+
note2py creates
10+
11+
/folder/python_files/1.py
12+
/folder/python_files/2.py
13+
14+
'''
15+
16+
import os
17+
import nbformat
18+
19+
def extract_code_from_ipynb(notebook_path):
20+
"""
21+
Extracts code cells from a Jupyter notebook file.
22+
23+
Parameters:
24+
- notebook_path: Path to the .ipynb file
25+
26+
Returns:
27+
- code: A string containing all the code from the notebook's code cells
28+
"""
29+
with open(notebook_path, 'r', encoding='utf-8') as f:
30+
nb = nbformat.read(f, as_version=4)
31+
32+
code = ""
33+
for cell in nb.cells:
34+
if cell.cell_type == 'code':
35+
code += cell.source + '\n\n'
36+
37+
return code
38+
39+
def convert_ipynb_to_py(directory):
40+
"""
41+
Converts all .ipynb files in the given directory to .py files containing only code cells.
42+
43+
Parameters:
44+
- directory: Path to the directory containing .ipynb files
45+
"""
46+
output_dir = os.path.join(directory, 'python_files')
47+
if not os.path.exists(output_dir):
48+
os.makedirs(output_dir)
49+
50+
file_counter = 1
51+
for root, _, files in os.walk(directory):
52+
for file in files:
53+
if file.endswith('.ipynb'):
54+
notebook_path = os.path.join(root, file)
55+
code = extract_code_from_ipynb(notebook_path)
56+
if code.strip(): # Only create a .py file if there is code
57+
py_file_path = os.path.join(output_dir, f"{file_counter}.py")
58+
with open(py_file_path, 'w', encoding='utf-8') as f:
59+
f.write(code)
60+
print(f"Created {py_file_path}")
61+
file_counter += 1
62+
63+
if __name__ == "__main__":
64+
directory = input("Enter the directory containing .ipynb files: ")
65+
convert_ipynb_to_py(directory)

removetext.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
removes the specified text from all the notebooks in the
3+
given directory
4+
5+
removetext.py requests the folder path and text to be
6+
removed from each notebook file in the given folder
7+
'''
8+
import os
9+
import json
10+
11+
def remove_text_in_code_cells(file_path, text_to_remove):
12+
"""
13+
Remove the specified text from code cells in the given .ipynb file.
14+
15+
Parameters:
16+
- file_path: Path to the .ipynb file
17+
- text_to_remove: Text to remove from code cells
18+
"""
19+
with open(file_path, 'r', encoding='utf-8') as f:
20+
content = json.load(f)
21+
22+
modified = False
23+
for cell in content.get('cells', []):
24+
if cell.get('cell_type') == 'code':
25+
source = cell.get('source', [])
26+
modified_source = [line.replace(text_to_remove, '') for line in source]
27+
if modified_source != source:
28+
cell['source'] = modified_source
29+
modified = True
30+
31+
if modified:
32+
with open(file_path, 'w', encoding='utf-8') as f:
33+
json.dump(content, f, indent=2)
34+
print(f"Removed text '{text_to_remove}' in {file_path}")
35+
else:
36+
print(f"No changes made to {file_path}")
37+
38+
def process_directory(directory, text_to_remove):
39+
"""
40+
Recursively process all .ipynb files in the given directory to remove the specified text from code cells.
41+
42+
Parameters:
43+
- directory: Path to the directory to be processed
44+
- text_to_remove: Text to remove from code cells
45+
"""
46+
for root, _, files in os.walk(directory):
47+
for file in files:
48+
if file.endswith('.ipynb'):
49+
file_path = os.path.join(root, file)
50+
remove_text_in_code_cells(file_path, text_to_remove)
51+
52+
if __name__ == "__main__":
53+
directory = input("Enter the directory to process: ")
54+
text_to_remove = input("Enter the text to remove from code cells: ")
55+
56+
process_directory(directory, text_to_remove)

replacepy.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''
2+
replace the old text with new specified text for all
3+
the notebooks in the given directory
4+
5+
replacepy.py requests the folder path, old and new text from
6+
the user
7+
'''
8+
import os
9+
import json
10+
11+
def replace_in_ipynb(file_path, old_str, new_str):
12+
"""
13+
Replace all occurrences of old_str with new_str in the given .ipynb file.
14+
15+
Parameters:
16+
- file_path: Path to the .ipynb file
17+
- old_str: String to be replaced
18+
- new_str: String to replace with
19+
"""
20+
with open(file_path, 'r', encoding='utf-8') as f:
21+
content = json.load(f)
22+
23+
modified = False
24+
for cell in content.get('cells', []):
25+
if cell.get('cell_type') == 'code':
26+
source = cell.get('source', [])
27+
modified_source = [line.replace(old_str, new_str) for line in source]
28+
if modified_source != source:
29+
cell['source'] = modified_source
30+
modified = True
31+
32+
if modified:
33+
with open(file_path, 'w', encoding='utf-8') as f:
34+
json.dump(content, f, indent=2)
35+
print(f"Replaced '{old_str}' with '{new_str}' in {file_path}")
36+
else:
37+
print(f"No changes made to {file_path}")
38+
39+
def process_directory(directory, old_str, new_str):
40+
"""
41+
Recursively process all .ipynb files in the given directory.
42+
43+
Parameters:
44+
- directory: Path to the directory to be processed
45+
- old_str: String to be replaced
46+
- new_str: String to replace with
47+
"""
48+
for root, _, files in os.walk(directory):
49+
for file in files:
50+
if file.endswith('.ipynb'):
51+
file_path = os.path.join(root, file)
52+
replace_in_ipynb(file_path, old_str, new_str)
53+
54+
if __name__ == "__main__":
55+
directory = input("Enter the directory to process: ")
56+
old_str = input("Input the old string ")
57+
new_str = input("Input the new string ")
58+
59+
process_directory(directory, old_str, new_str)

uninstall_py_packages.bat

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
REM a batch script to uninstall python packages from a given environment
2+
@echo off
3+
pip freeze > installed_packages.txt & REM collect names of all packages installed in the environment to a text file
4+
for /F "delims==" %%i in (installed_packages.txt) do ( & REM iteratively delete each package
5+
echo Uninstalling %%i...
6+
pip uninstall -y %%i
7+
)
8+
del installed_packages.txt & REM delete the text file created

0 commit comments

Comments
 (0)