使用选定的浏览器启动 IPython 笔记本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15632663/
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-08-18 20:35:11  来源:igfitidea点击:

Launch IPython notebook with selected browser

pythonwindowssubprocessipython

提问by user1043144

I am trying to start IPython with a non default browser (in my case Firefox) and thought I could replicate the replicate the script given in this blog

我正在尝试使用非默认浏览器(在我的情况下为 Firefox)启动 IPython,并认为我可以复制此博客中给出的脚本

I am on Windows 7

我在 Windows 7 上

I put the following code in a file say "module.py"

我将以下代码放在一个文件中,比如“module.py”

import subprocess
subprocess.call("ipython notebook --no-browser", shell=True)
subprocess.call([r'C:\Program Files (x86)\Mozilla Firefox\Firefox.exe', '-new-tab', 'http://127.0.0.1:8888/'])

However when I run it from the command line

但是,当我从命令行运行它时

 python C:\Users\mugabal\Desktop\module1.py

It execute the first line but not the second one (both lines work fine individually)

它执行第一行但不执行第二行(两行单独工作正常)

My question (in a more general term) how can I launch a process and tell it not to highHyman the console window?

我的问题(更笼统地说)如何启动进程并告诉它不要劫持控制台窗口?

I apologize in advance if I have overseen an obvious explanation but I looked both in the subprocess documentation and on this platform

如果我已经监督了一个明显的解释,我提前道歉,但我同时查看了子流程文档和这个平台

----- UPDATE -----

- - - 更新 - - -

I should have added that I tried to launch IPython with selected browser but could not figure out how to get it work

我应该补充一点,我试图用选定的浏览器启动 IPython,但不知道如何让它工作

