notebook icon indicating copy to clipboard operation
notebook copied to clipboard

Running `jupyter notebook` opens visual studio code, not a browser

Open yuvipanda opened this issue 5 years ago • 38 comments

If I run jupyter notebook, it pops up an empty instance of vscode for me - rather than a browser. I believe this is because .html files are opened in vscode on my system, rather than via the browser.

It took me a few tries to figure out why this was happening. We should maybe have better browser detection?

yuvipanda avatar Dec 27 '18 20:12 yuvipanda

@yuvipanda is this resolved? if so what did you do?

bharathgs avatar Jan 05 '19 16:01 bharathgs

@bharathgs nope! I just run --no-browser now and copy the URL manually

yuvipanda avatar Jan 05 '19 16:01 yuvipanda

Same thing happened to me - I suspect vscode has been set as the default handler for html files and jupyter notebook uses the default association.

david-waterworth avatar Jan 09 '19 04:01 david-waterworth

We use the python webbrowser module to pop up the browser. You can check with something like python -m webbrowser -t "http://www.python.org" (but perhaps with the notebook url instead).

You can also set the BROWSER environment variable for your command invocation, with something like:

BROWSER=/path/to/browser jupyter notebook

See https://docs.python.org/3.7/library/webbrowser.html

jasongrout avatar Jan 09 '19 07:01 jasongrout

@jasongrout 'python -m webbrowser -t "http://www.python.org"' loads in a browser, but python -m webbrowser -t "$(pwd)/a.html" opens in a text editor. I think this is the general problem.

I don't personally have a problem with setting browser environment, but this means the default experience for people who are told to run 'jupyter lab' on their terminal and a 'browser will pop up' is going to fail. That seems confusing and unideal.

yuvipanda avatar Jan 10 '19 00:01 yuvipanda

Gah, that's annoying. Which platform(s) are people seeing that on?

I had assumed webbrowser would always launch a browser, but it appears that it can delegate file:// URLs to the platform's native file handling. I don't know how to get around that. There are real security benefits to using the local file rather than launching the browser directly to http://localhost... with the security token.

takluyver avatar Jan 16 '19 10:01 takluyver

We can't launch the browser to an HTTP URL which then redirects to a file, because redirecting to file:// URLs is not allowed.

takluyver avatar Jan 16 '19 13:01 takluyver

@takluyver I'm on Ubuntu Linux

yuvipanda avatar Jan 16 '19 22:01 yuvipanda

@takluyver platform:Ubuntu 18.10

bharathgs avatar Jan 17 '19 03:01 bharathgs

OK, I'm assuming that the webbrowser module is calling xdg-open, while is a wrapper around the desktop's open command (e.g. gio open ... on GNOME), and that calling that with a file:// URL is equivalent to calling it with a path, so it opens the .html file with the default handler.

Can you both check import webbrowser; webbrowser.get().name?

I haven't thought of any good way to avoid this, given that launching with file:// URL is a nice security improvement. Probably the best we can do is try to detect that the default browser is something like xdg-open and override the normal logic to find a browser. The downside is that we may launch the 'wrong' browser, not the default the user has configured in their system, in cases where we currently get the right one. But it should at least ensure that most users get a browser rather than an editor.

Of course, with problems like this it's always tempting to add a config option to 'do it the old way'. I'm trying to avoid that. Settings shouldn't make it too easy to remove security, because in a few years people will follow instructions they find to change the setting without understanding the implications. You can already solve this problem with configuration by setting e.g. c.NotebookApp.browser = 'firefox'. So it's only about how to handle the default, unconfigured case.

Any better ideas before I start trying to code a workaround?

takluyver avatar Jan 18 '19 12:01 takluyver

