Streamlit.radio

Hello,
I added a radio button in my code.
I would like to know how to make sure that the choice to be made is only displayed at a certain time. In my case when the user enters an input. Indeed the choice is displayed from the beginning as you can see on these pictures. In the first image you can see the choice for all the movies but the choice has to be based only on the movie entered by the user (image 2).

And below is my code :
from the model machine learning file :

   def choix_titre():
       titre = st.text_input("Please enter your favorite movie")
       df = df_all[df_all['title'].str.contains(titre.title())]
       df = df.drop_duplicates(subset=["title", 'year'], keep='first')
       df = df.reset_index()
       df = df.drop('index', axis=1)
       df = df[['title', 'year']]
   
       l = []
       o = df.copy()       
       o["year"] = o["year"].apply(lambda x: str(x))
       o["title_year"] = o["title"] + "    ("+o["year"]+")"
       for i in o["title_year"]:
           l.append(i)
   

       status = st.radio("Select your movie", l)

thank you in advance,
Tatiana

Hi Tatiana

As I understand your request, an if-statement will do the work :slight_smile: I added it below at the bottom of the script.

   def choix_titre():
       titre = st.text_input("Please enter your favorite movie")
       df = df_all[df_all['title'].str.contains(titre.title())]
       df = df.drop_duplicates(subset=["title", 'year'], keep='first')
       df = df.reset_index()
       df = df.drop('index', axis=1)
       df = df[['title', 'year']]
   
       l = []
       o = df.copy()       
       o["year"] = o["year"].apply(lambda x: str(x))
       o["title_year"] = o["title"] + "    ("+o["year"]+")"
       for i in o["title_year"]:
           l.append(i)
   
       if len(titre) > 0: # THIS IS THE ADDED LINE
           status = st.radio("Select your movie", l)
1 Like

on a second thought, it is probably better to include the whole computation in the if statement! Like this:

def choix_titre():
   titre = st.text_input("Please enter your favorite movie")
   
   if len(titre) > 0: # THIS IS THE ADDED LINE
       df = df_all[df_all['title'].str.contains(titre.title())]
       df = df.drop_duplicates(subset=["title", 'year'], keep='first')
       df = df.reset_index()
       df = df.drop('index', axis=1)
       df = df[['title', 'year']]

       l = []
       o = df.copy()       
       o["year"] = o["year"].apply(lambda x: str(x))
       o["title_year"] = o["title"] + "    ("+o["year"]+")"
       for i in o["title_year"]:
           l.append(i)
       status = st.radio("Select your movie", l)
1 Like