简体   繁体   English

如何定期自动运行Jupyter Notebook单元

[英]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 . 我想将jupyter笔记本与我的网站集成在一起,在那儿我编写了从MySQL服务器获取实时数据并使用plotly进行实时可视化的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? 有什么方法可以使Jupyter笔记本电脑的电池定期自动运行,例如每天1小时?

My suggestion would be to 我的建议是

  • Setup a cron job with the periodicity you want. 以所需的周期设置 cron作业。
  • Use runipy to run all the cells in the notebook. 使用runipy运行笔记本中的所有单元。 It has a lot of functionality like saving the run as html report. 它具有很多功能,例如将运行另存为html报告。 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. 我可以在此处提供命令,但是它们非常简单,可以从链接中轻松地遵循。

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 如果不想在运行模式下等待单元格,则可以在线程内运行上面的代码

Please setup a function that runs for an interval of 1 hour using the Javascript setInterval function. 请使用Javascript setInterval函数设置一个运行时间为1小时的函数。 Inside the interval function you can call the jupyter object method, to run all cells. 在interval函数内部,您可以调用jupyter对象方法来运行所有单元格。

%%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 注意:我自己添加了脚本的setInterval部分,主要代码来自此SO answer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM