Before using this method, ensure that the Jupyter installation is complete.

Directions:

Write a shell script along these lines (see the script directly at the end of this article)

  1. Specify that WSL2 network address 0.0.0.0 is used to start Jupyter
Jupyter notebook - IP = 0.0.0.0Copy the code
  1. ifconfigView the WSL2 IPV4 address
  2. Replace the IP address in the output with the WSL2 IPV4 address
  3. Open the host browser through WSL2 to access the address output in the previous step

In WSL, it is possible to call the window command or execute the.exe file. For example, run the following command in WLS to open Chrome and access Baidu.

${path} chrome/chrome. Exe"http://www.baidu.com"
Copy the code

Note here: Windows directories can contain Spaces. When using a directory with a space directly on the WSL command line, you can simply escape the space with a backslash. But in shell scripts, errors are reported even if escaped.

Ln -s ${.exe path} ${specified path}/${alias} link the exe file directly to the specified directory, and then call it in shell script.

For example :(please modify exe path and link target path as required)

# what I use is the new edge, exe path to "C: / Program Files (x86)/Microsoft/edge/Application/msedge. Exe", in the WSL only need to change the "C:" to "/ MNT/C"
ln -s /mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe /home/${WSL username}/wincmd/edge
Copy the code

/home/${WSL username}/wincmd/edge

/home/${WSL username}/wincmd/edge "http://www.baidu.com"Copy the code

For long-term use, consider modifying.bashrc to add it to environment variables

The startup script

Copy the following script and create jupyter.sh. After modifying the specified content, run the./jupyter.sh [startup path] command to start and output the access address.

#! /usr/bin/env bash

#set -ex

#If there are multiple Jupyters, you can directly specify paths
jupyter=$(which jupyter)
#Specify a browser. Modify the browser as requiredBowser =/home/${WSL username}/wincmd/edge
#Specify the Jupyter startup pathbasedir=$1 if [[ ${basedir} = '' ]]; then echo "usage:" echo " ./jupyter.sh [path]" echo " [path] The base path of Jupyter" exit 1 fi
#The Jupyter startup path is displayedCD "${basedir}" | | exit 1 echo "boot path: $(PWD)"#Start Jupyter with the WSL2 network address and redirect the logsNohup "${jupyter}" notebook -- IP =0.0.0.0 > jupyter.log &
#Adjust the waiting time according to the actual startup speed
sleep 2s
#Search the token
token=$(head jupyter.log | grep token=)
#Intercept the port and what follows
port=${token##*:}
#Intercepting port
port=${port%%/*}
#The interception"="After the token
token=${token##*=}

#Get WSL2 IP
ip=$(ifconfig | grep 'inet ' | awk '{print $2}' | grep -v 127)

#Output access addressurl="http://${ip}:${port}/? token=${token}" echo "$url"#Access using the host browser
${bowser} "$url"
Copy the code