Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 886
Mike Avatar asked Dec 24 '15 01:12

Mike


People also ask

How do I open a new tab in a Jupyter notebook?

3.1.Double-click on the Jupyter Notebook desktop launcher (icon shows [IPy]) to start the Jupyter Notebook App. The notebook interface will appear in a new browser window or tab. A secondary terminal window (used only for error logging and for shut down) will be also opened.


2 Answers

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)
like image 186
Mike Müller Avatar answered Sep 23 '22 08:09

Mike Müller


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)))
like image 24
Michael Noguera Avatar answered Sep 21 '22 08:09

Michael Noguera