Changing browser for IPython Notebook from system

2020-05-25 08:00发布

问题:

I would like to keep firefox as my system default browser on my Mac, but launch IPython Notebook in Chrome[1].

This answer led me to my ipython_notebook_config.py file but I can't get an instance of Chrome running. After c = get_config() and import webbrowser, I've tried:

  1. webbrowser.register(u'chrome', None, webbrowser.Chrome())

  2. webbrowser.register(u'chrome', webbrowser.Chrome)

  3. webbrowser.register(u'chrome', None, webbrowser.GenericBrowser('/Applications/Browsers/Chrome.app'))

  4. webbrowser.register(u'chrome', None, webbrowser.GenericBrowser('/Applications/Browsers/Chrome.app/Contents/MacOS/Google\ Chrome'))

All followed by c.NotebookApp.browser = u'chrome'

I've fiddled with webbbrowser in the interpreter, and couldn't figure out how to create an instance of Chrome.


[1]: PS Why is IPython Notebook so slow in firefox, especially for pylab with the inline backend? It's orders of magnitude faster (for rendering, scrolling, etc) in chrome.

回答1:

Since the great switch to Jupyter, and with recent versions of OS X (e.g., Yosemite), Jupyter/iPython (e.g., 4.0.1), and Chrome (e.g., 47), things have changed a bit. Jupyter/iPython no longer puts the notebook config file in ~/.ipython; it's now in ~/.jupyter, and the default file is generated with

jupyter notebook --generate-config

If you have an existing ipython_notebook_config.py you can migrate it with jupyter migrate (H/T).

After generating or migrating your config file, add the following line to jupyter_notebook_config.py:

c.NotebookApp.browser = u'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome %s'


回答2:

Based on this answer, (running Python 2.7.3 and IPython-0.13.1 on Linux), all I had to set in my ipython_notebook_config.py was

c.NotebookApp.browser = u'/usr/bin/google-chrome %s'

I'm guessing, setting c.NotebookApp.browser to /Applications/Browsers/Chrome.app/Contents/MacOS/Google Chrome %s should work for you.



回答3:

On OS X, you can put the following in ipython_notebook_config.py to open Chrome:

c.NotebookApp.browser = u'/usr/bin/open -a Google\\ Chrome %s'

The executable in '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' fails for me with 'unable to obtain profile lock', so going through 'open' is the only simple alternative I see.



回答4:

This might not be the right things to do , but

$ open -a Google\ Chrome http://localhost:8888
$ open -a Firefox http://localhost:8888

Works from me (only on mac) to open any url in one of the 2 browser.

Use the --no-browser option and make an bash function that does that. Or even have a bookmark in Chrome.



回答5:

For people who want to make firefox their default for ipython notebooks (where it is not necessarily the system default), adding the following line to ipython_notebook_config.py should be sufficient:

c.NotebookApp.browser = 'Firefox'

For me, this was better than linking to the application file directly because it avoids the error: A copy of Firefox is already open. Only one copy of Firefox can be open at a time.



回答6:

This worked for me on OSX Mavericks:

c.NotebookApp.browser = u'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome %s'


回答7:

For future reference, this works looks the most elegant way to edit jupyter_notebook_config.py for me on macOS:

c.NotebookApp.browser = u'open -a "Google Chrome" %s'

You can obviously replace "Google Chrome" with any other browser.

Full procedure:

  1. jupyter notebook --generate-config
  2. open ./jupyter/jupyter_notebook_config.py
  3. Find the line #c.NotebookApp.browser and edit it as above


回答8:

If you don't want to open the browser at all, you can add ipython notebook --no-browser.