WSL(windows subsytem linux)下添加网络浏览器支持
windows下在subsystem linux中安装了Anaconda,在使用jupyter notebook时,发现命令行并不能自动打开浏览器,原来是子系统linux中没有可用网页浏览器,本文就讲述一下如何在WSL中添加网络浏览器支持。
问题现象
WSL命令行中,在相应目录下使用jupyter notebook,会出现如下提示:
➜ Algorithm_Research git:(master) jupyter notebook
[I 10:44:16.783 NotebookApp] Serving notebooks from local directory: /mnt/c/Users/smslit/Desktop/topscomm/Algorithm_Research
[I 10:44:16.784 NotebookApp] 0 active kernels
[I 10:44:16.784 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=000982665d732a5d7052fe3bbadb4346fe0b3fdbc8b87bb4
[I 10:44:16.784 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 10:44:16.789 NotebookApp] No web browser found: could not locate runnable browser.
[C 10:44:16.789 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=000982665d732a5d7052fe3bbadb4346fe0b3fdbc8b87bb4
其中可以看到No web browser found: could not locate runnable browser
,这是因为win下启用了ubuntu bash后只是命令行的接口,但没有相应网络浏览器安装。
意外发现
WSL可以执行exe
惊奇的发现wsl的命令行中可以调用exe,比如执行以下命令可以打开chrome浏览器,也就是说可以将windows中安装的浏览器注册给wsl。
/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe
WSL中可以用命令行配置默认浏览器
如果我们直接用如下命令行配置网页浏览器,会提示没有安装浏览器,没有选项。
sudo update-alternatives --config x-www-browser
怎么办?这个时候想到了一种饶路子的方式,ubuntu下有配置浏览器选项优先级的命令,这个命令需要指明浏览器,此时指定win 下chrome的路径就可以了,试一下:
➜ Downloads cd /mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application
➜ Application ll
总用量 1.4M
dr-xr-xr-x 0 root root 512 8月 18 08:31 60.0.3112.101
-r-xr-xr-x 1 root root 1.3M 8月 11 15:40 chrome.exe
-r-xr-xr-x 1 root root 410 8月 18 08:31 chrome.VisualElementsManifest.xml
-r-xr-xr-x 1 root root 121K 8月 14 08:41 master_preferences
drwxrwxrwx 0 root root 512 8月 25 18:14 SetupMetrics
➜ Application sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser /mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe 100
update-alternatives: 使用 /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe 来在自动模式中提供 /usr/bin/x-www-browser (x-www-browser)
➜ Application sudo update-alternatives --config x-www-browser
链接组 x-www-browser (提供 /usr/bin/x-www-browser)中只有一个候选项:/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe无需配置。
jupyter notebook
此时再次执行jupyter notebook,惊喜,确实可以自动打开chrome呈现仿真目录:
➜ Algorithm_Research git:(master) jupyter notebook
[I 10:59:17.800 NotebookApp] Serving notebooks from local directory: /mnt/c/Users/smslit/Desktop/topscomm/Algorithm_Research
[I 10:59:17.800 NotebookApp] 0 active kernels
[I 10:59:17.800 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=2d7f588a4341b73bc65fcc6c85aac0289677123ef1b8e24c
[I 10:59:17.800 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 10:59:17.807 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=2d7f588a4341b73bc65fcc6c85aac0289677123ef1b8e24c
[I 10:59:18.359 NotebookApp] Accepting one-time-token-authenticated connection from 127.0.0.1
[W 11:02:15.429 NotebookApp] 404 GET /static/components/preact/preact.min.js.map (127.0.0.1) 32.15ms referer=None
[W 11:02:15.433 NotebookApp] 404 GET /static/components/proptypes/index.js.map (127.0.0.1) 2.30ms referer=None
[W 11:02:15.436 NotebookApp] 404 GET /static/components/preact-compat/preact-compat.min.js.map (127.0.0.1) 2.09ms referer=None
需要在配置文件里做一下配置,注意里面的地址空格和括号都不需要用反斜杠转义
import webbrowser
webbrowser.register(‘edge’,None,webbrowser.GenericBrowser(u’/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe’))
c.NotebookApp.browser = ‘edge’
我配了之后能打开浏览器了,但是地址还是打不开,打开的是root目录下的文件,不是localhost那个地址,但是因为浏览器不能直接用绝对路径访问root目录,所以打不开
@Anonymous , 是我的jupyterlab版本太低,更新版本之后,把use_redirect_file设置为False就可以了,但是现在遇到了新问题,老版本的jupyterlab安装了插件,现在新版本不支持,打开的时候排版有问题
@Anonymous , 解决了,
If you use conda you can run either conda install nodejs or conda install -c conda-forge nodejs.
Otherwise, use n to install the latest version of node:
$ [sudo] npm install npm -g
$ npm install -g n
$ n latest
$ n
以上这个是github上看到的解决方法,我是用pip安装的jupyterlab,用以上方法解决了
@Anonymous , 重新提交一下,之前排版有问题
另外添加一下地址:https://github.com/explosion/jupyterlab-prodigy/issues/5
If you use conda you can run either conda install nodejs or conda install -c conda-forge nodejs.
Otherwise, use n to install the latest version of node:
[sudo] npm install npm -g
npm install -g n
n latest
n
确实,没成功,可能是因为使用的CONDA的jupyter?
确实,没成功,可能是因为使用的CONDA的jupyter?
可以使用 wslu 中的 wslview,可以打开 windows 中的默认浏览器
现在的浏览器打开jupyter界面默认使用.jupyter/目录下的文件了,但是windows的浏览器不能解析这种路径
您好!我按照您上述方法试了一下,还是没有办法自动打开chrome呈现仿真目录,请问您能提供一些建议或者帮助吗?谢谢!
@Breeze , 按顺序确认一下:
sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser 【这里替换为 chrome 的安装目录】 100
注册到x-www-browser
中sudo update-alternatives --config x-www-browser
配置 chrome 为默认浏览器@5km , 多谢!但是我按照这些操作还是无法成功实现自动打开。截图如下:
https://www.dropbox.com/s/nce190m37vqpwgy/Capture.PNG?dl=0
不知道您有什么建议?
@Breeze , 你发的截图已经不存在了?
@5km , Sorry! 我又发了一遍:
https://www.dropbox.com/s/nce190m37vqpwgy/Capture.PNG?dl=0
v1.5.2