Plotly pane not working with (async) generator
MarcSkovMadsen opened this issue · comments
Marc Skov Madsen commented
I'm on the current main
branch working on improving the Plotly reference guide.
I would expect the Plotly pane as any other pane to support (async) generator functions. But it does not.
import pandas as pd
import plotly.graph_objects as go
from asyncio import sleep
import panel as pn
pn.extension("plotly")
df = pn.cache(pd.read_csv)(
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv"
)
start_index = 50
data = go.Ohlc(
x=df.loc[:start_index, "Date"],
open=df.loc[:start_index, "AAPL.Open"],
high=df.loc[:start_index, "AAPL.High"],
low=df.loc[:start_index, "AAPL.Low"],
close=df.loc[:start_index, "AAPL.Close"],
)
async def stream():
for _ in range(0, 50):
index = len(data.x)
if index == len(df):
index = 0
data["x"] = df.loc[:index, "Date"]
data["open"] = df.loc[:index, "AAPL.Open"]
data["high"] = df.loc[:index, "AAPL.High"]
data["low"] = df.loc[:index, "AAPL.Low"]
data["close"] = df.loc[:index, "AAPL.Close"]
await sleep(100)
yield {"data": data, "layout": go.Layout(xaxis_rangeslider_visible=False)}
pn.pane.Plotly(stream).servable()
Its just empty
Workaround: periodic_callback
This works
import pandas as pd
import plotly.graph_objects as go
import panel as pn
pn.extension("plotly")
df = pn.cache(pd.read_csv)(
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv"
)
start_index = 50
data = go.Ohlc(
x=df.loc[:start_index, "Date"],
open=df.loc[:start_index, "AAPL.Open"],
high=df.loc[:start_index, "AAPL.High"],
low=df.loc[:start_index, "AAPL.Low"],
close=df.loc[:start_index, "AAPL.Close"],
)
fig = {"data": data, "layout": go.Layout(xaxis_rangeslider_visible=False)}
plotly_pane = pn.pane.Plotly(fig)
def stream():
index = len(data.x)
if index == len(df):
index = 0
data["x"] = df.loc[:index, "Date"]
data["open"] = df.loc[:index, "AAPL.Open"]
data["high"] = df.loc[:index, "AAPL.High"]
data["low"] = df.loc[:index, "AAPL.Low"]
data["close"] = df.loc[:index, "AAPL.Close"]
plotly_pane.object = fig
pn.state.add_periodic_callback(stream, period=100, count=50)
plotly_pane.servable()
plotly_streaming.mp4
Philipp Rudiger commented
Works just fine if you don't sleep for 100 seconds 😄
Marc Skov Madsen commented