I can reproduce this on mac if I associate html files with an editor (doesn't appear to be the default). Mac uses webbrowser.MacOSXOSAScript by default (no .name attr), which uses open, the equivalent of xdg-open.

I think discovering the default browser on all platforms is going to be a pretty big pain, but I can look into it on mac, at least.

minrk avatar Jan 18 '19 15:01 minrk

Found the mac API for discovering the default browser and calling it via ctypes: https://nbviewer.jupyter.org/gist/minrk/0ea42a1d4bafde85c7699bac84d078a1

not sure if there's a better way.

minrk avatar Jan 18 '19 15:01 minrk

For Linux, the webbrowser module is already calling xdg-settings get default-web-browser to find a default; on my Fedora system, that makes it explicitly launches Firefox rather than xdg-open. But I guess for @yuvipanda and @bharathgs either the setting isn't set or the command to read it isn't available.

I was thinking of making a priority order on Linux something like this:

  1. BROWSER environment variable
  2. xdg-settings default browser
  3. Firefox & variants like icecat
  4. Chromium & variants
  5. xdg-open

I don't know how many users would end up with a browser that's not their preferred default that way, but hopefully it's easy to configure your way out of that.

The situation on Mac seems more complex.

takluyver avatar Jan 18 '19 15:01 takluyver

14:42 $ xdg-settings get default-web-browser
/usr/bin/xdg-mime: 957: local: Edition-24TARZ.desktop: bad variable name

and

14:43 $ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import webbrowser
>>> webbrowser.get().name
'xdg-open'
>>> 

on my machine (Ubuntu 18.10 + GNOME)

yuvipanda avatar Jan 18 '19 22:01 yuvipanda

I had the same issue on Ubuntu linux. webbrowser.get().name defaulted to 'xdg-open', and I addressed the issue by adding export BROWSER=/usr/bin/firefox to .bash_profile, sourcing it and running jupyter lab

ncryer avatar Jan 21 '19 11:01 ncryer

Best way to solve this problem :

  1. Create a simple html file (with content as : <html> </html>).
  2. In file explorer, right-click on this file > properties > opens with > set any browser as default application (to open html files).
  3. Launch jupyter notebook/lab from console.
  4. See it opening in browser.
  5. Profit

naushadS avatar Feb 07 '19 10:02 naushadS

@naushadS that does the trick, wonderful

boblitex avatar Feb 14 '19 15:02 boblitex

Same thing happened to me yesterday - after an odd event in my ubuntu 16. I opened ubuntu after leaving it on overnight. It was logged in as guest instead as myself ( nobody touched the machine). My working conda environment was gone, and launching jupyter notebook defaulted to visual studio code where before it launched firefox. Inside my conda environmnet, python, I checked

>>> webbrowser.get().name 
'xdg-open'

then in bash I checked

$ xdg-settings get default-web-browser
firefox.desktop

I went back to python, this time outside my environment:

>>> webbrowser.get().name 
'firefox'

So, clearly its in my conda enviro. So generated a config jupyter notebook --generate-config then looked for the line #c.NotebookApp.browser = '' and changed it to c.NotebookApp.browser = '/usr/lib/firefox/firefox'

Fixed.

xvdp avatar Mar 08 '19 21:03 xvdp

@naushadS, it worked for me. Smart move ;-) Thank you!

filipedwan avatar Mar 27 '19 17:03 filipedwan

Happens with Azure Data Studio, which started from a fork of Visual Studio Code as well.

https://github.com/Microsoft/azuredatastudio/issues/3108

sgornick avatar Apr 06 '19 21:04 sgornick

Best way to solve this problem :

  1. Create a simple html file (with content as : <html> </html>).
  2. In file explorer, right-click on this file > properties > opens with > set any browser as default application (to open html files).
  3. Launch jupyter notebook/lab from console.
  4. See it opening in browser.
  5. Profit

that trick does well. Thank you very much. You save a lot of time

MaheshSuranga avatar May 27 '19 08:05 MaheshSuranga

The above did not work for me (Ubuntu 18.04). In my case the issue was not the .html file association, but the handling of the file:// URL scheme. Setting it properly with:

