Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

%blockrun button executes even after clicking the button on google colab #8

Open
DheerajKN opened this issue Mar 23, 2021 · 8 comments

Comments

@DheerajKN
Copy link

DheerajKN commented Mar 23, 2021

Error reproducible at
cell 1
try:
import ipython_blocking
except ImportError:
!pip install ipython_blocking
import ipython_blocking

cell 2
from ipywidgets import Dropdown, Button, Output
from IPython.display import display, clear_output

outs = Output()
query_selector = Dropdown(
options=['Select', 'Q1', 'Q2'],
value='Select',
description='Query Type:'
)

process_button = Button(
description='Process',
disabled=False
)

def proc_button(bb):
with outs:
clear_output()
display(process_button)

display(query_selector)
query_selector.observe(proc_button, names='value')
display(outs)

cell 3
%blockrun process_button

cell 4
print(query_selector.value)

causing the cell 3 to run continuously even after the button is selected.

@kafonek
Copy link
Owner

kafonek commented Mar 23, 2021

@DheerajKN thanks for opening this issue. I'm able to reproduce the issue, and I see this in the javascript console -

Error evaluating Javascript output:  ReferenceError: Jupyter is not defined
    at eval (eval at <anonymous> (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:1), <anonymous>:1:1)
    at eval (<anonymous>)
    at qa.eval [as h] (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:99)
    at sa (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:15)
    at ua.next (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:15)
    at eval (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:16)
    at new Promise (<anonymous>)
    at wa (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:16)
    at z (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:16)
    at ie (output_binary.js?vrz=colab-20210322-060003-RC00_364283823:97)

I'm not too familiar with what front-end javascript changes Colab makes over generic classic Notebook. Classic Notebook has a Javascript object that can control the Notebook DOM. In ipython_blocking, %blockrun calls this function to execute everything below,

def run_all_below():
    js = 'Jupyter.notebook.select_next().execute_cells_below()'
    display(Javascript(js))

If you know the equivalent Colab front-end API to use, I'm happy to look at this more deeply or review a PR. Thanks.

@DheerajKN
Copy link
Author

I am not much well-versed with using magic functions. Can you provide me the code run magic function from cloned GitHub project?

@DheerajKN
Copy link
Author

Can you try to replace Jupyter with IPython as maybe colab doesn't support Jupyter and instead uses IPython
def run_all_below():
js = 'IPython.notebook.select_next().execute_cells_below()'
display(Javascript(js))

@kafonek
Copy link
Owner

kafonek commented Mar 24, 2021

@DheerajKN you can test that idea out by going to colab, opening up your browser developer tools, and trying to execute IPython.notebook.select_next().execute_cells_below() in a console window. Unfortunately IPython is not defined, just like Jupyter.

There are two things to research here:

  • What is the equivalent to Jupyter.notebook.select_next().execute_cells_below() in Google Colab

  • How could ipython_blocking identify that it is in a Google Colab environment and should use the alternate syntax

I see that there is a google-colaboratory tag on stackoverflow that might be a good way to seek help on those questions. Unfortunately it doesn't appear too active, for instance https://stackoverflow.com/questions/65984431/run-all-cells-command-in-google-colab-programmatically is very close to what we're asking but there's no answer.

@DheerajKN
Copy link
Author

DheerajKN commented Mar 24, 2021

  1. For the first I couldn't find any so dropped a StackOverflow Request: https://stackoverflow.com/questions/66786245/what-is-the-equivalent-to-jupyter-notebook-select-next-execute-cells-below-i
  2. This did the trick:
if 'google.colab' in str(get_ipython()):
  print('Running on CoLab')
else:
  print('Not running on CoLab')

@saraswathykrk
Copy link

Error reproducible at
cell 1
try:
import ipython_blocking
except ImportError:
!pip install ipython_blocking
import ipython_blocking

cell 2
from ipywidgets import Dropdown, Button, Output
from IPython.display import display, clear_output

outs = Output()
query_selector = Dropdown(
options=['Select', 'Q1', 'Q2'],
value='Select',
description='Query Type:'
)

process_button = Button(
description='Process',
disabled=False
)

def proc_button(bb):
with outs:
clear_output()
display(process_button)

display(query_selector)
query_selector.observe(proc_button, names='value')
display(outs)

cell 3
%blockrun process_button

cell 4
print(query_selector.value)

causing the cell 3 to run continuously even after the button is selected.

Hi @DheerajKN,

Can you please let me know how you finally resolved this in colab?

Regards
Saraswathy.

@DheerajKN
Copy link
Author

@saraswathykrk I couldn't resolve this issue. As we were not able to find an equivalent function for execute_cells_below that is supported in Google Colab. StackOverflow for the same was provided.
It was part of my assignment, so just submitted it in a local Jupyter notebook. Locally it works!!

@saraswathykrk
Copy link

Thanks for the reply @DheerajKN.

Did you also try the below code given for Pause Colab Outputs? Just thinking if we could leverage this and achieve the blocking.

display(IPython.display.Javascript('''
const promise = new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = 'data:,window.value = "hello world!"';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
// Pause subsequent outputs until the script has been loaded.
google.colab.output.pauseOutputUntil(promise);
'''))

display(IPython.display.Javascript('''
// Can immediately reference scripts loaded earlier since
// output processing was blocked on them.
document.body.appendChild(document.createTextNode(window.value));
'''))

Regards
Saraswathy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants