将 Jupyter 与 Windows 子系统一起用于 Linux

Using Jupyter with Windows Subsystem for Linux

在 Windows 10 (installation instructions here) 上使用 Ubuntu bash 终端,我安装了 Anaconda 并且一直在毫无问题地使用 Jupyter 笔记本。不幸的是,Jupyter 无法从子系统中找到可运行的浏览器,因此我必须复制并粘贴它在终端中输出的 link - 但这是可行的。当我尝试打开多个笔记本时,主要问题就出现了。通常,Jupyter 会检测到一个端口(默认为 8888)已被使用并创建一个新端口,但它似乎无法检测到这一点,因此当我使用它生成的 link 时,我最终看着我打开的第一个笔记本而不是新笔记本。

知道问题出在哪里吗?如果没有,我该如何手动解决这个问题?

启动笔记本时手动分配不同的端口号。例如:

jupyter notebook --port=8889

尝试:

jupyter notebook --no-browser

我在浏览器上遇到了类似的问题,我得到了

No web browser found: could not locate runnable browser.

我安装了 WSLU https://github.com/wslutilities/wslu。 然后我得到了

Start : This command cannot be run due to the error: The system cannot find the file specified.
At line:1 char:1
+ Start --h
+ ~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

jupyter-notebook 不提供 url 作为 wlsview 的参数。它将带有文件的路径传递给浏览器。例如

file:///home/myhome/.local/share/jupyter/runtime/nbserver-5058-open.html

实际url

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="refresh" content="1;url=http://localhost:8888/tree?token=19b5f1fefb13f5fc315b05991175d1f8cb5ada9baaca6804" />
    <title>Opening Jupyter Notebook</title>
</head>
<body>

<p>
    This page should redirect you to Jupyter Notebook. If it doesn't,
    <a href="http://localhost:8888/tree?token=19b5f1fefb13f5fc315b05991175d1f8cb5ada9baaca6804">click here to go to Jupyter</a>.
</p>

</body>
</html>

创建一个文件 jupyter-notebook-browser,其中包含要提取实际内容的文件 url

#!/bin/bash
file=$(echo "" | sed 's/file:\/\///')
url=$(grep -oP 'href="\K([^"]*localhost[^"]+)' "$file")
wslview "$url"

然后 运行 jupyter-notebook --browser=jupyter-notebook-browser

或定义BROWSER变量和运行

export BROWSER="jupyter-notebook-browser"
jupyter-notebook

更新:
对于某些人来说,下面的原始答案应该仍然有效。
对于收到与 tcgetpgrp failed: Not a tty 消息相关的其他错误的其他人,目前没有真正的修复。 (See this issue on GitHub)
因此,你需要在配置文件$HOME/.jupyter/jupyter_notebook_config.py.

中使用jupyter notebook --no-browser或设置c.NotebookApp.open_browser = False

原回答:
我遇到了 Jupyter 找不到文件的问题(德语的完整错误消息):

Start : Dieser Befehl kann aufgrund des folgenden Fehlers nicht ausgeführt werden: Das System kann die angegebene
Datei nicht finden.
In Zeile:1 Zeichen:1
+ Start "file:///home/nico/.local/share/jupyter/runtime/nbserver-1164-o ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Jupyter 确实有效,但是当我输入 jupyter notebook 时它没有打开我的浏览器。

我找到了一个非常简单的解决方案:

  1. 创建 Jupyter 配置文件:
jupyter notebook --generate-config

touch $HOME/.jupyter/jupyter_notebook_config.py
  1. 现在将以下行添加到配置文件(使用 nano 或任何其他文本编辑器):
c.NotebookApp.use_redirect_file = False

现在,我的WSL使用wslview命令在Windows中打开默认浏览器。 (我认为)

如果 wslview . 没有任何作用,您可能需要手动安装 wslu

旁注:
此解决方案也适用于 jupyter lab,但您必须使用 c.LabApp.use_redirect_file = False$HOME/.jupyter/jupyter_lab_config.py.
或者在 $HOME/.jupyter/jupyter_server_config.py.

中更好 c.ServerApp.use_redirect_file = False