When I connect to a remote Jupyter Server with VScode, it's remote but local

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.

Introduction

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.

Introducing VScode

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.

Execution environment

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

VScode environment

I'm lying on the net, so I'll omit it. I just added the main body, Japanese and python add-ons.

Fumbling experiment

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.

Summary

It's remote, but it wasn't!

reference

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

When I connect to a remote Jupyter Server with VScode, it's remote but local
I tried to communicate with a remote server by Socket communication with Python.
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
When I tried to make a VPC with AWS CDK but couldn't make it
When I tried to connect with SSH, I got a warning about free space.
I want to bind a local variable with lambda
I want to start a jupyter environment with one command
I want to use a virtual environment with jupyter notebook!
I made a web server with Raspberry Pi to watch anime
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
A note I was addicted to when creating a table with SQLAlchemy
I want to blog with Jupyter Notebook
It's more recent, but I wanted to do BMI calculation with python.
Connect to GNU / Linux with Remote Desktop
I made a tool to convert Jupyter py to ipynb with VS Code
Run Jupyter notebook on a remote server
When I tried to create a virtual environment with Python, it didn't work
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Build a local server with a single command [Mac]
Log in to the remote server with SSH
Set up a local server with Go-File upload-
I want to make a game with Python
Enable Jupyter Notebook with conda on remote server
Pretend to be a server with two PCs
I get a UnicodeDecodeError when running with mod_wsgi
Set up a local server with Go-File download-
How to set up a local development server
I tried to start Jupyter with Amazon lightsail
I want to write to a file with Python
When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.
[Python] I want to use only index when looping a list with a for statement
I got stuck when trying to specify a relative path with relative_to () in python
[Memorandum] python + vscode + pipenv It's common, but it was a mess with warning, so a memorandum
I tried to create a table only with Django
I made a ready-to-use syslog server with Play with Docker
I want to climb a mountain with reinforcement learning
I tried to draw a route map with Python
I want to work with a robot in python.
I want to split a character string with hiragana
I tried to automatically generate a password with Python3
I want to manually create a legend with matplotlib
[Note] I can't connect with wget, but I can connect with curl
[Python] I tried running a local server using flask
Current directory when creating a new one with Jupyter
Ssh to virtual environment with remote development of vscode
I want to run a quantum computer with Python
I introduced black to vscode, but it doesn't autoformat
I was surprised to receive a nice review when I wrote Python to CheckIO and its explanation
I made a server with Python socket and ssl and tried to access it from a browser
How to connect to Cloud SQL PostgreSQL on Google Cloud Platform from a local environment with Java
When I try to connect to MySQL with mysql-connector-python, I cannot connect with the error "SSL connection error: SSL_CTX_set_tmp_dh failed"
[Solution] When I try to connect to CloudSQL with GAE, I get an ImportError only when using dev_appserver.
When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)
Local server with python
A story that stumbled when I made a chatbot with Transformer
I want to remove Python's Unresolved Import Warning with vsCode
I got a Value Error when using JUMAN ++ with PyKNP
screen and split screen with python and ssh login to remote server
I tried starting Django's server with VScode instead of Pycharm