1

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?

Debadri Dutta
  • 1,099
  • 1
  • 10
  • 38

3 Answers3

2

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.

Deepak Saini
  • 2,710
  • 1
  • 16
  • 24
0
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

Hami
  • 666
  • 2
  • 9
  • 26
Afshin Amiri
  • 3,128
  • 1
  • 18
  • 20
0

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

Naren Murali
  • 13,809
  • 3
  • 22
  • 48