Dynamic Button Chainlit
unknown
python
a year ago
1.4 kB
80
Indexable
import chainlit as cl # Define a function to handle user input and dynamically create buttons async def handle_user_input(user_input): # This is a placeholder for dynamic content generation logic # For demonstration, let's assume the response contains a list of options options = ["Dynamic Option 1", "Dynamic Option 2", "Dynamic Option 3"] response_text = f"You said: {user_input}. Here are some options:" return response_text, options # Define the Chainlit app @cl.on_message async def main(message: cl.Message): # Get user input from the message user_input = message.content # Get the response text and dynamic options response_text, options = await handle_user_input(user_input) # Create buttons for the dynamic options buttons = [cl.Button(name=option, value=option) for option in options] # Send the message with buttons to the user await cl.Message(content=response_text, buttons=buttons).send() # Handle button clicks @cl.on_button async def on_button_click(button: cl.Button): user_choice = button.value # Process the user's choice (for now, just echo back the choice) response = f"You chose: {user_choice}" # Send the response back to the user await cl.Message(content=response).send() # Run the Chainlit app if __name__ == "__main__": cl.run(main)
Editor is loading...
Leave a Comment