Create a terminal-style wait for response game

Hi! I am trying to create a game that allows users to respond to questions asked by a gpt powered source. I want streamlit to wait for the response/input before running through the rest of the code/rerunning completely. The source takes in their answer and uses that to ask more questions. Right now what is happening is that streamlit keeps rerunning even when some fields are blank/haven’t been answered yet. I tried to fix the situation by making the answer textboxes forms, but that still didn’t work. I also tried using time() and a pause function to no avail. Do you have any advice for how to make streamlit operate like terminal, where it waits for my user’s response before moving on to the next parts of the code?

For example, I want to have my user introduce themselves. So I call this function:

def run_intro_completion(self, game):
with st.form(key=‘my_form2’):
intro_dialogue = st.text_input(label=‘Introduce yourself’)
submit_button = st.form_submit_button(label=‘Submit’)
num_tokens = 0
if submit_button:
return (intro_dialogue, num_tokens, submit_button)

However, it keeps saying that my intro_dialogue variable is Nonetype, because streamlit continues the execution without waiting for the submit button tobe pressed/the intro field to be filled out.

Any help would be greatly appreciated!

1 Like

you can use the st.text_input and st.button functions instead of the form submission method

def run_intro_completion():
    intro_dialogue = st.text_input(label='Introduce yourself')
    submit_button = st.button(label='Submit')

    if submit_button and intro_dialogue:
        return intro_dialogue

intro = run_intro_completion()

if intro is not None:

I’ve just completed a similar type of project where the user has a chat session-- in my particular case I’m prompting the AI in such a way that I can extract questions or other commands from the response. When I detect those questions, I dynamically create the correct number of textarea inputs inside a form element. When the user submits I confirm I have all the answers I need (or cards drawn – in this particular case my app is a AI tarot reading).

adding a textbox for each question - emilytarot/ at main · msull/emilytarot · GitHub

confirming I have all the answers - emilytarot/ at main · msull/emilytarot · GitHub

From there I add the user’s message to the chat history, run it through the Open AI content moderation API endpoint (so I can terminate the session and show mental health resources if something flagged is input) and finally generate a new API response, which starts the loop over:

I’m pretty happy with this flow right now though I’ll be moving more of the logic into the ChatSession class for my next iteration of this.