ipython_blocking
ipython_blocking copied to clipboard
%blockrun button executes even after clicking the button on google colab
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.
@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.
I am not much well-versed with using magic functions. Can you provide me the code run magic function from cloned GitHub project?
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))
@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 -
[x] 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.
- 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
- This did the trick:
if 'google.colab' in str(get_ipython()):
print('Running on CoLab')
else:
print('Not running on CoLab')
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.
@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!!
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.