jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.58k stars 4.86k forks source link

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

Open yuvipanda opened 5 years ago

yuvipanda commented 5 years ago

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?

bharathgs commented 5 years ago

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

yuvipanda commented 5 years ago

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

david-waterworth commented 5 years ago

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.

jasongrout commented 5 years ago

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

yuvipanda commented 5 years ago

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

takluyver commented 5 years ago

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 commented 5 years ago

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

yuvipanda commented 5 years ago

@takluyver I'm on Ubuntu Linux

bharathgs commented 5 years ago

@takluyver platform:Ubuntu 18.10

takluyver commented 5 years ago

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?

minrk commented 5 years ago

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 commented 5 years ago

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.

takluyver commented 5 years ago

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.

yuvipanda commented 5 years ago
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)

ncryer commented 5 years ago

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

naushadS commented 5 years ago

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
boblitex commented 5 years ago

@naushadS that does the trick, wonderful

xvdp commented 5 years ago

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.

filipedwan commented 5 years ago

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

sgornick commented 5 years ago

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

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

MaheshSuranga commented 5 years ago

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

sergeleger commented 5 years ago

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.

mohitpandey92 commented 5 years ago

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?

rrdipnie commented 5 years ago

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

amirvba commented 4 years ago

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.

lcmuniz commented 4 years ago

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

rrdipnie commented 4 years ago

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

On Wed, May 13, 2020, 20:50 Luiz Carlos Muniz <notifications@github.com 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 .

preeti040211 commented 3 years ago

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

neginkheirmand commented 3 years ago

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

yup, this one works

Cornstars commented 3 years ago

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!

tonyfast commented 2 years ago

Thanks for reporting this issue. This is something we will need to be aware of for Notebook 7.0 based on JupyterLab. It seems like these could annoy some folks. We may need to update the documentation to include these work arounds.

DevsidB commented 2 years ago

Best Method: Jupyter not opening in browser but opens in some other software (vscode, pdf viewer etc)

  1. Search for any .html files in file explorer
  2. Right click on the file > open with > set default to any browser
  3. See your Jupyter open in the default browser when called from powershell.
Samarth12121 commented 1 year ago

I am on windows and was facing the same issue. Simply go to default app in settings and set your browser as the default option. You will notice that for .html files VS code was earlier set as the default. Just choose your preferred browser as the default.

jtpio commented 1 year ago

Thanks @Samarth12121 for commenting.

Maybe this could be added to the documentation in a FAQ / Troubleshooting section?

Lexi-Jimenez commented 1 year ago

I also had this problem on my windows. Every time I launched "Jupyter notebook" from Anaconda, it opened an HTML file in VS Code! I think it happened because I had set the default program for opening html files to be VS code!

Here is what I did to resolve it:

  1. Opened Windows Settings
  2. Opened "Apps"
  3. Opened "Default Apps"
  4. Searched for "HTML" and changed VS code as the default to Chrome app.

Once I did this, I could launch Jupyter Notebook and it immediately opened in my Chrome Browser.

AkashhRaut commented 1 year ago

@naushadS Thanks worked for me....really helpful

antun commented 9 months ago

What's the reason for opening the redirection HTML (e.g. jpserver-3468-open.html) page itself? Why not just open the http://localhost:8888/... directly when the notebook server starts?

Shah907 commented 9 months ago

found a 100% Solution for this and it's too simple. Just follow the tips.

As you launch the Jupyter Notebook with the Anaconda, if It opens with VS Code or other. And then Follow my Steps to fix it.

  1. Locate the HTML file from VS Code
  2. Go to the Destination of that File
  3. Right-click on the file to open with.
  4. Click on Choose Another App, And check the Box "Always use this app to open .html"
  5. Open the file with any browser you want and click Ok

Or Follow on this source : deepdecide

MrSuradechTH commented 2 days ago

Just set chrome to defualt open .html file

image