Driving subsequent widget options using toggle choices

This is my first time posting. I’m trying to build a web app that will take user input from a number of widgets and will then create an SVG based on the inputs. The code when presenting all of the widgets works great. However, I want to present a subset of questions to users based on whether things apply to them. I’ve set it up to use a toggle to let users select which things apply to them, and would like to show the appropriate widgets based on their selection.

My larger application includes a parameterized class, but I can’t get this simplified version to work and I’ve hit the edge of my current ability to troubleshoot. I’ve tried… a lot of things, and nothing has worked.

import panel as pn

tplt = pn.template.FastListTemplate(
    title='Community Mosaic', header_background="#143250", theme_toggle=False,
    main_max_width='1200px',
    accent_base_color="#1DC2BB")

tplt.main.append('Intro text')

name_input = pn.widgets.TextInput(name='My name is:')
toggle = pn.widgets.ToggleGroup(options=['I am a consultant','I have an internal role'] )

client_input = pn.widgets.RadioBoxGroup(name='My client work is in: ', options=['SEA','DAL', 'CHI', 'SF'], inline=True)
hybrid_role_input = pn.widgets.RadioBoxGroup(name='I serve in: ', options=['Capability Leadership', 'Account Leadership', 'Industry Leadership'], inline=True)
internal_func_input = pn.widgets.Select(name='My main function (internal) is: ', options=['People Team','Finance','Sales'])

all_ppl_inputs = pn.Column(name_input, toggle)
consult_only_inputs = pn.Column(client_input)
hybrid_inputs = pn.Column(client_input, hybrid_role_input)
internal_inputs = pn.Column(internal_func_input)

tplt.main.append(all_ppl_inputs)
role_inputs = pn.Column()

def callback(event):
    print(event)
    if event.new == ["I'm consulting"]:
        role_inputs.object = consult_only_inputs

watcher = toggle.param.watch(callback, ['value'], onlychanged=False)

tplt.show()
1 Like

Hi @laurajludwig

As I see it you are trying to develop a Questionnaire with a lot of Questions. It is similar to a Pipeline with Stages.

Panel has pipeline functionality here. You might in fact be able to use it or just get some inspiration. There might also be some inspiration in the code panel/pipeline.py.

If I where to get started my code would start out with a structure like the below

import param
import panel as pn

class Question(pn.viewable.Viewer):
    question = param.String(doc="""The question as a string""")
    choice = param.Selector(doc="""The selected value""")

    previous = param.Action(doc="""The user can trigger this to go back to the parent question""")
    next = param.Action(doc="""The user can trigger this to go to the next question""")
    
    
next_panels = param.Dict(doc="""A dictionary with choices as keys and panels as values""")

    _widgets = {

    }

class Questionaire(pn.viewable.Viewer):
    questions = param.List(class_=Question, doc="""The questions asked. Would start with one question and add questions when next is triggered. Would remove questions if previous is triggered.""")

The Questionarie would react to the previous and next events of the Questions and show the appropriate panel.

2 Likes

Thank you for this suggestion - I’ll give it a try and report back on what works!

1 Like