xdg-settings set default-url-scheme-handler file google-chrome.desktop

worked.

For Firefox you can use firefox.desktop.

sergeleger avatar Jun 07 '19 18:06 sergeleger

Hello everyone, I faced the same problem on Ubuntu 18.04 and it got solved after using @naushadS's solutions. I suspect that during installation of Anaconda, my default software for HTML files was changed to vscode. Does anyone know if it was done intentionally to promote vscode?

mohitpandey92 avatar Aug 15 '19 15:08 mohitpandey92

Same thing happened to me yesterday - after an odd event in my ubuntu 16. I opened ubuntu after leaving it on overnight. It was logged in as guest instead as myself ( nobody touched the machine). My working conda environment was gone, and launching jupyter notebook defaulted to visual studio code where before it launched firefox. Inside my conda environmnet, python, I checked

>>> webbrowser.get().name 
'xdg-open'

then in bash I checked

$ xdg-settings get default-web-browser
firefox.desktop

I went back to python, this time outside my environment:

>>> webbrowser.get().name 
'firefox'

So, clearly its in my conda enviro. So generated a config jupyter notebook --generate-config then looked for the line #c.NotebookApp.browser = '' and changed it to c.NotebookApp.browser = '/usr/lib/firefox/firefox'

Fixed.

it happens to my Windows 7, everytime i open jupyter notebook i will get a link with different token and it launch at Sublime. I already follow the step above but still failed. bcs it comes back to the default.. Please help.. thankyou image

rrdipnie avatar Sep 04 '19 10:09 rrdipnie

I also had this problem on my windows. Every time I ran "jupyter notebook" in commad line, it opened a text file in sublime text! It think it happend because I had set the default program for opening html files to be sublime text!

But I could solve it:)

Just go to the windows setting and then go to "default apps". And then click on "reset". It solved the problem for me.

amirvba avatar Apr 02 '20 16:04 amirvba

I followed the following steps:

I created the configuration file: jupyter notebook --generate-config I edited the file vi ~ / .jupyter / jupyter_notebook_config.py I uncommented the line below and changed it from True to False: Before: # c.NotebookApp.use_redirect_file = True After: c.NotebookApp.use_redirect_file = False

lcmuniz avatar May 13 '20 13:05 lcmuniz

yeah thankyou, lol its already solved from long time ago hehe..

On Wed, May 13, 2020, 20:50 Luiz Carlos Muniz <[email protected] wrote:

I followed the following steps:

I created the configuration file: jupyter notebook --generate-config I edited the file vi ~ / .jupyter / jupyter_notebook_config.py I uncommented the line below and changed it from True to False: Before: # c.NotebookApp.use_redirect_file = True After: c.NotebookApp.use_redirect_file = False

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jupyter/notebook/issues/4304#issuecomment-627999735, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHQZTHWIHCTMZJ7OZAESDQ3RRKQRXANCNFSM4GMMTBJQ .

rrdipnie avatar May 13 '20 13:05 rrdipnie

Best way to solve this problem :

  1. Create a simple html file (with content as : <html> </html>).
  2. In file explorer, right-click on this file > properties > opens with > set any browser as default application (to open html files).
  3. Launch jupyter notebook/lab from console.
  4. See it opening in browser.
  5. Profit

Perfect and easy solution thanks

preeti040211 avatar Jan 09 '21 07:01 preeti040211

@bharathgs nope! I just run --no-browser now and copy the URL manually

yup, this one works

neginkheirmand avatar Feb 18 '21 16:02 neginkheirmand

Best way to solve this problem :

  1. Create a simple html file (with content as : <html> </html>).
  2. In file explorer, right-click on this file > properties > opens with > set any browser as default application (to open html files).
  3. Launch jupyter notebook/lab from console.
  4. See it opening in browser.
  5. Profit

that trick does well. Thank you very much. You save a lot of time

Thanks you very much!

Cornstars avatar Mar 29 '21 20:03 Cornstars