将服务器端的jupyter映射到本地浏览器进行编辑

这篇文章主要介绍了服务器端jupyter notebook映射到本地浏览器的操作,具有很好的参考价值,希望对大家有所帮助。

做科研经常有这样的需求:即需要借助服务器的计算资源,本地编辑的工程文件需要每次都传到服务器才能运行。jupyter是较好的交互式编辑工具,有没有一种方式可以在jupyter上编辑属于服务器的文件呢?这就引出了我们的端口映射工具。

1.远程服务器上安装jupyter notebook(配置jupyter_notebook_config.py文件)

pip install jupyter

2.使用ssh连接本机与服务器,使用端口映射

ssh -L 18192:127.0.0.1:8192 server_name@server_ip_address

服务器端口指定为8192(或者其他),表示在服务器端终端输入jupyter notebook启动jupyter时端口为8192。本机端口设置为18192,即可在本机的浏览器中按照地址加端口18192打开jupyter。
3. 启动jupyter

启动jupyter需要注意两点,

  • 默认是打开浏览器,而我们不能在服务器端自动打开浏览器
  • 需要指定端口为端口B,如8192

jupyter notebook --no-browser --port=8192 --allow-root

输入时候终端会打印如下信息:

(base) [HeShuai@master ~]$ jupyter notebook --no-browser --port=8192 --allow-root
[I 17:18:45.517 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found
[I 17:18:46.552 NotebookApp] [nb_conda] enabled
[I 17:18:46.553 NotebookApp] Serving notebooks from local directory: /public/home/HeShuai
[I 17:18:46.554 NotebookApp] Jupyter Notebook 6.4.11 is running at:
[I 17:18:46.554 NotebookApp] http://localhost:8192/?token=a0f02953aab9f748e7203a445395fa5207faad397241d582
[I 17:18:46.554 NotebookApp] or http://127.0.0.1:8192/?token=a0f02953aab9f748e7203a445395fa5207faad397241d582
[I 17:18:46.554 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 17:18:46.570 NotebookApp]

To access the notebook, open this file in a browser:
file:///public/home/HeShuai/.local/share/jupyter/runtime/nbserver-52497-open.html
Or copy and paste one of these URLs:
http://localhost:8192/?token=a0f02953aab9f748e7203a445395fa5207faad397241d582
or http://127.0.0.1:8192/?token=a0f02953aab9f748e7203a445395fa5207faad397241d582

这时候,你有两种方式在本地浏览器打开jupyter,注意端口需要指定为本地端口A

方式一:

http://localhost:18192

方式二:

http://127.0.0.1:18192

复制token(上述紫色部分内容)输入就可以愉快的使用jupyter啦!

 

 

 

posted @ 2022-05-13 18:18  何帅  阅读(1003)  评论(0编辑  收藏  举报