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,183
  • 1
  • 13
  • 39

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,810
  • 1
  • 19
  • 26
  • So I can directly link it to my website? And like it will run throughout the day on its own,whatever I've coded in it? – Debadri Dutta Aug 05 '18 at 17:40
  • The runipy site notes that it "started before Jupyter's [execute API](https://nbconvert.readthedocs.io/en/latest/execute_api.html), which is now the recommended way to run notebooks from the command-line." – fact_finder Oct 12 '22 at 12:33
  • As @fact_finder notes, runipy is now deprecated, and points uses to the execute API of nbconvert. – jeffmcc Jan 10 '23 at 19:03
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
  • 704
  • 2
  • 9
  • 27
Afshin Amiri
  • 3,438
  • 1
  • 20
  • 21
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
  • 19,250
  • 3
  • 27
  • 54