I am new to Streamlit, and am trying to build a simple app which will show a cat picture from https://cataas.com/cat on clicking a button. I have the following simple code:
def show_kitty():
st.image('https://cataas.com/cat')
st.button("Click for cats", on_click = show_kitty())
However, the app renders a picture on the first button click, and the picture does not change on subsequent clicks.
I tried doing the same with texts instead of images, this time displaying cat facts:
def get_cat_fact():
x = requests.get('https://catfact.ninja/fact')
st.text(x.text)
st.button("Click for cat facts", on_click = get_cat_fact())
and it worked - the text changes on every button click.
Does anyone know why this might be? How can I make Streamlit refresh images too?
Probably st.image()
is caching the result, meaning that whenever you call it with the same input parameter, it will simply take the same result as before.
You should simply make it explicit:
import requests
def show_kitty():
image = requests.get("https://cataas.com/cat").content
st.image(image)
st.button("Click for cats", on_click=show_kitty)