21

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,497
  • 3
  • 26
  • 40

3 Answers3

28

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
  • 77,540
  • 18
  • 155
  • 159
  • 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
  • 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
  • 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
5

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
  • 391
  • 3
  • 13
1

@Cybernetic I don't have the reputation to comment, and I feel like it would be impertinent to edit Michael's answer...

Code below should fix the module not callable error:

import IPython

def window_open(url):
    IPython.display.display(IPython.display.Javascript('window.open("{url}");'.format(url=url)))
Conor McM
  • 11
  • 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