1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] holoviews/panel plot not updated when executed in Jupyterlab

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024 às 10:02.

  1. Stack

    Stack Membro Participativo

    In a jupyterlab cell, I am trying to update a plot figure from an external data source which does not know about the figure. But I cannot manage to have the figure updated during the cell execution as the data source is updated. Instead, my figure is updated only once at the end.

    Here is the test code sample:

    import matplotlib as mpl
    import panel as pn
    from IPython import display
    import holoviews as hv
    import time
    pn.extension()

    val = 0
    stream = hv.streams.Stream.define('I', index = 0)()

    # This is the callback that is called whenever I push data in my `stream`
    # This writes "HELLO {i}" in a matplotlib figure where `i` is the data updated
    def _cb(index):
    mpl.pyplot.ioff()
    fig = mpl.figure.Figure(figsize=(1, 1))

    string = f"HELLO {index}"
    print(string)
    fig.suptitle(string)

    mpl.pyplot.ion()
    panel = pn.panel(fig)
    return panel

    # Create the panel widget
    col = pn.Column(pn.bind(_cb, index=stream.param.index))
    display.display(col)

    # Regularly publish data in my stream
    for i in range(10):
    stream.event(index=i)
    time.sleep(1)
    print("end of loop")

    What I expect

    1. When creating the figure and calling display.display, I shall see the figure created.
    2. When calling stream.event in my for loop, I should see the above figure updated every 1s, at the same time I should see "HELLO {i}" in the logs
    What I observe

    1. My figure is correctly created and displayed
    2. The logs appear below the cell, but the figure is not updated (keeps on "HELLO 0")
    3. When the cell finishes, I see the log "end of loop" and the figure is updated with "HELLO 9"
    Note 1


    When running only the for loop in a second cell, the figure of the first cell is correctly updated every second. This does not match my final use case as all the code should be executed in one cell, but I'd like to understand what happens.

    Note 2


    When running this cell in vscode with Jupyterlab extension, vscode first complains about not having jupyter_bokeh and after installation and kernel restarts, it behaves as expected (regular updates of the figure). Running in jupyterlab with a kernel containing jupyter_bokeh does not work though. Do you now if jupyter_bokeh might be related to this issue?

    My configuration


    holoviews 1.18.3
    ipython 8.18.1
    ipython-genutils 0.2.0
    ipywidgets 8.0.6
    jupyter_bokeh 4.0.5
    jupyter_client 8.6.0
    jupyter_core 5.7.1
    jupyter-events 0.9.0
    jupyter-lsp 2.2.4
    jupyter_server 2.13.0
    jupyter_server_terminals 0.5.2
    jupyterlab 4.1.3
    jupyterlab_pygments 0.3.0
    jupyterlab_server 2.25.3
    jupyterlab_widgets 3.0.10
    jupytext 1.16.2
    matplotlib 3.7.5
    matplotlib-inline 0.1.6
    panel 1.3.8


    Thanks for your help!

    Continue reading...

Compartilhe esta Página