Defer data load

Download this notebook from GitHub (right-click to download).

In [ ]:
import panel as pn
import pandas as pd
import hvplot.pandas

Defer loading of the data and populating the widgets until the page is rendered:

In [ ]:
stocks_url = ''

select_ticker = pn.widgets.Select(name='Stock Ticker')

def load_data():
    if 'stocks' not in pn.state.cache: 
        pn.state.cache['stocks'] = df = pd.read_csv(stocks_url, parse_dates=['date']).set_index('symbol')
        df = pn.state.cache['stocks']
    symbols = list(df.index.unique())
    select_ticker.options = symbols
    select_ticker.value = symbols[0]

If 'stocks' is not yet in cache we show a spinner widget, otherwise let us plot the data:

In [ ]:
def plot_ticker(ticker):
    if 'stocks' not in pn.state.cache or not ticker:
        return pn.indicators.LoadingSpinner(value=True)
    return pn.state.cache['stocks'].loc[ticker].hvplot.line('date', 'price')

pn.Row(select_ticker, plot_ticker).servable()

Download this notebook from GitHub (right-click to download).