How to extract the value from streamlit button

I have a dictionary of {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
with st.button when user clicks any of the button I should be able to fetch the brand/model/year… How to do this in streamlit…

Welcome to the community, @Deepak_Rath! We’re thrilled to have you here! :hugs:

Yes, in Streamlit, you can manage button clicks and extract values from a dictionary by using button labels that correspond to the keys of the dictionary. When a user clicks a button, you can display or use the value associated with that button’s label (i.e., the dictionary key).

Here’s a simple example to illustrate how you can achieve this using Streamlit’s st.button() function along with a dictionary:

Recording 2024-04-15 at 11.32.18

import streamlit as st

# Define the dictionary
car_info = {
    "brand": "Ford",
    "model": "Mustang",
    "year": 1964
}

# Create buttons for each key in the dictionary
if st.button('Get Brand'):
    st.write('Brand:', car_info['brand'])

if st.button('Get Model'):
    st.write('Model:', car_info['model'])

if st.button('Get Year'):
    st.write('Year:', car_info['year'])

I hope this is what you were looking for?

Best,
Charly

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.