リモートサーバー上のjupyter notebookをVSCodeで利用する方法
リモートサーバー上で動かしているjupyter notebookをローカルのVSCode上で使いたいときの手順です. いつも忘れてしまうので,まとめておきます.
まず,sshでサーバーにつなぐ際,ポートフォワーディングを入れておきます.
ssh -L 5000:localhost:5000 [remote server address] -l user
ここでは,Dockerコンテナ内でjupyter notebookを動作させているとします. Dockerイメージの作成手順は省略します. Dockerコンテナ作成時にリモートサーバーに対してポートフォワーディングできるようにしておきます.
docker run -it -v /home/user/:/home/user/ -p 5000:5000 [container_name] bash
次にDockerコンテナ内に入って,jupyter notebookを起動します.
jupyter notebook --port 5000 --ip=0.0.0.0 --allow-root
ローカルのVSCodeでCtrl + Shift + pでコマンドパレットを呼び出します. 呼び出すと,以下のような感じでコマンドパレットがでます. #Python環境が整っていることが前提です
Python: Create New Blank Jupyter Notebookと入力して,空のjupyter notebookを作成します. そして,再度コマンドパレットを呼び出し,Python: Specify local of remote Jupyter server for connectionsと入力します. そうすると,サーバーアドレスを問われるので,以下のように入力します.
https://[remote server address]:5000/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
アドレスやtokenはDockerコンテナ内のjupyter notebook起動時に表示されます. そのアドレスをリモートサーバーのipアドレスに変更して入力するだけです.