holoviz / panel

Panel: The powerful data exploration & web app framework for Python

Home Page:https://panel.holoviz.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Plotly pane not working with (async) generator

MarcSkovMadsen opened this issue · comments

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

image

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

Works just fine if you don't sleep for 100 seconds 😄

Thx. In danish its an "Error 40" (fejl 40). The problem is 40cm from the screen 😄

image