如何更改 jupyter notebook 在 windows 中使用的默认浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47772157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:00:41  来源:igfitidea点击:

How to change the default browser used by jupyter notebook in windows

windowsgoogle-chromeanacondajupyter

提问by Marco

I'm on a windows machine without admin right and I would like to run jupyter on chrome, while the default browser is another.

我在没有管理员权限的 Windows 机器上,我想在 chrome 上运行 jupyter,而默认浏览器是另一个。

I have a local installation of the Anaconda distribution and my first option to start jupyter would be through the Anaconda Navigator, but maybe I have to do something else. Because it is a local installation the command line jupyter notebookproduces no results.

我在本地安装了 Anaconda 发行版,我启动 jupyter 的第一个选择是通过 Anaconda Navigator,但也许我必须做其他事情。因为它是本地安装,所以命令行jupyter notebook不会产生任何结果。

When I paste the url address in the default browser I have (something like http://localhost:8892/notebooks/Home/Exercices/Testing1.ipynbthe chrome page asks me for a password or token. I have no password and I do not know what a token is.

当我在默认浏览器中粘贴 url 地址时(类似于http://localhost:8892/notebooks/Home/Exercices/Testing1.ipynbchrome 页面要求我输入密码或令牌。我没有密码,我不知道令牌是什么。

Is there a way to change the browser of the Anaconda Navigator? or how can I start jupyter with Chrome?

有没有办法更改 Anaconda Navigator 的浏览器?或者如何使用 Chrome 启动 jupyter?

回答by Marco

Thanks to @Darthdith and this post How to change the default browser used by the ipython/jupyter notebook in Linux?I was able to figure it out:

感谢@Darthdith 和这篇文章如何更改 Linux 中 ipython/jupyter 笔记本使用的默认浏览器?我能够弄清楚:

Step 1: To open Anaconda Prompt from the Start Menu and type

步骤 1:从开始菜单打开 Anaconda Prompt 并键入

jupyter notebook --generate-config

This will generate the file ~/.jupyter/jupyter_notebook_config.py

这将生成文件 ~/.jupyter/jupyter_notebook_config.py

Step 2: Edit this file and change the following line (chrome is also is also in a local installation)

第 2 步:编辑此文件并更改以下行(chrome 也在本地安装)

c.NotebookApp.browser = u'C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe %s'

回答by Abhirup Das

In Windows, write in cmd/ Anaconda Prompt:

在 Windows 中,在 cmd/Anaconda Prompt 中写入:

jupyter notebook --generate-config

The jupyter_notebook_config.py file generated is situated in "C:\Users\YourName\.jupyter\" folder.

生成的 jupyter_notebook_config.py 文件位于“C:\Users\YourName\.jupyter\”文件夹中。

Open it using a text editor and change #c.NotebookApp.browser = ''to

使用文本编辑器打开它并更改#c.NotebookApp.browser = ''

import webbrowser
webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'))
c.NotebookApp.browser = 'chrome'

and save the file.

并保存文件。

Now execute the jupyter-notebook command and the set browser will be used.

现在执行 jupyter-notebook 命令,将使用设置的浏览器。

回答by Lee

I don't know the precise details for Windows, but this is how to set the default browser on a Mac:

我不知道 Windows 的确切细节,但这是在 Mac 上设置默认浏览器的方法:

jupyter notebook --generate-config

This creates a file jupyter_notebook_config.py in ~/.jupyter. Edit the line

这会在 ~/.jupyter 中创建一个文件 jupyter_notebook_config.py。编辑行

#c.NotebookApp.browser = ''

On a Mac I set it to:

在 Mac 上,我将其设置为:

c.NotebookApp.browser = u'/Applications/Gooogle\ Chrome.app %s'

You just need to figure out how to point it to Chrome on Windows.

您只需要弄清楚如何将其指向 Windows 上的 Chrome。

回答by darthbith

As far as I know, there's no way to change the default browser that opens. However, you can find the token for the Notebook server by opening Anaconda Prompt from the Start Menu and typing

据我所知,无法更改打开的默认浏览器。但是,您可以通过从“开始”菜单打开 Anaconda Prompt 并键入来找到 Notebook 服务器的令牌

jupyter notebook list

This will give you a URL with the token that you can copy/paste into any other browser. The output of the listcommand looks like

这将为您提供一个带有令牌的 URL,您可以将其复制/粘贴到任何其他浏览器中。list命令的输出看起来像

Currently running servers:
http://localhost:8888/?token=41429d3dcf554d0dde69498aac0950654a590664ba02b3cd :: /path/to/home/folder

So you can either type http://localhost:8888into the browser and then copy/paste the token into the field, or just copy/paste the whole URL with the token.

因此,您可以http://localhost:8888在浏览器中输入内容,然后将令牌复制/粘贴到字段中,也可以将整个 URL 与令牌一起复制/粘贴。

回答by user3113045

The following also works for me. I give it a full path to chrome, plus %sat the end.

以下也适用于我。我给了它一个完整的 chrome 路径,加上%s最后。

jupyter notebook --browser='C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

If chrome is in the PATH environment variable, the following might work too.

如果 chrome 在 PATH 环境变量中,则以下内容也可能有效。

jupyter notebook --browser=chrome

回答by Hansa Tharuka

Open anaconda prompt and type

打开 anaconda 提示符并输入

jupyter notebook --generate-config

then go to "jupyter_notebook_config.py" path and add following line

然后转到“jupyter_notebook_config.py”路径并添加以下行

c.NotebookApp.browser = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

回答by frhyme

On Mac this works:

在 Mac 上,这有效:

1) Generate a config file from within your environment:

1)从您的环境中生成一个配置文件:

