Issue
I have a colab notebook with code that opens multiple new tabs in the browser:
from IPython.display import Javascript
def open_tab(url):
display(Javascript('window.open("{url}");'.format(url=url)))
Everything works fine when I use it with dataframe["urls"].apply(open_tab). However whenever I refresh/reopen the tab with colab notebook again, browser immediately reopens all the tabs that were opened in the last run.
That happens despite notebook being disconnected and runtime deleted previously, so no code is executed. Works the same on Chrome and Edge browsers. I suspect it might be something with browser caching and linking generated tabs with the tab of origin (colab notebook), but have no idea how to verify that :) I expected new tabs being opened only when the cell is executed.
Solution
The display()
function creates an output object in the notebook of type display_data
. For example consider the following code:
import pandas as pd
from IPython.display import Javascript
def open_tab(url):
display(Javascript('window.open("{url}");'.format(url=url)))
df = pd.DataFrame(["https://stackoverflow.com", "https://google.com", "https://docs.python.org"], columns=["urls"])
df["urls"].apply(open_tab)
When you run this once and then download the notebook as .ipynb
file you will see the following in the notebook source:
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 86
},
"id": "dKjZ-iZWw0Lc",
"outputId": "30859861-e042-4912-9d08-f10740731ece"
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"window.open(\"https://stackoverflow.com\");"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"window.open(\"https://google.com\");"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"window.open(\"https://docs.python.org\");"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0 None\n",
"1 None\n",
"2 None\n",
"Name: urls, dtype: object"
]
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"import pandas as pd\n",
"from IPython.display import Javascript\n",
"def open_tab(url):\n",
" display(Javascript('window.open(\"{url}\");'.format(url=url)))\n",
"\n",
"df = pd.DataFrame([\"https://stackoverflow.com\", \"https://google.com\", \"https://docs.python.org\"], columns=[\"urls\"])\n",
"df[\"urls\"].apply(open_tab)"
]
}
]
The output that you see at the bottom of the cell is there ("output_type": "execute_result"
), and the source code, but also three "output_type": "display_data"
objects, one for each url.
This type of output is also used for images, graphs, and rendered HTML, which you would normally most likely want to see when you reopen the notebook, even if you don't want to run the code again. Similar to those outputs, also the generated JavaScript is "shown again", i.e., executed.
To prevent this you can right-click on the cell that generates the JavaScript and choose "remove output", which will delete the output objects (while keeping the output of all other cells).
Answered By - Marijn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.