>ipython notebook --browser='C:\Program Files (x86)\Mozilla Firefox\Firefox.exe'
... 
[NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888/
...
**[NotebookApp] No web browser found: could not locate runnable browser.**

To be precise, the following command in a Windows command prompt window works as expected:

准确地说,Windows 命令提示符窗口中的以下命令按预期工作:

start firefox 

but

ipython notebook --browser=firefox 

does not work (same error as above).

不起作用(与上述相同的错误)。

采纳答案by JPG

I had the same problem on windows and got it work this way:

我在 Windows 上遇到了同样的问题,并以这种方式工作:

  • Create a config file with command ipython profile create default

  • Edit ipython_notebook_config.py file, search for line

  • 使用命令创建配置文件 ipython profile create default

  • 编辑 ipython_notebook_config.py 文件,搜索行

#c.NotebookApp.browser =''

#c.NotebookApp.browser =''

and replace it with

并将其替换为

import webbrowser
webbrowser.register('firefox', None, webbrowser.GenericBrowser('C:\Program Files (x86)\Mozilla Firefox\firefox.exe'))
c.NotebookApp.browser = 'firefox'

then it works for me.

那么它对我有用。

Hope it will help you.

希望它会帮助你。

JPG

JPG

回答by Matt

Why not use

为什么不使用

--browser=<Unicode> (NotebookApp.browser)
    Specify what command to use to invoke a web browser when opening the
    notebook. If not specified, the default browser will be determined by the
   `webbrowser` standard library module, which allows setting of the BROWSER

回答by mbendhel

I uncomment this line and change into False, not to make ipython notebook open a web browser at start, so we can point the ipython notebook address in an active web browser.

我取消对这一行的注释并将其更改为 False,而不是让 ipython notebook 在启动时打开 web 浏览器,因此我们可以在活动的 web 浏览器中指向 ipython notebook 地址。

# Whether to open in a browser after starting. The specific browser used is
# platform dependent and determined by the python standard library `webbrowser`
# module, unless it is overridden using the --browser (NotebookApp.browser)
# configuration option.
c.NotebookApp.open_browser = False

Better still, I pin the address in my Firefox to make it active everytime I open the browser.

更好的是,我将地址固定在我的 Firefox 中,以使其每次打开浏览器时都处于活动状态。

回答by user3059413

I tried what JPG and norfeldt suggested. It worked perfectly on my Windows 7 computer. Here is a copy of the modified section of ipython_notebook_config.py (located under C:\Users\'your username'\.ipython to use Safari as the default browser for notebook. As norfeldt said, please notice the u before 'C:\...)

我尝试了 JPG 和 norfeldt 建议的内容。它在我的 Windows 7 计算机上完美运行。这是 ipython_notebook_config.py 的修改部分的副本(位于 C:\Users\'您的用户名'\.ipython 下以使用 Safari 作为笔记本的默认浏览器。正如 norfeldt 所说,请注意 'C:\ ...)

# c.NotebookApp.certfile = u''

import webbrowser
webbrowser.register('safari', None, webbrowser.GenericBrowser(u'C:\Program Files (x86)\Safari\safari.exe'))
c.NotebookApp.browser = 'safari'

回答by user1843683

I set the environment variable BROWSER to the the executable of the browser (in my case Google Chrome), and Ipython Notebook started in the browser I liked.

我将环境变量 BROWSER 设置为浏览器的可执行文件(在我的例子中是 Google Chrome),并且 Ipython Notebook 在我喜欢的浏览器中启动。

PS H:\> $env:BROWSER = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
PS H:\> $env:BROWSER
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
PS H:\>
PS H:\> ipython notebook
2015-02-19 14:05:01.690 [NotebookApp] Using existing profile dir: C:\Users\abc\.ipython\profile_default'
2015-02-19 14:05:01.832 [NotebookApp] Using MathJax from CDN: http://cdn.mathjax.org/mathjax/latest/MathJax.js
2015-02-19 14:05:01.901 [NotebookApp] The port 8888 is already in use, trying another random port.
2015-02-19 14:05:01.908 [NotebookApp] Serving notebooks from local directory: H:\
2015-02-19 14:05:01.908 [NotebookApp] 0 active kernels
2015-02-19 14:05:01.910 [NotebookApp] The IPython Notebook is running at: http://localhost:8889/
2015-02-19 14:05:01.910 [NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

回答by Ye Xu

Without coding, you can just set your default browser to Chrome or Firefox etc. It works for my windows system.

无需编码,您只需将默认浏览器设置为 Chrome 或 Firefox 等。它适用于我的 Windows 系统。

回答by JBWhitmore

On my mac, I got the following command to use Firefox instead of my default Chrome:

在我的 Mac 上,我收到了以下命令来使用 Firefox 而不是我的默认 Chrome:

jupyter notebook --browser firefox

回答by Antoni Parellada

This is not a real answer. I just want to share with the less computer savvy what JPG's answer looks like step-by-step. Presumably, on Windows Explorer (screen capture attached below), the file jupyter_notebook_config.pyis listed:

这不是一个真正的答案。我只想与不太懂电脑的人分享 JPG 的答案一步一步的样子。据推测,在 Windows 资源管理器(下面附有屏幕截图)上,jupyter_notebook_config.py列出了该文件:

enter image description here

在此处输入图片说明

In my case, the directory for the file (on top menu of Explorer) was C:\Users\My_name\.jupyter

就我而言,文件的目录(在资源管理器的顶部菜单上)是 C:\Users\My_name\.jupyter

The second part of the answer can be implemented by simply pasting:

答案的第二部分可以通过简单粘贴来实现:

import webbrowser
webbrowser.register('firefox', None, webbrowser.GenericBrowser('C:\Program Files (x86)\Mozilla Firefox\firefox.exe'))
c.NotebookApp.browser = 'firefox'

in the space on the space seen on the screen capture below, corresponding to the jupyter_notebook_config.pyopened within PyCharm:

在下面的屏幕截图中看到的空间上的空间中,对应jupyter_notebook_config.py于在 PyCharm 中打开的:

enter image description here

在此处输入图片说明

... only that I set it up to open in Opera:

...只是我将它设置为在 Opera 中打开:

import webbrowser
webbrowser.register('opera', None, webbrowser.GenericBrowser('C:\Program Files (x86)\Opera\launcher.exe'))
c.NotebookApp.browser = 'opera'

回答by sanketec87

When you execute the below code in command prompt it gives the result link which you can copy in any browser to open Jupiter notebook.

当您在命令提示符下执行以下代码时,它会提供结果链接,您可以在任何浏览器中复制该链接以打开 Jupiter Notebook。

jupyter notebook --browser firefox