Defer data load

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

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:

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:

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()
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

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