25

Is there a way to cause a programmatically generated url to open in a new browser tab or window from an IPython notebook cell?

Upon execution of the notebook cell the result should be the opening of a new tab or window pointing to the generated link.

NOTE: When I just return an IPython.core.display.HTML instance with a hyperlink the link is broken. If the url is copied and pasted into a browser window it is valid.

Mike
  • 2,637
  • 5
  • 29
  • 40

3 Answers3

30

When you work with your standard browser, you can use the webbrowser module:

import webbrowser

# generate an URL
url = 'https://' + 'www.google.com'
webbrowser.open(url)
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • 3
    I dont think it will work, since the python code is running in a web server and you are just visualizing and editing it with your web browser – guilhermecgs Jan 19 '17 at 11:40
  • 1
    Did you try? It works. Just copy-paste the code in your Notebook and see what happens. – Mike Müller Jan 19 '17 at 11:47
  • 1
    actually I did... It did not work... Maybe I am doing something wrong, but I get a FALSE boolean as return – guilhermecgs Jan 19 '17 at 12:03
  • 1
    It works for me on Mac and Chrome. Maybe you don't have a default webbrowser defined. Try setting the environment variable BROWSER to your browser. – Mike Müller Jan 19 '17 at 12:12
  • just to be clear... My computer is a Windows PC. The jupyter notebook is running in a docker container in a remote unix server... maybe the remote server browser is not defined – guilhermecgs Jan 19 '17 at 12:15
  • 2
    OK. My answer is for the "normal" usage. Run the Notebook server locally. No remote server, no Docker. – Mike Müller Jan 19 '17 at 12:19
  • 1
    Google colab is a notebook-like application, not exactly the same as Jupyter. Therefore, it has some difference here and there. – Mike Müller May 03 '19 at 09:35
  • 1
    I run my jupyter server on a remote node and then open it in my local computer's browser. Is there a way to open a new tab on my local computer's browser from the notebook? – DVL Feb 21 '20 at 01:31
  • Is it possible to also open a website remotely? re: this question https://stackoverflow.com/q/72507242/10693596 – SultanOrazbayev Jun 10 '22 at 14:57
  • It seems to not works in Google Colab? I'm using Brave browser. – Muhammad Yasirroni Dec 18 '22 at 01:37
  • Works only locally. See my remarks above. – Mike Müller Feb 06 '23 at 07:10
6

You can use javascript to open the link client-side. It should work on remote servers because the tab-opening occurs in the user's browser instead of on the server.

This simple code snippet uses window.open() to open a new tab/popup with your desired url.

from IPython.display import Javascript

def window_open(url):
    display(Javascript('window.open("{url}");'.format(url=url)))
Michael Noguera
  • 401
  • 4
  • 14
1

Based on https://stackoverflow.com/a/61900572/7733418 the answer by Michael, I provide a variant to fix the module not callable error:

import IPython

def window_open(url):
    IPython.display.display(IPython.display.Javascript('window.open("{url}");'.format(url=url)))
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Conor McM
  • 21
  • 3
  • Please, share why it works – Kfcaio Apr 04 '22 at 16:15
  • The module not callable error happens because somewhere in Cybernetic's code IPython.display (a module) has been imported but Michael's code assumes that IPython.display.display has been imported. My version makes the function being called explicit by importing IPython and then specifying the fully qualified name. – Conor McM Apr 05 '22 at 22:09
  • I took the liberty to edit more than usual, in order to help you avoid the impression of misusing an answer for a comment. Note that you should not do that and I normally flag as not-an-answer. Your post however seems an appropriate variant/extension/improvement to an existing answer, which is an acceptable answer post in my opinion. Please review to make sure I did not bend your intention. But please do not use an answer if you are convinced that what you are writing is a comment. Have fun. – Yunnosch Jul 12 '22 at 11:50
  • Please also consider to [edit] and move what you wrote in your comment into the answer itself. It seems an explanation which will improve the answer if contained there. – Yunnosch Jul 12 '22 at 11:52