Hi, I am new to streamit. A small question is I need to display a value that changes when a button is clicked. Lets say I have a caption/header etc that needs to be changed when I click a button. is this possible?
Steps to reproduce
import streamlit as st
import pandas as pd
import numpy as np
st.title(“Sample Tutorial for adding numbers”)
col1, col2 = st.columns(2)
with col1:
a = st.text_input("Please enter number 1",key = "number 1" , help = "Enter the first number")
b = st.text_input("Please enter number 2",key = "number 2" , help = "Enter the second number")
# sum_value = st.metric(value = 1, label = "sum")
#
sum = st.button("Calculate Sum", key = "calculate")
sum_caption = st.caption("The sum is:")
if sum:
try:
a = float(a)
b = float(b)
c = a + b
st.write(c)
except:
st.exception("One of the numbers entered is not float")
I’m not 100% sure how you want to modify your code as it seems to work when I use it, but you can create st.empty() containers and then update these such as in the code below
import streamlit as st
import pandas as pd
import numpy as np
st.title("Sample Tutorial for adding numbers")
col1, col2 = st.columns(2)
sum_caption = st.empty()
sum_caption.caption("The sum is:")
mycaption = st.empty()
with col1:
a = st.text_input("Please enter number 1",key = "number 1" , help = "Enter the first number")
b = st.text_input("Please enter number 2",key = "number 2" , help = "Enter the second number")
# sum_value = st.metric(value = 1, label = "sum")
#
mysum = st.button("Calculate Sum", key = "calculate")
if mysum:
try:
a = float(a)
b = float(b)
c = a + b
mycaption.caption(c)
except:
st.exception("One of the numbers entered is not float")
Hi Luke. Thanks a lot for the answer. I was able to find it online the same answer and it worked. Nonetheless I think that it would be much easier if we are able to access the value of widget later and change it if possible. What do you think?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.