26

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.

3 Answers 3

31

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)
Sign up to request clarification or add additional context in comments.

11 Comments

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
Did you try? It works. Just copy-paste the code in your Notebook and see what happens.
actually I did... It did not work... Maybe I am doing something wrong, but I get a FALSE boolean as return
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.
OK. My answer is for the "normal" usage. Run the Notebook server locally. No remote server, no Docker.
|
7

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

2 Comments

I get TypeError: 'module' object is not callable
Hmm... it's been a while since I've looked at this, but I remember finding this question and its responses helpful.
2

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

4 Comments

Please, share why it works
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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.