1. 程式人生 > 其它 >將伺服器端的jupyter對映到本地瀏覽器進行編輯

將伺服器端的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啦!