How to automate running of Jupyter Notebook cells periodically

廉价感情. 提交于 2020-07-09 16:46:13

问题


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?


回答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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/51692672/how-to-automate-running-of-jupyter-notebook-cells-periodically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!