'Open URL in new tab from IPython Notebook/Jupyter cell

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.



Solution 1:[1]

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)

Solution 2:[2]

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)))

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike Müller
Solution 2
Solution 3 Conor McM