Create a Jupyter Notebook object that changes after the cell starts.

I want to create a progress bar that updates asynchronously in a Jupyter laptop (with a kernel ipython)

Example

In [1]: ProgressBar(...)
Out[1]: [|||||||||------------------]  35%  # this keeps moving

In [2]: # even while I do other stuff

I plan to deploy a background thread to check and update the move. I am not sure how to update the output (or even if it is possible.)

+4
source share
1 answer

It may help you on the right track, code taken from lightning that borrowed a lot from matplotlib from it. warning this is all pretty underestimated.

In python you need to instantiate a comm object

from IPython.kernel.comm import Comm
comm = Comm('comm-target-name', {'id': self.id})

https://github.com/lightning-viz/lightning-python/blob/master/lightning/visualization.py#L15-L19. , , .

javascript:

var IPython = window.IPython;

IPython.notebook.kernel.comm_manager.register_target('comm-target-name', function(comm, data) {
      // the data here contains the id you set above, useful for managing
      // state w/ multiple comm objects

      // register the event handler here
      comm.on_msg(function(msg) {
      })
});

. , javascript on_msg , comm, js โ†’ python. , , . https://github.com/lightning-viz/lightning-python/blob/master/lightning/visualization.py#L90

, python:

comm.send(data=data)

https://ipython.org/ipython-doc/3/api/generated/IPython.kernel.comm.comm.html#IPython.kernel.comm.comm.Comm.send

+3

Source: https://habr.com/ru/post/1618218/


All Articles