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

时间:2013-03-26 08:43:25

标签: python windows subprocess ipython

我正在尝试使用非默认浏览器启动IPython(在我的情况下是Firefox) 并且我认为我可以复制给定in this blog

的脚本

我使用的是Windows 7

我将以下代码放在一个文件中,说“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/'])

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

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

它执行第一行而不是第二行(两行都可以单独工作)

我的问题(用更通用的术语)我如何启动一个进程并告诉它不要高举控制台窗口?

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

-----更新-----

我应该补充一点,我尝试用选定的浏览器启动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.**

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

start firefox 

但是

ipython notebook --browser=firefox 

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

10 个答案:

答案 0 :(得分:65)

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

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

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

#c.NotebookApp.browser =''

并将其替换为

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

然后它适合我。

希望它会对你有所帮助。

JPG

答案 1 :(得分:22)

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

jupyter notebook --browser firefox

答案 2 :(得分:9)

为什么不使用

--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

答案 3 :(得分:8)

这不是一个真正的答案。我只是想与精通计算机的人分享一下,JPG的答案看起来是一步一步的。据推测,在Windows资源管理器(下面附带的屏幕截图)中,列出了文件jupyter_notebook_config.py

enter image description here

在我的例子中,文件的目录(在资源管理器的顶层菜单上)是C:\Users\My_name\.jupyter

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

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

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

enter image description here

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

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

答案 4 :(得分:5)

我取消注释此行并更改为False,而不是让ipython笔记本在开始时打开Web浏览器,因此我们可以将ipython笔记本地址指向活动的Web浏览器。

# 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

更好的是,我在我的Firefox中固定地址,以便每次打开浏览器时都将其激活。

答案 5 :(得分:1)

我将环境变量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).

答案 6 :(得分:1)

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

jupyter notebook --browser firefox

答案 7 :(得分:0)

我尝试了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'

答案 8 :(得分:0)

要使其与 bravechrome 一起使用,您需要附加一个表示 URL 的 %s 参数。在 Linux 终端上测试了以下内容并且效果很好:

jupyter notebook --browser="google-chrome %s"
jupyter notebook --browser="brave-browser %s"

firefox 不需要 %s

jupyter notebook --browser=firefox

答案 9 :(得分:-2)

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

相关问题