jupyter notebook --generate-config

This will place jupyter_notebook_config.pyin ~/.jupyter.

这将放置jupyter_notebook_config.py~/.jupyter

2) Modify the following line in jupyter_notebook_config.py:

2)修改以下行jupyter_notebook_config.py

c.NotebookApp.browser = 'open -a /Applications/Google\ Chrome.app %s'

回答by Jacob Church

I'd like to offer a little more information about what to put in your jupyter_notebook_config.py file than is included in any of the other answers. jupyter is using python's webrowsermodule to launch the browser by passing the value for c.NotebookApp.browser to the webbrowser.get(using=None)function. If no value is specified, the function selects the user's default browser. If you do specify a value here, it can be interpreted in one of two ways, depending on whether or not the value you specified ends with the characters %s.

我想提供更多关于在 jupyter_notebook_config.py 文件中放置什么的信息,而不是包含在任何其他答案中的信息。jupyter 使用 python 的webrowser模块通过将 c.NotebookApp.browser 的值传递给webbrowser.get(using=None)函数来启动浏览器。如果未指定任何值,该函数将选择用户的默认浏览器。如果您确实在此处指定了一个值,则可以通过两种方式之一对其进行解释,具体取决于您指定的值是否以字符 结尾%s

If the string does not contain the characters %sit is interpreted as a browser name and the module checks if it has a browser registered with that name (see the python documentation for which browsers are registered by default). This is why Abhirup Das's answer works, first the webbrowser module is imported

如果字符串不包含字符,%s则将其解释为浏览器名称,并且模块会检查是否有使用该名称注册的浏览器(请参阅默认情况下注册浏览器的 Python 文档)。这就是 Abhirup Das 的答案有效的原因,首先导入 webbrowser 模块

import webbrowser

import webbrowser

the chrome browser is registered with the module

chrome 浏览器已注册到模块

webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'))

webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'))

and finally, the jupyter server is fed the browser name

最后,jupyter 服务器被提供浏览器名称

c.NotebookApp.browser = 'chrome'

c.NotebookApp.browser = 'chrome'

