问题
I am working on google colab as it a part of my school project.
But I got stuck on a problem. So, I wanted some one to help me out.
I want that every time the page is refreshed, a new random number generated using python gets shown up in the output area (in the image it has number 1).
I am able to run a javascript code as soon as the page loads. But I don't know how to simulate a click on the run icon. So, if someone knows how to simulate click on that run button (it's actually not a button, it's a <div>
). Please, if anyone could help me out, I will be very thankful.
Sorry for poor english.
Code:
from random import randint
import IPython
print(randint(1,5))
js_code = '''
document.addEventListener("load", a());
function a() {
var s = /* The run button */
s.cick();
}
'''
display(IPython.display.Javascript(js_code));
回答1:
By design, Colab requires user interaction to execute code. There's no way to automatically execute a cell when a notebook loads.
来源:https://stackoverflow.com/questions/55918385/refresh-results-in-google-colaboratory-as-the-page-loads