'How to automate running of Jupyter Notebook cells periodically
I want to integrate my jupyter notebook with my website, where I have written the code to fetch real-time data from MySQL
server and do real-time visualisation using plotly
. But every time I'm having to run all the cells of my Kernel. Is there a way I can automate the running of the Jupyter notebook cells periodically say everyday 1 hour?
Solution 1:[1]
My suggestion would be to
- Setup a cron job with the periodicity you want.
- Use runipy to run all the cells in the notebook. It has a lot of functionality like saving the run as html report. This will particularly be useful in your case as you want to visualise plotly plots.
I can provide the commands here, but they are pretty straight forward and can easily be followed from the links.
Solution 2:[2]
Please setup a function that runs for an interval of 1 hour using the Javascript setInterval
function. Inside the interval function you can call the jupyter
object method, to run all cells.
%%html
<script>
// AUTORUN ALL CELLS ON NOTEBOOK-LOAD!
require(
['base/js/namespace', 'jquery'],
function(jupyter, $) {
$(jupyter.events).on("kernel_ready.Kernel", function () {
setInterval(function(){
console.log("Auto-running all cells-below...");
jupyter.actions.call('jupyter-notebook:run-all-cells-below');
// jupyter.actions.call('jupyter-notebook:save-notebook');
}, 60000); // 60000 for 1 hour interval gap
});
}
);
</script>
Note: I have added the setInterval
part of the script by myself, the major code,comes from this SO answer
Solution 3:[3]
import time
# Wait for 3600 seconds
while True:
time.sleep(3600)
#YOUR CODE HERE
If you want not to wait for the cell in running mode you can run the code above inside a thread
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Deepak Saini |
Solution 2 | Naren Murali |
Solution 3 | Hami |