Skip to content

Commit

Permalink
Add support for the new JupyterLab on Amazon SageMaker Studio (#4664)
Browse files Browse the repository at this point in the history
* Added support for new the JupyterLab on Amazon SageMaker Studio

* Added support for the new JupyterLab on Amazon SageMaker Studio

---------

Co-authored-by: Jacinto Esteban <[email protected]>
  • Loading branch information
jakinto and Jacinto Esteban authored Jun 7, 2024
1 parent d0648e7 commit e3113f3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 72 deletions.
14 changes: 11 additions & 3 deletions aws_sagemaker_studio/streamlit_demo/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ GREEN='\033[1;32m'
NC='\033[0m'
S3_PATH=$1

echo "***************************************************************************"
echo "*** If you are NOT on Amazon SageMaker Studio Classic use 'bash run.sh' ***"
echo "***************************************************************************"

# Run the Streamlit app and save the output to "temp.txt"
streamlit run app.py > temp.txt &

Expand All @@ -17,8 +21,6 @@ sleep 5
PORT=$(grep "Network URL" temp.txt | awk -F':' '{print $NF}' | awk '{print $1}' | tail -c 5)
echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Port Number ${PORT}"



# Get Studio domain information
DOMAIN_ID=$(jq .DomainId /opt/ml/metadata/resource-metadata.json || exit 1)
RESOURCE_NAME=$(jq .ResourceName /opt/ml/metadata/resource-metadata.json || exit 1)
Expand Down Expand Up @@ -104,8 +106,14 @@ fi

echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Studio Url ${STUDIO_URL}"

JUPYTER_TYPE=
if grep -q '^NAME="Ubuntu"' /etc/os-release; then
JUPYTER_TYPE="jupyterlab"
else
JUPYTER_TYPE="jupyter"
fi

link="${STUDIO_URL}/jupyter/${RESOURCE_NAME}/proxy/${PORT}/"
link="${STUDIO_URL}/${JUPYTER_TYPE}/${RESOURCE_NAME}/proxy/${PORT}/"

echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Starting Streamlit App"
echo -e "${CYAN}${CURRENTDATE}: [INFO]: ${GREEN}${link}${NC}"
Expand Down
12 changes: 9 additions & 3 deletions aws_sagemaker_studio/streamlit_demo/setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
pip install --no-cache-dir -r requirements.txt
sudo yum install -y iproute
sudo yum install -y jq
sudo yum install -y lsof
if grep -q '^NAME="Ubuntu"' /etc/os-release; then
sudo apt-get install -y iproute2
sudo apt-get install -y jq
sudo apt-get install -y lsof
else
sudo yum install -y iproute
sudo yum install -y jq
sudo yum install -y lsof
fi
137 changes: 71 additions & 66 deletions aws_sagemaker_studio/streamlit_demo/status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ GREEN='\033[1;32m'
NC='\033[0m'
S3_PATH=$1

echo "******************************************************************************"
echo "*** If you are NOT on Amazon SageMaker Studio Classic use 'bash status.sh' ***"
echo "******************************************************************************"

# Get Studio domain information
DOMAIN_ID=$(jq .DomainId /opt/ml/metadata/resource-metadata.json || exit 1)
Expand All @@ -26,7 +29,6 @@ REGION=$(echo "${RESOURCE_ARN_ARRAY[3]}")
# Check if it's Collaborative Space
SPACE_NAME=$(jq .SpaceName /opt/ml/metadata/resource-metadata.json || exit 1)


# Find the process IDs of all running Streamlit instances
streamlit_pids=$(pgrep streamlit)

Expand All @@ -44,81 +46,84 @@ for pid in $streamlit_pids; do

done


echo "These are the Streamlit Apps Currently Running: "
# Loop through the array and print each port number
for PORT in "${port_array[@]}"; do
# if it's not a collaborative space
if [ -z "$SPACE_NAME" ] || [ $SPACE_NAME == "null" ] ;
then
# If it's a user-profile access
echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Domain Id ${DOMAIN_ID}"
STUDIO_URL="https://${DOMAIN_ID}.studio.${REGION}.sagemaker.aws"

# if it's not a collaborative space
if [ -z "$SPACE_NAME" ] || [ $SPACE_NAME == "null" ] ;
then
# If it's a user-profile access
STUDIO_URL="https://${DOMAIN_ID}.studio.${REGION}.sagemaker.aws"

# It is a collaborative space
else
# It is a collaborative space
else

SEM=true
SPACE_ID=
SEM=true
SPACE_ID=

# Check if Space Id was previously configured
if [ -f /tmp/space-metadata.json ]; then
SAVED_SPACE_ID=$(jq .SpaceId /tmp/space-metadata.json || exit 1)
SAVED_SPACE_ID=`sed -e 's/^"//' -e 's/"$//' <<< "$SAVED_SPACE_ID"`
# Check if Space Id was previously configured
if [ -f /tmp/space-metadata.json ]; then
SAVED_SPACE_ID=$(jq .SpaceId /tmp/space-metadata.json || exit 1)
SAVED_SPACE_ID=`sed -e 's/^"//' -e 's/"$//' <<< "$SAVED_SPACE_ID"`

if [ -z "$SAVED_SPACE_ID" ] || [ $SAVED_SPACE_ID == "null" ]; then
ASK_INPUT=true
else
ASK_INPUT=false
fi
else
if [ -z "$SAVED_SPACE_ID" ] || [ $SAVED_SPACE_ID == "null" ]; then
ASK_INPUT=true
else
ASK_INPUT=false
fi

# If Space Id is not available, ask for it
while [[ $SPACE_ID = "" ]] ; do
# If Space Id already configured, skeep the ask
if [ "$ASK_INPUT" = true ]; then
echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Please insert the Space Id from your url. e.g. https://${GREEN}<SPACE_ID>${NC}.studio.${REGION}.sagemaker.aws/jupyter/default/lab"
read SPACE_ID
SEM=true
else
SPACE_ID=$SAVED_SPACE_ID
fi

if ! [ -z "$SPACE_ID" ] && ! [ $SPACE_ID == "null" ] ;
then
while $SEM; do
echo "${SPACE_ID}"
read -p "Should this be used as Space Id? (y/N) " yn
case $yn in
[Yy]* )

