Is there any way to open a new tab using a webbrowser module of python in colab?
Asked Answered
L

2

5
import webbrowser
webbrowser.open_new_tab('www.google.com')

I am using Google-colaboratory and getting False as a output. Instead of that, it should open a new tab with url of Google. It's working well in python 3.7 as well as in PyCharm.

I know that program written in colab runs on google cloud and there's nothing like browser in google cloud.

Is there any other way to do this in google-colab except by using selenium tools?

Thanks in Advance!

Lymphadenitis answered 21/4, 2020 at 1:57 Comment(0)
S
8

No, there is no way to control your browser via Python's webbrowser package in Colab. The reason is that your browser is running on your local machine, while the Python code in Colab is running on a virtual machine in Google Cloud.

The only way you can open a new tab via Colab is by injecting javascript code that will cause your browser to do so; for example:

%%javascript
window.open('https://colab.research.google.com', '_blank');

But be aware that your browser may block windows opened in this manner.

Selassie answered 21/4, 2020 at 5:30 Comment(0)
V
12

Sometimes if the lines of code required to open a webpage need to be enclosed in conditional statements or a loop or any situation in which %%javascript cannot be written as first line of the cell, use this code

from IPython.display import Javascript
def open_web():
    url = 'https://www.google.com/'
    display(Javascript('window.open("{url}");'.format(url=url)))
Vander answered 18/11, 2020 at 12:48 Comment(2)
Precisely my usecase.Superior
The same note still apply: "But be aware that your browser may block windows opened in this manner." Navigate on the address bar, unblock the blocked site.Hypothyroidism
S
8

No, there is no way to control your browser via Python's webbrowser package in Colab. The reason is that your browser is running on your local machine, while the Python code in Colab is running on a virtual machine in Google Cloud.

The only way you can open a new tab via Colab is by injecting javascript code that will cause your browser to do so; for example:

%%javascript
window.open('https://colab.research.google.com', '_blank');

But be aware that your browser may block windows opened in this manner.

Selassie answered 21/4, 2020 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.