How to update a string (i.e. a string that changes)?

I have a variable currentValue of type string. This variables changes as the user interacts with the panel. I want to show the string to the user, but I only managed to display the initial value (which is nothing, because I initialize with “”).

How do I update a string shown in a holoviz panel?

Can you show a minimal working example of your code?

I imagine something like:
Link Two Objects in Javascript — Panel v1.2.1 (holoviz.org)

Also, I think you want obj.param.value instead of obj.value
Core Concepts — Panel v1.2.1 (holoviz.org)

Thank you for sending me in the right direction, but the example in the documentation is not working. The markdown display should show the same text as the text input:

Also note that in my case this would create an error: AttributeError: 'str' object has no attribute 'jslink'

See here a simplified version of my code:

select1 = pn.widgets.Select(options={'Square':"np.square(", 'Log':"np.log("})
select2 = pn.widgets.Select(options=df.columns.to_list())
button = pn.widgets.Button(name='Add')

def add_item(event):
    newItem= select1.value + select2.value + ")"
    return newItem
    
variable = pn.bind(add_item, select1) 

formula = ""
def concatenateFormula(event): 
    global formula               
    formula += variable()
    return formula                  

button.on_click(concatenateFormula)

This code allows me to add transformations (= square or log) of the names of dataframes and create a single formula containing all the variables and transformations.

I am not sure if I fully get what you want to achieve. I modified your example, is this what you are after?

import panel as pn

select1 = pn.widgets.Select(options={'Square': "np.square(", 'Log': "np.log("})
select2 = pn.widgets.Select(options=['a', 'b'])
button = pn.widgets.Button(name='Add')

_formula = ""
def concatenate(button):
    global _formula
    _formula += select1.value + select2.value + ")"
    return _formula
formula = pn.bind(concatenate, button=button)

pn.serve(pn.Column(select1, select2, button, formula))

Thank you!

This does seem better. Indeed the first function is not needed. However, I did not know I could bind a string to a button. I thought I could only bind Holoviz widget items.

Also, using returning formula, but _formula is what made it work. Off course, I used global to access the variable, but kinda forgot that the global thing also messes some things up later in the code.

One thing that is not correct, is that I need to initialize an empty string. By default, the first entry in the dropdown menu is selected.

The reason it doesn’t start empty is because concatenate has to be called when viewed for the first time. This kind of problem occurs when a calculation and a viewable item are combined in one function, while state is relevant.

I find it usually more convenient to use (Parameterized) classes and split the modifications from the viewable output. Your program could work something like this:

import panel as pn
import param

class Concatenator(param.Parameterized):

    select1 = param.Selector({'Square': "np.square(", 'Log': "np.log("})
    select2 = param.Selector(['a', 'b'])
    add = param.Event(label='Add')
    clear = param.Event(label='Clear')
    formula = param.List([''], precedence=-1)

    @param.depends('add', watch=True)
    def _add(self):
        self.formula += [self.select1 + self.select2 + ")"]

    @param.depends('clear', watch=True)
    def _clear(self):
        self.formula = []

    def widgets(self):
        return pn.Param(self.param)

    @param.depends('formula')
    def result(self):
        return str(self.formula)


concatenator = Concatenator()
pn.serve(pn.Row(concatenator.widgets, concatenator.result))

OK, this is indeed so much better. One question: How to rearrange the widgets? Previously I placed the select dropdown menus and button in a specific location.

Update: By using concatenator.param.add instead of concatenator.add.

Any specific reason why so many examples on this forum use class variables instead of instance variables?

Well, these parameters aren’t really class variables.

You might want to read up about ‘param’ Parameters and Parmeterized classes here.

In short, the definitions of the parameters are at class level, but when creating an instance each object gets its own parameter objects and the values or not shared.