Updating a value based on current MultiSelect and RadioButtonGroup selections

Hello, this is a screenshot of my current Panel app:

My objective is to formulate a sentence based on current MultiSelect and RadioButtonGroup selections.

For example, in the screenshot above, the TextAreaInput should have the following text in it:

I am guessing that you are feeling compassionate, loving and absorbed

because:

  1. the RadioButtonGroup has “guessing that you are feeling” selected
  2. the feeling mentioned in the sentence are the ones currently selected from the MultiSelect.

You could probably use pn.bind()

def build_sentence(word1, word2, word3, word4):
    ...

pn.bind(build_sentence, word1=widget1, word2=widget2)
1 Like

I have initiated my attempt to use pn.bind but toggling the value of the RadioButtonGroup does not update the TextAreaInput even though I think I am binding it.

import panel as pn

feelings = dict()
positive_feelings = list()
negative_feelings = list()

# Below we create a bunch of feelings.
# the positive feelings are a list of lists. and so are the negative ones.
# The first item in each list is the category of that list of feelings.
# E.g. "affection" is the category for all of the feelings in that list of
# positive feelings.

positive_feelings.append("""affectionate
compassionate
fond
loving
openhearted
tender
warm""".split())

positive_feelings.append("""engaged
absorbed
curious
engrossed
enchanted
enthralled
entranced
fascinated
interested
intrigued
involved
open
spellbound
stimulated""".split())

negative_feelings.append("""anger
aggravated
angry
animosity
annoyed
contempt
disgruntled
enraged
exasperated
furious 
hate 
hostile
incensed
irate
irritated
irked
livid
miffed
nettled
outraged
peeved
resentful""".split())

negative_feelings.append("""aversion
abhorrence
appalled
bothered displeased
disgust
dislike 
enmity
horrified
loathing
repulsion
revulsion""".split())

feelings['positive'] = positive_feelings
feelings['negative'] = negative_feelings

i_am = pn.pane.Markdown("I AM").servable()
feeling_toggle = pn.widgets.RadioButtonGroup(
    name='Radio Button Group', options=['feeling', 'guessing you are feeling'],
    button_type='success').servable()

for f in feelings:
    print(f"FEELING {f}")
    pn_row = pn.Row(f'### {f} feelings:', scroll=False).servable()
    for list_of_feelings in feelings[f]:
        print(f"--> Currently working with {list_of_feelings}")
        card_label = list_of_feelings[0].upper()
        card_select = pn.widgets.MultiSelect(options=list_of_feelings)
        card = pn.Column(f'### {card_label}', card_select)
        pn_row.append(card)

ui_sentence = pn.widgets.TextAreaInput(name='sentence', auto_grow=True).servable()


def build_sentence(feeling_expr):
    sentence = "I am " + feeling_expr
    return sentence


def update_ui_sentence(feeling_subject):
    sentence = build_sentence(feeling_subject)
    ui_sentence.value = sentence


pn.bind(update_ui_sentence, feeling_toggle)


I have this working well enough for the time being. I will open more threads as the code progresses:

import panel as pn

feelings = dict()
positive_feelings = list()
negative_feelings = list()

# Below we create a bunch of feelings.
# the positive feelings are a list of lists. and so are the negative ones.
# The first item in each list is the category of that list of feelings.
# E.g. "affection" is the category for all of the feelings in that list of
# positive feelings.

positive_feelings.append("""affectionate
compassionate
fond
loving
openhearted
tender
warm""".split())

positive_feelings.append("""engaged
absorbed
curious
engrossed
enchanted
enthralled
entranced
fascinated
interested
intrigued
involved
open
spellbound
stimulated""".split())

negative_feelings.append("""anger
aggravated
angry
animosity
annoyed
contempt
disgruntled
enraged
exasperated
furious 
hate 
hostile
incensed
irate
irritated
irked
livid
miffed
nettled
outraged
peeved
resentful""".split())

negative_feelings.append("""aversion
abhorrence
appalled
bothered displeased
disgust
dislike 
enmity
horrified
loathing
repulsion
revulsion""".split())

feelings['positive'] = positive_feelings
feelings['negative'] = negative_feelings


ui_sentence = pn.widgets.TextAreaInput(auto_grow=True).servable()


i_am = pn.pane.Markdown("I AM").servable()



def build_sentence(feeling_expr):
    sentence = "I am " + feeling_expr
    return sentence


def update_ui_sentence(feeling_subject):
    sentence = build_sentence(feeling_subject)
    print(f"updated sentence {sentence}")
    ui_sentence.value = sentence


feeling_toggle = pn.widgets.RadioButtonGroup(
    name='Radio Button Group', options=['feeling', 'guessing you are feeling'],
    button_type='success')

sentence_reactor = pn.bind(update_ui_sentence, feeling_toggle)

pn.Column(feeling_toggle, sentence_reactor).servable()

for f in feelings:
    print(f"FEELING {f}")
    pn_row = pn.Row(f'### {f} feelings:', scroll=False).servable()
    for list_of_feelings in feelings[f]:
        print(f"--> Currently working with {list_of_feelings}")
        card_label = list_of_feelings[0].upper()
        card_select = pn.widgets.MultiSelect(options=list_of_feelings)
        card = pn.Column(f'### {card_label}', card_select)
        pn_row.append(card)