Editor + Server Development and Other Environment Development in Panel lacks documentation

In the getting started guide we read:

You can edit your Panel code as a .py file in any text editor, marking the objects you want to render as .servable() ,

How do you mark an object as .servable()? Where is an example of this?

then launch a server with panel serve my_script.py --show

This does not work after pip install panel … what does work is python -m panel serve but it doesnt show anything. I’m trying to get the function example2() in the code below to work interactively and I have no interest/intention in Jupyter notebooks or VS code. The docs state that Editor + Server and Other Environments work, so i would appreciate some help in getting it to work. Again, support is requested in getting example2() to run successfully:

from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvas
import numpy as np
import panel as pn

import load_data

### BROKEN FUNCTION: DOESNT WORK FROM COMMAND-LINE USAGE
def mpl_plot(avg, highlight):
    fig = Figure()
    FigureCanvas(fig)  # not needed in mpl >= 3.1
    ax = fig.add_subplot()
    avg.plot(ax=ax)
    if len(highlight): highlight.plot(style='o', ax=ax)
    return fig


def my_mpl_plot(avg, highlight):
    fig, ax = plt.subplots()
    avg.plot(ax=ax)
    if len(highlight): highlight.plot(style='o', ax=ax)
    return fig


def find_outliers(data, variable='Temperature', window=30, sigma=10, view_fn=mpl_plot):
    avg = data[variable].rolling(window=window).mean()
    residual = data[variable] - avg
    std = residual.rolling(window=window).std()
    outliers = (np.abs(residual) > std * sigma)
    return view_fn(avg, avg[outliers])


def example1():
    f = find_outliers(load_data.data, variable='Temperature', window=20, sigma=10, view_fn=my_mpl_plot)
    plt.show()
    input()


def example2():
    def my_call():
        return find_outliers(load_data.data, variable='Temperature', window=20, sigma=10, view_fn=my_mpl_plot)

    pn.extension()
    pn.interact(my_call)


if __name__ == '__main__':
    example2()
1 Like

Panel objects render themselves in a notebook. When working outside of the notebook you need to call their servable method to indicate Panel that you want one (or more actually) object(s) to be served.

That’s the example from the Getting started that you can deploy with panel serve getting_started.py --show:

#getting_started.py
import pandas as pd; import numpy as np; import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvas
import panel as pn

data = pd.read_csv('https://raw.githubusercontent.com/holoviz/panel/master/examples/assets/occupancy.csv')
data['date'] = data.date.astype('datetime64[ns]')
data = data.set_index('date')

def mpl_plot(avg, highlight):
    fig = Figure()
    FigureCanvas(fig) # not needed in mpl >= 3.1
    ax = fig.add_subplot()
    avg.plot(ax=ax)
    if len(highlight): highlight.plot(style='o', ax=ax)
    return fig

def find_outliers(variable='Temperature', window=30, sigma=10, view_fn=mpl_plot):
    avg = data[variable].rolling(window=window).mean()
    residual = data[variable] - avg
    std = residual.rolling(window=window).std()
    outliers = (np.abs(residual) > std * sigma)
    return view_fn(avg, avg[outliers])

pn.interact(find_outliers).servable()

There’s no reason why panel serve would not know after installing it with pip (I’m doing that quite frequently with no problem).

I just installed hvplot and noticed some of the install dialog that may shed light on the issue. I’m using Python from the Microsoft store:

  WARNING: The script colorcet.exe is installed in 'C:\Users\thequ\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script holoviews.exe is installed in 'C:\Users\thequ\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
1 Like

@metaperl are you installing your packages in a virtual environment?

The warning indicates that the console scripts of colorcet and holoviews are installed, but as their directory is not in PATH they aren’t made available. I guess the same warning was emitted when panel was installed.