When I connected to a remote Jupyter Server with VScode, I was able to execute a local file, and the file output during file execution was remote.
I usually study numerical calculations using Python at university. Since the computing resources of the laboratory PC are scarce, the execution environment is built on the home PC, and the work is forwarding the local port of the jupyter notebook started on the home PC to the same port of the laboratory PC. The image is
Laboratory PC (host name: local) ↓ [local] ssh -L 8888:localhost:8888 remote Home PC (host name: remote) ↓ [remote] jupyter notebook Laboratory PC ↓ [local] xgd-open http://localhost:8888/?token=<token>
It is like this. Only remote files can be accessed this way. No, at least I understand that. Since both PCs are Windows, I could easily synchronize files with local by setting the working directory of jupyter started by remote to Onedrive. The image is
C:/users/<remote_user>/onedrive == C:/users/<local_user>/onedrive
It is like this.
This fall, Visual Studio Code started native support for jupyter notebook.
Reference: It seems that the Python extension of VS Code natively supports Jupyter, so I tried using it immediately. Although jupyter notebook has the advantage of an interactive development environment, it does not have the auxiliary functions that come with high-performance text editors such as Atom, and since it is a Web application, the UI style must be described in css. Tedious. I heard that this update of VScode also has the ability to connect to a remote jupyter server, and I was inspired by this big wave that I had no choice but to ride. But maybe it's only a few months since it's released, isn't there an article that focuses on the ability to connect to this remote jupyter server? When I groped, I came across a brand new technology that "can run local files in a remote execution environment". Moreover, the behavior was not intuitively understandable, so I decided to share it as an article.
As a trial, I used my notebook PC instead of the laboratory PC mentioned above.
Hostname: remote; Macho desktop with abundant computational resources win10, python3.6, jupyter
Host name: local; CPU is atom clock 1GHz gully laptop win10, python3.6, jupyter, VScode
I'm lying on the net, so I'll omit it. I just added the main body, Japanese and python add-ons.
Let the local VScode working directory be local_dir and the remote jupyter server working directory be remote_dir.
First, launch your notebook in your local environment. Type the following into the VScode command palette to open an empty notebook.
Python: Create New Blank Jupyter Notebook
Then write the following code in the cell and execute it.
~/local_dir/whereami.ipynb
#ln[1]
import socket
print(socket.gethostname())
#Out[1]
'local' #Local PC host name
I was able to run it in my local environment. Then connect to the remote jupyter server.
Python: Specify local or remote Jupyter server for connections
Then you will be prompted to specify the URL of the remote jupyter notebook. In my environment, as mentioned above, on the premise of sshing separately
http://localhost:8888/?token=<token>
is not it. This will create a hidden folder and files in local_dir.
~/local_dir/.vscode/settings.json
{
"python.dataScience.jupyterServerURI": http://localhost:8888/?token=<token>
}
Opening a notebook in the directory containing this configuration file will move the execution environment to a remote location.
~/local_dir/whereami.ipynb
#ln[1]
import socket
print(socket.gethostname())
#Out[1]
'remote' #Remote PC host name
Where is the actual executable file?
~/local_dir/whereami.ipynb
#ln[2]
import os
print(os.path.abspath(""))
#Out[2]
'<remote_dir>' #working directory of jupyter server
I intended to have a local file open, but I am supposed to have a file in the working directory of the remote PC. When I looked into the remote prompt that opened the jupyter notebook, I saw evidence of some caching in the log. Actually, the local file named Untitled [0-9] + .ipynb is cached in the working directory of jupyter server, and it seems that the cache is deleted when VScode is closed. Attempts to output some file in this state.
~/local_dir/whereami.ipynb
#ln[3]
import numpy as np
a = np.array([1,2,3])
np.savetxt("whereami.csv", a)
Where will whereami.csv be output?
~/remote_dir/whereami.csv
was.
It's remote, but it wasn't!
I tried running Jupyter Notebook with Visual Studio Code https://dev.classmethod.jp/server-side/python/visual-studio-code-jupyter-notebook/
It seems that the Python extension of VS Code has native support for Jupyter, so I tried using it immediately. https://qiita.com/simonritchie/items/5d865e72dba47cf8f6c0
Recommended Posts