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

后端 未结 19 2296
盖世英雄少女心
盖世英雄少女心 2020-12-04 10:17

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

I have a local installation of the Ana

相关标签:
19条回答
  • 2020-12-04 10:57

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

    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.

    0 讨论(0)
  • 2020-12-04 10:57

    In my case, macOS 10.15.4 with anaconda 1.9.12, finally, I found an effective one as below:

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

    I hope this helps someone. :-)

    0 讨论(0)
  • 2020-12-04 10:58

    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 webrowser module 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.

    If the string does not contain the characters %s it 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

    import webbrowser

    the chrome browser is registered with the module

    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

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

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

    rather than

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

    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.

    0 讨论(0)
  • 2020-12-04 10:58

    On Mac this works:

    1) Generate a config file from within your environment:

    jupyter notebook --generate-config
    

    This will place jupyter_notebook_config.py in ~/.jupyter.

    2) Modify the following line in jupyter_notebook_config.py:

    c.NotebookApp.browser = 'open -a /Applications/Google\ Chrome.app %s'
    
    0 讨论(0)
  • 2020-12-04 10:59

    Find .../jupyter/runtime/nbserver-11596-open.html file, or whatever the file name is, you can find the file name when jupyter notebook starts, and associate it with Chorme worked for me.

    0 讨论(0)
  • 2020-12-04 11:01

    Jupyter looks for the BROWSER environment variable when choosing which browser to launch.

    I recommend setting BROWSER over configuring Jupyter specifically, because setting BROWSER is the default way to specify which browser you prefer, regardless of which application it applies to.

    • To choose the browser for a single session, set the BROWSER environment variable when running the jupyter process.

      BROWSER=chromium-browser jupyter notebook when you have chromium-browser when you prefer to use chromium-browser on PATH. Typical for Linux.

      BROWSER=C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe jupyter notebook when you don't have the application on PATH. Typical for Windows.

      BROWSER=<your browser> jupyter notebook otherwise.

    • To choose browser for your whole system, set the BROWSER environment variable globally.

    0 讨论(0)
提交回复
热议问题