'Plotly Candlestick updated and shown dynamically in a loop

I would like to dynamically update a Candlestick chart from plotly:

import time
import plotly.graph_objects as go

while True:
  candle_df = candle_handler.get_dataframe()
  candlestick = go.Candlestick(x=candle_df['Time'], open=candle_df['Open'], high=candle_df['High'], low=candle_df['Low'], close=candle_df['Close'])  
  fig = go.Figure(data=[candlestick])
  fig.show()
  time.sleep(3)

where candle_handler.get_dataframe() pull the data from an API and updates the data in the candle_df pandas dataframe.

However, the chart is shown only very briefly at every iteration of the loop (much less than for 3 seconds).

I have found a snippet that works for a scatter plot:

import time
import plotly.graph_objects as go

data = [1,3,2,4,3,3,2,3]

fig = go.FigureWidget()
fig.add_scatter()

for i in range(len(data)):
  time.sleep(3)
  fig.data[0].y = data[:i]
  fig.show()

and I would like to do something similar for the Candlestick chart.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source