Display Content with pn.panel
In this guide, we will learn to display Python objects easily with pn.panel
:
Display any Python object via
pn.panel(the_object, ...)
.
:::{note} When we ask to run the code in the sections below, we may execute the code directly in the Panel docs via the green run button, in a cell in a notebook, or in a file app.py
that is served with panel serve app.py --dev
. :::
Display a String
To build an app, our first step is to display things. Luckily, Panel provides us with the simple yet powerful pn.panel()
function. This function effortlessly transforms Python objects into viewable components within our app. Let's start with something simple: a string.
Run the code:
:::{note} We add .servable()
to the component to add it to the app served by panel serve app.py --dev
. Adding .servable()
is not needed to display the component in a notebook. :::
pn.panel
uses a heuristic algorithm to determine how to best display the object
. To make this very explicit, we will print
the component in all the examples below.
Run the code:
Your cell or terminal output should contain Markdown(str)
. It means pn.panel
has picked a Markdown
pane to display the str
object.
Let's verify that markdown strings are actually displayed and rendered nicely.
Run the code:
Display a DataFrame
Now that we've mastered the art of displaying strings, let's take it up a notch. In our journey to build a data-centric app, we'll often need to display more complex objects like dataframes. With Panel, it's as easy as pie.
Run the code:
:::{tip} If we want to display larger dataframes, customize the way the dataframes are displayed, or make them more interactive, we can find specialized components in the Component Gallery supporting these use cases. For example, the Tabulator widget and Perspective pane. :::
Display Plots
Many data apps contains one or more plots. Lets try to display some.
Pick a plotting library below.
:::::{tab-set}
::::{tab-item} Altair :sync: altair
Run the code below:
Please notice that pn.panel
chose a Vega
pane to display the Altair figure.
:::{note} Vega is the name of the JavaScript plotting library used by Altair.
We must add "vega"
as an argument to pn.extension
in the example to load the Vega Javascript dependencies in the browser.
If we forget to add "vega"
to pn.extension
, then the Altair figure might not display. :::
::::
::::{tab-item} hvPlot :sync: hvPlot
Run the code below:
Please notice that pn.panel
chose a HoloViews
pane to display the hvPlot figure.
:::{note} hvPlot is the easy to use plotting sister of Panel. It works similarly to the familiar Pandas .plot
API. hvPlot is built on top of the data visualization library HoloViews. hvPlot, HoloViews, and Panel are all part of the HoloViz family. :::
::::
::::{tab-item} Matplotlib :sync: matplotlib
Run the code below:
Please notice pn.panel
chose a Matplotlib
pane to display the Matplotlib figure.
:::{note} In the example above we provided arguments to pn.panel
. These will be applied to the pane selected by pn.panel
to display the object. In this example the Matplotlib
pane is selected.
The arguments dpi
, format
and tight
would not make sense if a string was provided as an argument to pn.panel
. In that case, pn.panel
would pick a Markdown pane and the exception TypeError: Markdown.__init__() got an unexpected keyword argument 'dpi'
would be raised. :::
::::
::::{tab-item} Plotly :sync: plotly
Please notice that pn.panel
chose a Plotly
pane to display the Plotly figure.
:::{note} We must add "plotly"
as an argument to pn.extension
in the example to load the Plotly Javascript dependencies in the browser.
If we forget to add "plotly"
to pn.extension
, then the Plotly figure might not display. :::
::::
:::::
Display any Python object
pn.panel
can display (almost) any Python object.
Run the code below:
Display any Python object in a layout
If we place objects in a layout like pn.Column
(more about layouts later), then the layout will apply pn.panel
for us automatically.
Run the code below:
Please notice that the image of the wind turbine is quite large. To fine-tune the way it is displayed, we can use pn.panel
with arguments.
Run the code below:
:::{note} The example above sets the css styles
of the Audio
player. The styles
parameter is introduced in the Styles tutorial. :::
Consider Performance
pn.panel
is a versatile helper function that converts objects into a Pane. It automatically selects the best representation for an object based on available Pane types, ranking them by priority.
For optimal performance, specify the desired Pane type directly, like pn.pane.Matplotlib(fig)
instead of using pn.panel(fig)
. You will learn about Panes in the Display Content with Panes section.
Recap
In this guide, we have learned to display Python objects easily with pn.panel
:
Display a string with
pn.panel(some_string)
Display plot figures like Altair, hvPlot, Matplotlib and Plotly with
pn.panel(fig)
Display DataFrames with
pn.panel(df)
Display most Python objects with
pn.panel(some_python_object)
Configure how an object is displayed by giving arguments to
pn.panel
Display most Python objects in layouts like
pn.Column
with and without the use ofpn.panel
Use a specific Pane instead of
pn.panel
if performance is keyAdd JavaScript dependencies via
pn.extension
. For examplepn.extension("vega")
orpn.extension("plotly")