holoviz / panel

Panel: The powerful data exploration & web app framework for Python
https://panel.holoviz.org
BSD 3-Clause "New" or "Revised" License
4.79k stars 518 forks source link

Please don't load unused extension on the client side #4282

Closed MarcSkovMadsen closed 1 year ago

MarcSkovMadsen commented 1 year ago

If I have a multiple page the extensions like Plotly used on one page is also loaded on the other pages. This slows down the launch of the other pages, it does not add positively to the user experience and it means I have to spend more money on servers.

Please make it possible not to load unused extensions.

I would like to document the solution in #4281

Example

panel serve home.py dashboard.py --index home --autoreload

Verify that plotly-2.10.1-min.js is loaded even though its not needed on the home page.

home-developer-tools

# home.py
import panel as pn

pn.extension(sizing_mode="stretch_width")

SECTION = """
Panel works really well with the visualization tools you already know and love like [Altair/ Vega](https://panel.holoviz.org/reference/panes/Vega.html), [Bokeh](https://panel.holoviz.org/reference/panes/Bokeh.html), [Datashader](https://datashader.org/), [Deck.gl/ pydeck](https://panel.holoviz.org/reference/panes/DeckGL.html), [Echarts/ pyecharts](https://panel.holoviz.org/reference/panes/ECharts.html), [Folium](https://panel.holoviz.org/reference/panes/Folium.html), [HoloViews](https://holoviews.org/), [hvPlot](https://hvplot.holoviz.org), [plotnine](https://panel.holoviz.org/reference/panes/Matplotlib.html), [Matplotlib](https://panel.holoviz.org/reference/panes/Matplotlib.html), [Plotly](https://panel.holoviz.org/reference/panes/Plotly.html), [PyVista/ VTK](https://panel.holoviz.org/reference/panes/VTK.html), [Seaborn](https://panel.holoviz.org/gallery/styles/SeabornStyle.html) and more. Panel also works with the [ipywidgets](https://panel.holoviz.org/reference/panes/IPyWidget.html) ecosystem.
"""

IMAGE = pn.pane.GIF("https://user-images.githubusercontent.com/42288570/211983400-3315ad0a-866a-4916-8809-6fc38eca34d9.gif", alt_text="Pythons DataViz works with Panel", link_url="https://panel.holoviz.org/reference/index.html#panes", height=400)

component = pn.Column(
    SECTION, IMAGE
)

NAVIGATION = """
## Navigation

[home](home)
[dashboard](dashboard)
"""

pn.template.FastListTemplate(
    site="Panel", site_url="https:\\panel.holoviz.org",
    title="Home", main=[component], sidebar=[NAVIGATION],
    main_max_width="900px",
).servable()
import panel as pn
import pandas as pd
import plotly.graph_objects as go

from bokeh.sampledata import stocks

pn.extension('plotly')

title = '## Stock Explorer Plotly'

tickers = ['AAPL', 'FB', 'GOOG', 'IBM', 'MSFT']

SOURCES = {
    "AAPL": "http://sampledata.bokeh.org/AAPL.csv",
    "FB": "http://sampledata.bokeh.org/FB.csv",
    "GOOG": "http://sampledata.bokeh.org/GOOG.csv",
    "IBM": "http://sampledata.bokeh.org/IBM.csv",
    "MSFT": "http://sampledata.bokeh.org/MSFT.csv",
}

def get_df(ticker, window_size):
    url = SOURCES[ticker]
    df = pd.read_csv(url)
    df = df.rename(columns={"Date": "date", "Close": "close"})
    df['date'] = pd.to_datetime(df.date)
    return df.set_index('date').rolling(window=window_size).mean().reset_index()

ticker = pn.widgets.Select(name='Ticker', options=tickers)
window = pn.widgets.IntSlider(name='Window Size', value=6, start=1, end=21)

def get_plot(ticker, window_size):
    df = get_df(ticker, window_size)
    return go.Scatter(x=df.date, y=df.close)

component = pn.Column(
    pn.Column(title, ticker, window),
    pn.bind(get_plot, ticker, window),
    sizing_mode='stretch_width'
)

NAVIGATION = """
## Navigation

[home](/)
[dashboard](dashboard)
"""

pn.template.FastListTemplate(
    site="Panel", site_url="https:\\panel.holoviz.org",
    title="Dashboard", main=[component], sidebar=[NAVIGATION],
    main_max_width="900px",
).servable()
philippjfr commented 1 year ago

Duplicate of https://github.com/holoviz/panel/issues/2573