Linking slider to argument of a function

Hello,

I’m new to panel and most likely have a misunderstanding. Hope somebody can help me.

I have a function with an argument and I want to link that argument to a slider.

Here is a very simplified example:

def my_func(argument):
    # complex operations based on the argument, not shown here
    print(argument*5)
    return

func_inst = my_func(argument=5)

arg_slider = pn.widgets.IntSlider(start=0,end=19,value=10)
arg_slider.link(my_func,value='argument')

pn.Row(arg_slider,func_inst)

This shows the slider and the initial result (from argument 5), but when I move the slider, the function is not recalculated.

I know that there are different ways to do this, e.g. via “interact”. For the example I have, interactive causes a few headaches, as I have multiple sliders and need to put them in different places on the panel.

I am not sure if “link” really supports a handling as I’m looking for, i.e. if it can link a slider and a function-argument. Please just say if that is not possible.

Thanks for your help!

Cheers,
Georg

I think you need bind your function to the arguments. Link is used to link two different widgets or parameters.

arg_slider.link(my_func,value=‘argument’)

by

pn.bind(my_func, arg_slider)

a most detailed explanation can be found in the getting started section ()

1 Like

Another good introduction to panel is Introduction to data apps with Panel, Pydata Copenhagen, March 2022 - YouTube by @Marc.

Where he goes over a very similar example.

2 Likes

Welcome to the community @grgmyr

Thanks for asking your questions here. It will also help the next user trying to get started with Panel and help guide the documentation overhaul which is on the roadmap.

1 Like

Thank you all so much! This did the job and I am amazed who simple and clean everything can be done by using bind - I thought much too complicated. Thanks also for the video link, this was also helpful.

Cheers!

3 Likes