Interactions#
The interact
function (panel.interact)
automatically creates controls for interactively browsing code and data.
[1]:
import panel as pn
from panel.interact import interact, interactive, fixed, interact_manual
from panel import widgets
pn.extension()
interact
#
At the simplest level, interact
controls are automatically generated for function arguments, and the function is then called with those arguments when you interactively edit the controls. To use interact
you need to define a function that you want to examine. Here is a function that returns the only argument: x
.
[2]:
def f(x):
return x
If you pass this function as the first argument together with an integer keyword argument x=10
to interact
a slider is generated and bound to the function parameters.
[3]:
interact(f, x=10)
[3]:
If you move the slider, the function is called, which outputs the current value of x
. If you pass True
or False
interact
generates a check box:
[4]:
interact(f, x=True)
[4]:
When you pass a string, interact
generates a text area.
[5]:
interact(f, x='Hi Pythonistas!')
[5]:
interact
can also be used as a Decorator. In this way you can define a function as well as determine the type of interaction. As the following example shows, interact
works also with functions that have multiple arguments.
[6]:
@interact(x=True, y=1.0)
def g(x, y):
return (x, y)
g
[6]:
Layout of interactive widgets#
The interact
function returns a panel that contains the widgets and the display output. By indexing these panels we can lay out the objects exactly how we want:
[7]:
import numpy as np, pandas as pd, matplotlib.pyplot as plt
%matplotlib inline
def mplplot(df, **kwargs):
fig = df.plot().get_figure()
plt.close(fig)
return fig
def sine(frequency=1.0, amplitude=1.0, n=200, view_fn=mplplot):
xs = np.arange(n)/n*20.0
ys = amplitude*np.sin(frequency*xs)
df = pd.DataFrame(dict(y=ys), index=xs)
return view_fn(df, frequency=frequency, amplitude=amplitude, n=n)
[8]:
i = pn.interact(sine, n=(5,100))
pn.Row(i[1][0], pn.Column("<br>\n### Sine waves", i[0][0], i[0][1]))
[8]:
[9]:
layout = interact(f, x=10)
pn.Column('**A custom interact layout**', pn.Row(layout[0], layout[1]))
[9]:
Set arguments with fixed
#
There may be times when you want to examine a function using interact
, but want to set one or more of its arguments to specific values. This can be achieved using the fixed
function:
[10]:
def h(p, q):
return (p, q)
[11]:
interact(h, p=5, q=fixed(20))
[11]:
Widget abbreviations#
If you pass certain values, interact
use automatically the appropriate widget, e.g. a checkbox for True
or IntSlider
for integer values. So you don’t have to explicitly specify the appropriate widget:
[12]:
interact(f, x=widgets.FloatSlider(start=0.0,end=10.0,step=0.01,value=3.0))
[12]:
[13]:
interact(f, x=(0.0,10.0,0.01,3.0))
[13]:
This example shows how the keyword arguments are processed by interact
:
If the keyword argument is an instance of
Widget
with anvalue
attribute, this widget is used. Any widget with anvalue
attribute can be used, including custom ones.Otherwise, the value is treated as a Widget Abbreviation that is converted to a widget before use.
The following table gives an overview of the various Widget Abbreviations:
Keyword argument |
Widget |
---|---|
|
|
|
|
Integer value as |
|
Floating-point |
|
|
|