Openning website with Webbrowser - Google Colab [duplicate]

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

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.