This browser registration does not persist, so the process must be repeated every time the server is launched.

此浏览器注册不会持续存在,因此每次启动服务器时都必须重复该过程。

Alternatively, if the string does contain the characters %s, it is interpreted as a literal browser command. Since this question is about how to run the browser on Windows, the browser command will probably contain backslashes. The backslash is used in python string literals to escape any characters that otherwise have any special meaning (e.g., to include a quote or double quote inside the string literal). Any backslashes in the browser command need to be be escaped or replaced. The easiest way is to replace the backslashes in the command with foward slashes, e.g.,

或者,如果字符串确实包含字符%s,则将其解释为文字浏览器命令。由于这个问题是关于如何在 Windows上运行浏览器,所以浏览器命令可能会包含反斜杠。反斜杠在 python 字符串文字中用于转义任何具有特殊含义的字符(例如,在字符串文字中包含引号或双引号)。浏览器命令中的任何反斜杠都需要转义或替换。最简单的方法是用正斜杠替换命令中的反斜杠,例如,

'C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe %s'

'C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe %s'

rather than

而不是

'C:\Home\AppData\Local\Google\Chrome\Application\chrome.exe %s'

'C:\Home\AppData\Local\Google\Chrome\Application\chrome.exe %s'

I for the life of me couldn't get unicode/raw string commands or commands where I escaped each backslash with an extra backslash to work, so replacing the backslashes with forward slashes may be the only option. I verified that the strings I tried all worked in python, so the only way to be sure would be to look at the jupyter source code.

我一生都无法获得 unicode/raw string 命令或命令,我用额外的反斜杠转义每个反斜杠才能工作,因此用正斜杠替换反斜杠可能是唯一的选择。我验证了我尝试的所有字符串都在 python 中工作,因此唯一确定的方法是查看 jupyter 源代码。

Anyway, since registering a browser with the module does not persist, if your browser isn't already registered by default, it is probably best to use a literal browser command with the backslashes replaced with forward slashes.

无论如何,由于向模块注册浏览器不会持续存在,如果您的浏览器默认尚未注册,最好使用文字浏览器命令,将反斜杠替换为正斜杠。

回答by NIathukorala

You don't need to change anything in the jupyter_notebook_config file. check whether your default web browser(if it's chrome) or reset and again choose as a web browser(chrome for me)as a default browser. it worked for me.

您无需更改 jupyter_notebook_config 文件中的任何内容。检查您的默认网络浏览器(如果是 chrome)还是重置,然后再次选择作为网络浏览器(对我来说是 chrome)作为默认浏览器。它对我有用。

回答by Tony Stark

After considerable thrashing about trying to launch a jupyter notebook in chrome from Anaconda in Win10 when chrome was not my default browser, I combined several of the suggestions above and, in the jupyter_notebook_config.py file under .jupyter in my home directory put in these lines in place of the default c.NotebookApp.browser line, and it finally worked!:

在 chrome 不是我的默认浏览器时,尝试在 Win10 中的 Anaconda 中启动 chrome 中的 jupyter notebook 经历了相当大的挣扎之后,我结合了上面的几个建议,并在我的主目录中的 .jupyter 下的 jupyter_notebook_config.py 文件中放入了这些行代替默认的 c.NotebookApp.browser 行,它终于奏效了!:

import webbrowser
webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:/PROGRA~2/Google/Chrome/Application/chrome.exe'))
c.NotebookApp.browser = 'chrome'

Note the use of Unix-style directory separators (this is apparently a bug in webbrowser) and the use of DOS-style "PROGRA~2" --- both of these seem to be necessary. Adding "%s" after "chrome.exe" seemed not to help.

请注意使用 Unix 风格的目录分隔符(这显然是 webbrowser 中的一个错误)和使用 DOS 风格的“PROGRA~2”——这两个似乎都是必要的。在“chrome.exe”之后添加“%s”似乎没有帮助。