简体   繁体   中英

Windows Subsystem for Linux (WSL 2) and Jupyter Lab : How to open a Jupyter Notebook saved at the Linux file system?

I have a Jupyter lab installed on Windows. I installed Jupyter Lab on WSL Ubuntu. I can lunch Jupyter Lab from Linux terminal. This will open Jupyter Lab on Chrome browser from which I can start a new Jupyter Notebook with Python [conda env:root]*. However, it only shows the windows file system. I try to open my note book that is saved on the Linux file system using:
$ jupyter lab my_linux_folder/my_notebook.ipynb

Jupyter lab lunches successfully, but cannot open the notebook that is on the Linux file system and gives an error:

Cannot open
Could not find path: /my_notebook.ipynb

Is it possible to open a notebook that is on the Linux file system "\wsl$\" and how?
How to go to "\wsl$\" from JupyterLab file browser?
Or more generally how to open a notebook that is saved under "\wsl$\"?

WSL 2 issues a dynamic IP address each time you launch WSL 2 -- see MSFT docs .

Personally, I run a Python command within a subshell to print that IP:

jupyter lab --ip $(python3 -c "import subprocess; subprocess.run(['hostname', '-I'], text=True).stdout")

This works for me on Ubuntu 20.04 and Windows 10 build 19041.329.

Note: You'll likely have to visit that IP address instead of localhost, plus the port through which you're running Jupyter, eg http://:8888.

To get my IP address via the CLI I use:

ip addr | grep eth0 | grep inet

I choose the first IP address available, typically using that address without the subnet mask, ie the forward slash + number.

Update:

You can access the Linux files by modifying the configuration file. You'll need to allow root access and specify the Jupyter Notebook directory. The directory starts from the root directory of the Linux file system. You can also start from the Windows file system /mnt/c/users/admin/.jupyter .

{
    "NotebookApp": {
        ...
        "allow_root": true,
        "notebook_dir": "/home/admin/.jupyter",
        ...
    }
}

Original:

Is there a particular reason you need to save the Jupyter Notebook files on the Linux file system? WSL has full access to the Windows file system so it shouldn't matter where the file is saved.

To add to Jason's point:

Apparently, WSL 2 uses a virtual network adapter that has its own IP address. It also changes the IP address every time the server is restarted. It got annoying having to manually update the IP address so I wrote a script to update it automatically.

I wrote an article about how to do it to make it easier for everyone:

How to Set Up the Jupyter Notebook Home and Public Server On Windows Subsystem for Linux 2 (WSL2)

The attached photo is a Notebook running on WSL 2 that's saved on the Windows 10 file system.

在此处输入图像描述

Run this command, this will run notebook on your eth0 ip address.

jupyter notebook --no-browser --ip `ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d"/" -f1`

or

# Get eth0 IP address
HOST_ETH0_IP=`ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d"/" -f1`
# Run jupyter notebook for the IP address
jupyter notebook --no-browser --ip $HOST_ETH0_IP

or

HOST_ETH0_IP=`ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d"/" -f1`
jupyter lab--no-browser --ip $HOST_ETH0_IP

If you can set the ip address on the jupyter config

jupyter lab --generate-config
vim ~/.jupyter/jupyter_notebook_config.py

Then copy and paste the output of ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d"/" -f1 ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d"/" -f1

c.NotebookApp.ip = 'HOST_ETH0_IP'

I wrote a script to start the jupyter server and google chrome from WSL. It seems to work.

#!/bin/sh

ADDR=$(jupyter notebook list | grep http | awk '{print $1}')
if ! [ "$ADDR" ]; then
    IP=$(ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d/ -f1)
    # by default job control doesn't work in scripts
    set -m
    jupyter notebook --no-browser --ip $IP --port 8888 &
    sleep 1
    ADDR=$(jupyter notebook list | grep http | awk '{print $1}')
fi

/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe "$ADDR"

# without fg it is not possible to shutdown the server with Ctrl-C
fg %1 2>/dev/null || exit 0

Considering Jupyter has been installed on your WSL2:

0. Open your wsl shell and navigate to the path you want:
This is the only step your should precede which will open jupyter in the directory you want.

uername@ubuntu:/mnt/c/Users/yourname$ cd
uername@ubuntu:~$

for those who are not familiar with opening wsl jupyter-notebook in windows:

1. Use jupyter-notebook --no-browser command :
Open your shell and run jupyter-notebook --no-browser . For example, since I use my jupyter notebook in Anaconda distribution first I should activate the environment:

uername@ubuntu:~$ conda activate
(base) username@ubuntu:~$ jupyter-notebook --no-browser

2. Open the token address in your browser:
After running the command above you get similar result as the following, This part says what you need to do to open the jupyter notebook on your computer. Note the part that says:

Or copy and paste one of these URLs
    http://localhost:8888/?token=f17b5818f9d89ca2cb07e93d0026c2d913d8d4c7cef63f3b

or alternatively, you can hold ctrl & click on one of the links to get your wsl juyter notebook on windows.

[I 2021-09-30 11:28:00.279 LabApp] JupyterLab extension loaded from /home/username/anaconda3/lib/python3.8/site-packages/jupyterlab
[I 2021-09-30 11:28:00.279 LabApp] JupyterLab application directory is /home/username/anaconda3/share/jupyter/lab
[I 11:28:00.282 NotebookApp] Serving notebooks from local directory: /home/username
[I 11:28:00.282 NotebookApp] Jupyter Notebook 6.3.0 is running at:
[I 11:28:00.283 NotebookApp] http://localhost:8888/?token=f17b5818f9d89ca2cb07e93d0026c2d913d8d4c7cef63f3b
[I 11:28:00.283 NotebookApp]  or http://127.0.0.1:8888/?token=f17b5818f9d89ca2cb07e93d0026c2d913d8d4c7cef63f3b
[I 11:28:00.283 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 11:28:00.286 NotebookApp]

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

If you don't use --no-browser argumnet, wsl tries to open the jupyter-notebook on itself and since there is no browser either can exist on wsl, you get an error, although still you get the token address and you can copy it and use in on windows browser.

Try going to \\wsl$\{wsl distro name}\home\{user name}\my_linux_folder\

Longer explanation: browse to \\wsl$\ and see what you see there. There should be a distro name (Ubuntu, perhaps?) then you will have access to the root filesystem of your WSL. You can go to the home directory, then select your username, then your files should look familiar.

You could go the other way, and point Jupyter to a windows folder from Linux. However, assuming you might be using WSL2, the first method has much better performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM