Skip to content

Commit

Permalink
Pass python dependencies as args and pin versions
Browse files Browse the repository at this point in the history
  • Loading branch information
saileshd1402 committed Dec 2, 2024
1 parent 090a4fd commit abb400e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-example-notebooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ jobs:
-i ./examples/pytorch/image-classification/create-pytorchjob.ipynb \
-o ./examples/pytorch/image-classification/create-pytorchjob-output.ipynb \
-p "namespace default" \
-d jupyter==1.1.1 -d ipykernel==6.29.5 -d papermill==2.6.0 \
-k ./sdk/python
4 changes: 2 additions & 2 deletions examples/pytorch/image-classification/create-pytorchjob.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"outputs": [],
"source": [
"python_sdk='kubeflow-training'\n",
"training_python_sdk='kubeflow-training'\n",
"namespace='kubeflow-user-example-com'"
]
},
Expand All @@ -57,7 +57,7 @@
"source": [
"# TODO (andreyvelich): Change to release version when SDK with the new APIs is published.\n",
"# Install Kubeflow Python SDK\n",
"!pip install {python_sdk}"
"!pip install {training_python_sdk}"
]
},
{
Expand Down
51 changes: 40 additions & 11 deletions scripts/run-notebook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ NOTEBOOK_INPUT=""
NOTEBOOK_OUTPUT=""
PAPERMILL_PARAMS=()
PAPERMILL_PARAM_YAML=""
PYTHON_SDK="git+https://github.com/kubeflow/training-operator.git#subdirectory=sdk/python"
TRAINING_PYTHON_SDK="git+https://github.com/kubeflow/training-operator.git#subdirectory=sdk/python"
PYTHON_DEPENDENCIES=()
PYTHON_REQUIREMENTS_FILE=""

usage() {
echo "Usage: $0 -i <input_notebook> -o <output_notebook> [-p \"<param> <value>\"...] [-y <params.yaml>]"
Expand All @@ -30,18 +32,23 @@ usage() {
echo " -p Papermill parameters (optional), pass param name and value pair (in quotes whitespace separated)"
echo " -y Papermill parameters YAML file (optional)"
echo " -k Kubeflow Training Operator Python SDK (optional)"
echo " -d Python dependencies args (optional)"
echo " -r Python dependencies requirements file (optional)"
echo " -h Show this help message"
echo "NOTE: papermill, jupyter and ipykernel are required Python dependencies to run Notebooks. Dependencies can be passed through -d or -r"
exit 1
}

while getopts "i:o:y:p:k:h:" opt; do
while getopts "i:o:y:p:k:r:d:h:" opt; do
case "$opt" in
i) NOTEBOOK_INPUT="$OPTARG" ;; # -i for notebook input path
o) NOTEBOOK_OUTPUT="$OPTARG" ;; # -o for notebook output path
p) PAPERMILL_PARAMS+=("$OPTARG") ;; # -p for papermill parameters
y) PAPERMILL_PARAM_YAML="$OPTARG" ;; # -y for papermill parameter yaml path
k) PYTHON_SDK="$OPTARG" ;; # -k for training operator python sdk
h) usage ;; # -h for help (usage)
i) NOTEBOOK_INPUT="$OPTARG" ;; # -i for notebook input path
o) NOTEBOOK_OUTPUT="$OPTARG" ;; # -o for notebook output path
p) PAPERMILL_PARAMS+=("$OPTARG") ;; # -p for papermill parameters
y) PAPERMILL_PARAM_YAML="$OPTARG" ;; # -y for papermill parameter yaml path
k) TRAINING_PYTHON_SDK="$OPTARG" ;; # -k for training operator python sdk
d) PYTHON_DEPENDENCIES+=("$OPTARG") ;; # -d for passing python dependencies as args
r) PYTHON_REQUIREMENTS_FILE="$OPTARG" ;; # -k for passing python dependencies as requirements file
h) usage ;; # -h for help (usage)
*) usage; exit 1 ;;
esac
done
Expand All @@ -51,11 +58,27 @@ if [ -z "$NOTEBOOK_INPUT" ] || [ -z "$NOTEBOOK_OUTPUT" ]; then
exit 1
fi

# Install Python dependencies to run Jupyter Notebooks with papermill
pip install jupyter ipykernel papermill==2.6.0
# Check if we need to install dependencies
if [ ${#PYTHON_DEPENDENCIES[@]} -gt 0 ] || [ -n "$PYTHON_REQUIREMENTS_FILE" ]; then
pip_install_cmd="pip install"

papermill_cmd="papermill $NOTEBOOK_INPUT $NOTEBOOK_OUTPUT -p python_sdk $PYTHON_SDK"
for dep in "${PYTHON_DEPENDENCIES[@]}"; do
pip_install_cmd="$pip_install_cmd $dep"
done

if [ -n "$PYTHON_REQUIREMENTS_FILE" ]; then
pip_install_cmd="$pip_install_cmd -r $PYTHON_REQUIREMENTS_FILE"
fi

echo "Installing Dependencies: $pip_install_cmd"
eval "$pip_install_cmd"
if [ $? -ne 0 ]; then
echo "Error: Failed to install dependencies." >&2
exit 1
fi
fi

papermill_cmd="papermill $NOTEBOOK_INPUT $NOTEBOOK_OUTPUT -p training_python_sdk $TRAINING_PYTHON_SDK"
# Add papermill parameters (param name and value)
for param in "${PAPERMILL_PARAMS[@]}"; do
papermill_cmd="$papermill_cmd -p $param"
Expand All @@ -65,6 +88,12 @@ if [ -n "$PAPERMILL_PARAM_YAML" ]; then
papermill_cmd="$papermill_cmd -y $PAPERMILL_PARAM_YAML"
fi

# Check if papermill is installed
if ! command -v papermill &> /dev/null; then
echo "Error: papermill is not installed. Please install papermill to proceed. Python dependencies can be passed through -d or -r args"
exit 1
fi

echo "Running command: $papermill_cmd"
$papermill_cmd

Expand Down

0 comments on commit abb400e

Please sign in to comment.