Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yuvipanda opened this issue Dec 27, 2018 · 39 comments
Open

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

yuvipanda opened this issue Dec 27, 2018 · 39 comments
Milestone

Comments

@yuvipanda
Copy link
Contributor

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

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

@yuvipanda
Copy link
Contributor Author

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

@david-waterworth
Copy link

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
Copy link
Member

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
Copy link
Contributor Author

@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 takluyver added this to the 5.7.5 milestone Jan 16, 2019
@takluyver
Copy link
Member

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
Copy link
Member

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
Copy link
Contributor Author

@takluyver I'm on Ubuntu Linux

@bharathgs
Copy link

@takluyver platform:Ubuntu 18.10

@takluyver
Copy link
Member

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
Copy link
Member

minrk commented Jan 18, 2019

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
Copy link
Member

minrk commented Jan 18, 2019

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
Copy link
Member

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
Copy link
Contributor Author

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

ncryer commented Jan 21, 2019

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

naushadS commented Feb 7, 2019

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

@naushadS that does the trick, wonderful

@minrk minrk modified the milestones: 5.7.5, 5.8 Mar 6, 2019
@xvdp
Copy link

xvdp commented Mar 8, 2019

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

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

@sgornick
Copy link

sgornick commented Apr 6, 2019

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

microsoft/azuredatastudio#3108

@MaheshSuranga
Copy link

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

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.

@lresende lresende modified the milestones: 5.8, 6.0 Jun 24, 2019
@lresende lresende added this to the 5.8 milestone Jun 24, 2019
@mohitpandey92
Copy link

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

rrdipnie commented Sep 4, 2019

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

amirvba commented Apr 2, 2020

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

lcmuniz commented May 13, 2020

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

rrdipnie commented May 13, 2020 via email

@preeti040211
Copy link

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

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

yup, this one works

@Cornstars
Copy link

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 tonyfast modified the milestones: 5.8, 7.0 Feb 10, 2022
@tonyfast
Copy link
Collaborator

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

DevsidB commented Aug 18, 2022

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.

@jtpio jtpio modified the milestones: 7.0, 7.0.x Jun 9, 2023
@Samarth12121
Copy link

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
Copy link
Member

jtpio commented Jun 23, 2023

Thanks @Samarth12121 for commenting.

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

@Lexi-Jimenez
Copy link

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

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

@antun
Copy link

antun commented Dec 1, 2023

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

Shah907 commented Dec 19, 2023

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

Just set chrome to defualt open .html file

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests