首页 > 解决方案 > 如何在创建 ai 平台笔记本时安装 python 库

问题描述

当我在 GCP 中创建笔记本实例时,我想使用“选择要在创建后运行的脚本”。

具体来说,我想用它来安装 python 包。

我需要编写什么样的脚本(扩展名和内容)?

标签: pythongoogle-cloud-platformgcp-ai-platform-notebook

解决方案


这将是安装 Voila 的 Post 启动脚本的示例。将此文件保存在 GCS 存储桶中,并在创建 Notebook 时定义路径,例如:

gcloud notebooks instances create nb-1 \
'--vm-image-project=deeplearning-platform-release' \
'--vm-image-family=tf2-latest-cpu' \
'--metadata=post-startup-script=gs://ai-platform-notebooks-tools/install-voila.sh' \
'--location=us-central1-a'

脚本内容:

#!/bin/bash -eu
# Installs Voila in AI Platform Notebook

function install_voila() {
  echo 'Installing voila...'
  /opt/conda/condabin/conda install -y -c conda-forge ipywidgets ipyvolume bqplot scipy
  /opt/conda/condabin/conda install -y -c conda-forge voila
  /opt/conda/bin/jupyter lab build
  systemctl restart jupyter.service || echo 'Error restarting jupyter.service.'
}

function download_samples() {
  echo 'Downloading samples...'
  cd /home/jupyter
  git clone https://github.com/voila-dashboards/voila

}

function main() {
  install_voila || echo 'Error installing voila.' 
  download_samples || echo 'Error downloading voila samples.' 
}

main

推荐阅读