Realtime and Live charts on Google Colab(Jupyter Notebooks)

Embedding live charts in a website is a trivial task with a websockets or a polling backend. Having a live chart in a standalone python application is also trivial. However, Jupyter notebooks work differently. They execute python code in notebook servers and brings the results back to the browser.




One possible solution for achieving this has been mentioned in below stackoverflow thread.

However, when this code is executed, it has to generate every frame in the server-side and download the image to update the notebook's output cell. Apart from that, we have to call, 


in-order to avoid notebook from drawing each frame as a new image, resulting in thousands of images repeated in the output cell. This could even crash the browser at some point.

The alternative is using Javascript!

IPython.display package comes with two functions HTML and JavaScript that can be used to embed and call html/javascript in the output cell. Hence we can leverage this feature to embed a chartjs or any other JavaScript based chart in the output cell and dynamically update the chart using chartjs JavaScript APIs. The source code can be simple as follows.

The mock data source is still generating data at the server-side, but unlike the previous case, it's now sending just js function that has to be called along with the function arguments. Which is a very lightweight payload compared to the pee-rendered image data sent in the previous approach. Additionally, we no longer have to repeatedly call clear_output, which will directly clear the HTML part of the output cell and re-render new image. Now with the chartjs library we are directly redrawing on the canvas, which is super efficient compared to previous approach.

Given below is a link to the Google Colab notebook that you can try out immediately.