Replate st.write with a new value from an api request

Hello, everyone!
I’m using requests module to request a certain value from a website api that varies over time, like a cryptocurrency price for example. So i’m using st.write(price) and i would like to replace this value instead of printing the price in a new line.

For now i’m using this code:

while True:
# Get request
resp = request.post(url, headers=headers, data=data)
# Transform in a json file
newjson = resp.json()
# Get the value (price is a str)
price = newjson[‘rows’][0][‘price’]
# Print the price
st.write('The price is: {}".format(price))

I need the script to print the new value in a little time like every 1 second, 2 seconds max.
Thank you!

Hi @lbrtonny, welcome to the Streamlit community!

This pattern is documented in the st.empty section:

https://docs.streamlit.io/en/stable/api.html?highlight=empty#streamlit.empty

Effectively, you define a container outside of the loop, then continually overwrite its value.

Best,
Randy