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

System default browser not used, because serverapp started as "opening a local html file" #1488

Open
MichaelClerx opened this issue Jan 4, 2025 · 10 comments
Labels

Comments

@MichaelClerx
Copy link

MichaelClerx commented Jan 4, 2025

Description

On a fresh install of Fedora 41 (gnome 47) I suddenly found that the jupyter notebook command opened chrome, not my system default of firefox. This was really confusing as I couldn't see any configuration files, environment variables or anything else telling it to do this, and webbrowser.open('https://google.com') would load firefox as expected.

Reproduce

Fresh install of recent linux, including multiple browsers.
Change default browser

After a lot of searching, I discovered it was because the default browser used by webbrowser is xdg-open, which will use the system default browser for http://..., but the default application for the .html extension for local files file://....

And because jupyter was starting as
file:///home/michael/.local/share/jupyter/runtime/jpserver-10052-open.html
this was treated as a local "opening a html file" action...

This gets worse, because if you do web development you might want the local "opening a html file" to be opening it in a text editor...

Expected behavior

Expecting notebook to "open a page in the default browser".
Instead it "perfoms the default action for opening a local html file"

Obvious solution would be to use webbrowser.open('http://localhost:8888/tree?...
instead of getting there via a local file?

Context

Operating System and version: Fedora 41
Browser and version: Firefox and chrome
Jupyter Server version: 2.14.2

@minrk
Copy link
Contributor

minrk commented Jan 6, 2025

See jupyter/notebook#4304 for the longstanding report for this, and various things folks have tried, and workarounds you can use. I think if xdg-settings get default-web-browser works, the right thing will happen.

This seems like we should report a bug to the standard library if there isn't one already, because xdg-open is not a safe assumption for file:// URLs.

@minrk
Copy link
Contributor

minrk commented Jan 6, 2025

Opened upstream issue: python/cpython#128540

I think we can work around this if we register our own default browser lookup, but I'm not sure how easy that is going to be. Linux actually seems to be the hardest, since the most standard mechanism I can think of is already used there (xdg-settings get default-web-browser, but I guess it doesn't always work).

What do you get from xdg-settings get default-web-browser btw?

@MichaelClerx
Copy link
Author

michael@fedora:~$ xdg-settings get default-web-browser
org.mozilla.firefox.desktop

But it doesn't use the default web browser for .html files: it uses whatever the default application for .html is (so default browser in this case is irrelevant)

@minrk
Copy link
Contributor

minrk commented Jan 6, 2025

Hm, I thought the default-web-browser approach was supposed to be preferred to xdg-open. What do you get from webbrowser.get().name?

And the result of subprocess.check_output(["org.mozilla.firefox.desktop"])?

@MichaelClerx
Copy link
Author

MichaelClerx commented Jan 6, 2025

xdg correctly loads firefox, and the "name" it reports is not a command:

$ xdg-settings get default-web-browser
org.mozilla.firefox.desktop
$ org.mozilla.firefox.desktop
bash: org.mozilla.firefox.desktop: command not found

$ xdg-open https://github.com --> Opens in firefox

The webbrowser module reports xdg-open as the default:

$ python -c "import webbrowser; print(webbrowser.get().name)"
xdg-open

@minrk
Copy link
Contributor

minrk commented Jan 6, 2025

I think this is a bug in webbrowser's handling of xdg-settings, then, because that's how it tries to look up the default browser. It executes the result of xdg-settings get default-web-browser and then falls back on xdg-open when that fails.

I'm not sure what the right xdg invocation is to say "open this url with this specific application"

@MichaelClerx
Copy link
Author

MichaelClerx commented Jan 6, 2025

Could even be a bug in xdg-open:

$ xdg-open --manual
Name

   xdg-open - opens a file or URL in the user's preferred
   application

Synopsis

   xdg-open { file | URL }

   xdg-open { --help | --manual | --version }

Description

   xdg-open opens a file or URL in the user's preferred
   application. If a URL is provided the URL will be opened in the
   user's preferred web browser. If a file is provided the file
   will be opened in the preferred application for files of that
   type. xdg-open supports file, ftp, http and https URLs.

where, in my reading, the final bit says a file://... string is a URL, not a file, and should therefore be opened in a browser

xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser. If a file is provided the file will be opened in the preferred application for files of that
type. xdg-open supports file, ftp, http and https URLs.

@MichaelClerx
Copy link
Author

$ org.mozilla.firefox.desktop
bash: org.mozilla.firefox.desktop: command not found

I guess you're meant to use this desktop entry, but that doesn't seem possible in a linux-flavour independent way: https://wiki.archlinux.org/title/Desktop_entries

@MichaelClerx
Copy link
Author

On my system you could do gtk4-launch $(xdg-settings get default-web-browser) https://github.com but this only works because the default browser .desktop file is in /usr/share/applications (see website quoted above)

@minrk
Copy link
Contributor

minrk commented Jan 6, 2025

I've been reading into this a bit today, and there doesn't seem to be anything approaching consensus on locating and invoking applications, even on what works and what doesn't.

For example, the limitation about gtk-launch search path doesn't even necessarily seem true, because I just did a fresh install of ubuntu 24.04 and

gtk-launch $(xdg-settings get default-web-browser) file://$PWD/file.html

and it launches firefox.desktop, which resides in /var/lib/snapd/desktop/applications/. It also works for .desktop files in ~/.local/share/applications.

So gtk-launch does seem to work with the output of xdg-settings get default-web-browser, at least sometimes, but I have no idea how general that is (obviously not when gtk-launch is unavailable).

gio launch appears to also work with absolute paths, but not by name. I don't see any simple API that appropriately resolves name.desktop -> /path/to/name.desktop, but I assume it must exist.

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

No branches or pull requests

2 participants