How do I convert my select box too multiselect with dynamic code

Hi guys, so this is my sample code

> menu=['Guideline1','Guideline2','Guideline3']
> choice = st.sidebar.selectbox("Choose Guideline", menu)
> 
> st.write("")
> if choice == 'Guideline1':
>     if num_clicks:
> 
>         cdqa_pipeline = joblib.load("Guideline1.joblib")
>         
>         prediction = cdqa_pipeline.predict(num_clicks, n_predictions=n_predictions)
> 
>         for i, value in enumerate(prediction):
>             answer, title, paragraph, predictionsss = value
>             annotated_text(("Answer:", '', "#8ef"))
>             answer_var = st.write(answer)
>             paragraph_var = st.write(paragraph)
>             
> if choice == 'Guideline2':
>     if num_clicks:
> 
>         cdqa_pipeline = joblib.load("Guideline2.joblib")
>         
>         prediction = cdqa_pipeline.predict(num_clicks, n_predictions=n_predictions)
> 
>         for i, value in enumerate(prediction):
>             answer, title, paragraph, predictionsss = value
>             annotated_text(("Answer:", '', "#8ef"))
>             answer_var = st.write(answer)
>   
>             
> if choice == 'Guideline3':
>     if num_clicks:
> 
>         cdqa_pipeline = joblib.load("Guideline3.joblib")
>         
>         prediction = cdqa_pipeline.predict(num_clicks, n_predictions=n_predictions)
> 
>         for i, value in enumerate(prediction):
>             answer, title, paragraph, predictionsss = value
>             annotated_text(("Answer:", '', "#8ef"))
>             answer_var = st.write(answer)

what I want to do now is, make the select box , as multiselect box and the code runs dynamically based on options.
If I choose guidelines 1& 3, in the line
cdqa_pipeline = joblib.load(" … ") here it should read both Guideline1.joblib and Guideline3.joblib
and outputs result from both!

any advice on how to do this?
Thankyou!

Hello @Hiba_Fatima

You could do something like

choices = st.sidebar.multiselect("Choose Guidelines", menu)
if "Guideline1" in choices:
    ...do stuff with guideline1
if "Guideline2" in choices:
    ...do stuff with guideline2

Or if you want to take it to the next level, you could try to extract a method something like

def show_guideline_predictions(guideline,  n_predictions):
    cdqa_pipeline = joblib.load(f"{guideline}.joblib")
         
    prediction = cdqa_pipeline.predict(num_clicks, n_predictions=n_predictions)
 
    for i, value in enumerate(prediction):
        answer, title, paragraph, predictionsss = value
        annotated_text(("Answer:", '', "#8ef"))
        st.write(answer)
        st.write(paragraph)


choices = st.sidebar.multiselect("Choose Guidelines", menu)
for guideline in choices:
    if num_clicks:
        show_guideline_predictions(guideline, n_predictions)

Best,
Peter

2 Likes

I was just about to suggest something similar, more generically coded like this:

joblib_map = {'job1': 'job1.joblib', 'job2': 'job2.joblib', 'job3': 'job3.joblib'}
def run_job(job):
    joblib = joblib_map.get(job, None)
    if joblib:
        st.write(f'Running {joblib}!!')
    else:
        st.error(f'Unable to get job lib for {job}')

jobs=['job1','job2','job3']
jobs_chosen = st.multiselect("Choose Job", options=jobs, default=[])
if st.button('Run Jobs'):
    for job in jobs_chosen:
        run_job(job)
else:
    st.info(f'Waiting to run: {jobs_chosen}')

Note, this is far (far, far) from being a recommended job execution approach but may work for your use case. There are great libraries for proper job execution which deal with long-running jobs (timeouts, restarts, etc.), parallelism, state and fault management, etc.