Bokeh chart blinking/flickering with st.empty (streaming data)

For testing purpose, I have implemented a simple Bokeh chart with a deque() input and randomly generated streaming data. When running, the chart in streamlit keeps on blinking/flickering.

Instead, the same simple chart with Plotly.express does not have this problem.

Is there a way to fix this? Thanks.

Bokeh code:

import streamlit as st
from random import randint
from time import sleep
from collections import deque
from bokeh.plotting import figure

placeholder = st.empty()
y = deque()
x = deque()
count = 0

def data_streaming(price, time, index):
while len(price) <= 1000:
rnd = randint(50, 200)
index += 1
if len(y) > 10:
y.popleft()
x.popleft()
price.append(rnd)
time.append(index)
sleep(0.2)
print(β€˜y:’, price[-1])
print(β€˜x:’, time[-1])
with placeholder.container():
chart = figure(
title=β€˜Price Trend Analysis By Bookeh’,
x_axis_label=β€˜x’,
y_axis_label=β€˜y’)
chart.line(x, y, legend_label=β€˜Trend’, line_width=2)
st.write(chart)
sleep(0.1)

data_streaming(y, x, count)

Plotly.express code:

import streamlit as st
from random import randint
from time import sleep
from collections import deque
import plotly.express as px

placeholder = st.empty()
y = deque()
x = deque()
count = 0

def data_streaming(price, time, index):
while len(price) <= 1000:
rnd = randint(50, 200)
index += 1
if len(y) > 10:
y.popleft()
x.popleft()
price.append(rnd)
time.append(index)
sleep(0.2)
print(β€˜y:’, price[-1])
print(β€˜x:’, time[-1])
with placeholder.container():
fig = px.line(x=x, y=y)
st.write(fig)
sleep(0.1)
index += 1

data_streaming(y, x, count)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.