Iteratively update FigureWidget in JupyterLab cell

Hello all.

I am trying to iteratively update a FigureWidget inside a JupyterLab cell and I cannot make it work.

The problem seems to be that a cell needs to finish running, for the FigureWidget to be displayed.

import time
from plotly import tools
import plotly.graph_objs as go

subplot = tools.make_subplots(2, 1)
f2 = go.FigureWidget(subplot)
i = 1
traceA = go.Scatter(
        y = list(range(1,i*20)),
        mode='lines')
f2.add_trace(traceA,row=i,col=1)
f2
time.sleep(5)
i = 2
traceA = go.Scatter(
        y = list(range(1,i*20)),
        mode='lines')
f2.add_trace(traceA,row=i,col=1)
f2

In this simple example, there is no figure displayed before the sleep.

Hi @Takeo,

You can either break this up into multiple cells and run the whole notebook

import time
from plotly import tools
import plotly.graph_objs as go

subplot = tools.make_subplots(2, 1)
f2 = go.FigureWidget(subplot)
i = 1
traceA = go.Scatter(
        y = list(range(1,i*20)),
        mode='lines')
f2.add_trace(traceA,row=i,col=1)
f2
time.sleep(5)
i = 2
traceA = go.Scatter(
        y = list(range(1,i*20)),
        mode='lines')
f2.add_trace(traceA,row=i,col=1)

Or you can use IPython.display.dispaly to display the figure in the middle of the cell

import time
from plotly import tools
import plotly.graph_objs as go
from IPython.display import display

subplot = tools.make_subplots(2, 1, print_grid=False)
f2 = go.FigureWidget(subplot)
display(f2)

time.sleep(2)
i = 1
traceA = go.Scatter(
        y = list(range(1,i*20)),
        mode='lines')
scatter = f2.add_trace(traceA,row=i,col=1)

time.sleep(2)
i = 2
traceA = go.Scatter(
        y = list(range(1,i*20)),
        mode='lines')
scatter1 = f2.add_trace(traceA,row=i,col=1)

Hope that helps!
-Jon