从远程服务器运行Jupyter Notebook的脚本 [英] Script to run jupyter notebooks from remote server

查看:416
本文介绍了从远程服务器运行Jupyter Notebook的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台运行jupyter笔记本的服务器(Ubuntu服务器16.04),还有一台使用google-chrome可视化这些笔记本的本地计算机(Mac).为此,我必须:

I have a server (Ubuntu server 16.04) that runs jupyter notebooks, and a local machine (Mac) where I use google-chrome to visualize these notebooks. To do so, I have to:

  1. 在服务器中运行jupyter笔记本:

  1. Run jupyter notebook in the server:

jupyter笔记本--no-browser --port = $ {remotePort}

jupyter notebook --no-browser --port=${remotePort}

在我的本地计算机上指定一个SHH隧道:

Specify a SHH tunnel in my local machine:

ssh -f $ {username} @ $ {serverIP} -L $ {localPort}:localhost:$ {remotePort}

ssh -f ${username}@${serverIP} -L ${localPort}:localhost:${remotePort}

要自动执行此过程,我创建了脚本jupyter.sh(如下所述),因此我只能在本地计算机上运行:

To automate this process, I've created the script jupyter.sh (described below), such that I only run on my local machine:

bash jupyter.sh -u myUserNameInServer

它完美无瑕.它能够运行前两个步骤,并且会自动在我的Web浏览器中打开jupyter页面.不过,我想知道是否有更好的方法可以做到这一点.非常感谢您的评论.

It works flawlessly. It's able to run the previous two steps plus it automatically open the jupyter page in my web-browser. Still, I would like to know if there is a better way to do this. I would greatly appreciate your comments.

谢谢.

#######################################################################
## 1. SET VARIABLES TO STABLISH THE SSH CONNECTION
# Get username from command line: bash jupyter.sh -u username
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
    -u|--username)
    username="$2"
    shift # past argument
    ;;
esac
shift # past argument or value
done
# Specificy other variables to stablish the ssh connection
localPort=8890
browser="Google Chrome"
serverIP=the_IP_of_the_server
#######################################################################
# 2. RUN JUPYTER IN REMOTE SERVER
out=$(ssh -T ${username}@${serverIP} <<HERE
    # Only run jupyter if it isn't already running
    if [ \$(ps -u ${username} | grep jupyter | wc -l) -eq 0 ]
    then
        # Create a folder called jupyter, and move into it
        if [ ! -d jupyter ]; then mkdir jupyter; fi
        cd jupyter
        # Create a script to run jupyter
        echo "jupyter notebook --no-browser --NotebookApp.token=${username}" > jupyter.sh 
        # Run jupyter in the background
        screen -S jupyter -d -m bash jupyter.sh
    fi
    # Output the remote port number. If there is more than 1, get the first one
    jupyter notebook list | grep localhost | awk '{split(\$0,a,"localhost:");split(a[2],b,"/"); print b[1]}' | head -n1
HERE
)

#######################################################################
# 3. SET SSH TUNNEL
# Pass the remote port to a variable in the local machine
remotePort=$(echo $out | awk '{print $NF}') 
# Start listening in local port 8890 if that port isn't already in use
# num equal 1 if port number is already in use, 0 otherwise
num=$(netstat -lnt | awk 'BEGIN{x=0} ($6 == "LISTEN" && $4 ~ "8890$"){x=1}END{print x}')
if [ $num -eq 0 ]
then
    ssh -f ${username}@${serverIP} -L ${localPort}:localhost:${remotePort} -N
fi
#
# Open jupyter in browser
open -a "${browser}" http://localhost:${localPort}/tree?token=${username} &

推荐答案

您可以使用默认情况下未激活的"jupyter配置文件",因此需要在服务器上执行以下命令:

You can use the "jupyter config file" wich is not activated by default so need to do this command fisrt (on your server):

jupyter notebook --generate-config

然后在"/.jupyter"文件夹中生成的"jupyter_notebook_config.py"中: 取消注释行并更改值.

Then in the generated "jupyter_notebook_config.py" in the "/.jupyter" folder : uncomment the line and change the value.

通过这种方法,您可以调整配置,例如密码而不是令牌,默认目录,端口等..随心所欲,这可能是在脚本中执行某些"jupyter config"然后保持所有内容的正确方法别的.

This way you can tweak configuation such as password instead of token, default directory , port and so on .. Do as you want it may be a proper way to do some of the "jupyter config" in your script then keep evertything else.

这篇关于从远程服务器运行Jupyter Notebook的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