2

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?

2 Answers2

2

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)
arnaud
  • 3,293
  • 1
  • 10
  • 27
  • 1
    I figured st.image() was caching the result, but I did not know how to prevent it. This worked perfectly, thank you very much! – svinjska_mast May 17 '22 at 15:25
1

I think that the correct implementation is:

import requests

def show_kitty():
    image = requests.get("https://cataas.com/cat").content
    st.image(image)

if st.button("Click for cats"):
    show_kitty()

You must consider the sparse code like the main specification of UI, and into the callback if you need you can change the state of global variable (example here) without changing directly the elements of UI. I think that is the best practice.

Nicola Landro
  • 336
  • 3
  • 17