-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjupyter_converter.sh
executable file
·71 lines (58 loc) · 1.9 KB
/
jupyter_converter.sh
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if jupyter is installed
if ! command_exists jupyter; then
echo "Error: jupyter is not installed. Please install it to use this script."
exit 1
fi
# Optionally check if black is installed
if ! command_exists black; then
echo "Warning: black is not installed. The script will run without formatting."
use_black=false
else
use_black=true
fi
# Check if at least one notebook file is provided
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <notebook1.ipynb> [notebook2.ipynb ...]"
exit 1
fi
# Process each notebook file provided as an argument
for notebook in "$@"; do
if [[ ! -f "$notebook" ]]; then
echo "Error: File '$notebook' not found."
continue
fi
# Convert the Jupyter notebook to a Python script
jupyter nbconvert --to script "$notebook"
if [ $? -ne 0 ]; then
echo "Error: Failed to convert '$notebook'."
continue
fi
# Extract the base name of the file (without extension)
base_name=$(basename "$notebook" .ipynb)
# Define the generated .py file name
py_file="${base_name}.py"
# Check if the .py file was created
if [[ ! -f "$py_file" ]]; then
echo "Error: Conversion succeeded, but Python file '$py_file' not found."
continue
fi
# Clean the Python script by removing cell notations and overwrite the original file
grep -v '^# In\[' "$py_file" > "${py_file}.tmp"
if [ $? -ne 0 ]; then
echo "Error: Failed to clean '$py_file'."
rm -f "${py_file}.tmp"
continue
fi
# Replace the original file with the cleaned file
mv "${py_file}.tmp" "$py_file"
# Optionally format the cleaned Python script with black
if [ "$use_black" = true ]; then
black "$py_file"
fi
echo "Cleaned and formatted Python script generated: ${py_file}"
done