jq -n --arg space_id $SPACE_ID '{"SpaceId":$space_id}' > /tmp/space-metadata.json

STUDIO_URL="https://${SPACE_ID}.studio.${REGION}.sagemaker.aws"

SEM=false
;;
[Nn]* )
SPACE_ID=
ASK_INPUT=true
SEM=false
;;
* ) echo "Please answer yes or no.";;
esac
done
fi
done
else
ASK_INPUT=true
fi

link="${STUDIO_URL}/jupyter/${RESOURCE_NAME}/proxy/${PORT}/"
# If Space Id is not available, ask for it
while [[ $SPACE_ID = "" ]] ; do
# If Space Id already configured, skeep the ask
if [ "$ASK_INPUT" = true ]; then
echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Please insert the Space Id from your url. e.g. https://${GREEN}<SPACE_ID>${NC}.studio.${REGION}.sagemaker.aws/jupyter/default/lab"
read SPACE_ID
SEM=true
else
SPACE_ID=$SAVED_SPACE_ID
fi

echo -e "${GREEN}${link}${NC}"
done
exit 0
if ! [ -z "$SPACE_ID" ] && ! [ $SPACE_ID == "null" ] ;
then
while $SEM; do
echo "${SPACE_ID}"
read -p "Should this be used as Space Id? (y/N) " yn
case $yn in
[Yy]* )

jq -n --arg space_id $SPACE_ID '{"SpaceId":$space_id}' > /tmp/space-metadata.json

STUDIO_URL="https://${SPACE_ID}.studio.${REGION}.sagemaker.aws"

SEM=false
;;
[Nn]* )
SPACE_ID=
ASK_INPUT=true
SEM=false
;;
* ) echo "Please answer yes or no.";;
esac
done
fi
done
fi

JUPYTER_TYPE=
if grep -q '^NAME="Ubuntu"' /etc/os-release; then
JUPYTER_TYPE="jupyterlab"
else
JUPYTER_TYPE="jupyter"
fi

echo "These are the Streamlit Apps Currently Running: "
# Loop through the array and print each port number
for PORT in "${port_array[@]}"; do
link="${STUDIO_URL}/${JUPYTER_TYPE}/${RESOURCE_NAME}/proxy/${PORT}/"

echo -e "${CYAN}${CURRENTDATE}: [INFO]: ${GREEN}${link}${NC}"


done
exit 0
fi

0 comments on commit e3113f3

Please sign in to comment.