Simple text with dynamic part

Hi,
maybe I am a little bit confused, but I struggle building up a simple Text with a variable that is updated by running a method or input widget.
I thought about just taking a pn.pane.Markdown like this

import param
import holoviews as hv
import panel as pn
hv.extension('bokeh')
pn.extension()
class UpdateText(param.Parameterized):
    someinput = param.ObjectSelector(default = '0', objects=['0','1','2','3','4','5','6'])
         
    def dchg_explanation(self):
        text = pn.pane.Markdown("""
                                ###Selectet Discharge
                                The selected discharge is:  self.someinput ausgewählt. 
                                Some more text.""")
    
        return text
test = UpdateText()
pn.Row(test.param, test.dchg_explanation)

Maybe I just don’t know how to handle Markdown…
Maybe anyone can give me a hint.

1 Like

Two things you have to do.

  1. make the function depends on someinput
  2. make the string an f-string.
import param
import panel as pn

pn.extension()


class UpdateText(param.Parameterized):
    someinput = param.ObjectSelector(
        default="0", objects=["0", "1", "2", "3", "4", "5", "6"]
    )

    @pn.depends("someinput")
    def dchg_explanation(self):
        text = pn.pane.Markdown(
            f"""
            ###Selectet Discharge
            The selected discharge is: {self.someinput} ausgewählt. 
            Some more text.
            """
        )
        return text


test = UpdateText()
pn.Row(test.param, test.dchg_explanation)
1 Like

Thx, that works!

1 Like

Sorry for the necropost, but Google still surfaces this post and I wanted to add an alternative usage. I work mainly with pn.widgets.*. Based on the above, I discovered that the following syntax is also valid:

import panel as pn

pn.extension()

some_input = pn.widgets.Select(
    name='Some Input',
    options=list(range(7))
)

@pn.depends(some_input)
def dchg_explanation(some_input):
    return pn.pane.Markdown(
        f"""
        ### Selected discharge
        
        The selected discharge is: {some_input} ausgewählt.
        Some more text.
        """
    )

pn.Row(some_input, dchg_explanation)

I’d suggest

import panel as pn

pn.extension()

some_input = pn.widgets.Select(name="Some Input", options=list(range(7)))


my_markdown = pn.pane.Markdown(
    object=pn.bind(
        lambda _: f"""
        ### Selected discharge
        
        The selected discharge is: {some_input} ausgewählt.
        Some more text.
        """,
        some_input,
    )
)

pn.Row(my_markdown).servable()