Weird button behaviour

I’m building an app to use for manual evaluation of side-by-side search results. The buttons to do the evaluation are generated like this (eventually these print statements will be database writes):

def display_buttons(mapping):
    if st.sidebar.button("Blue is better"):
        print(mapping['blue'] + " is better")
    if st.sidebar.button("Red is better"):
        print(mapping['red'] + " is better")

if st.sidebar.button("Random Query"):
    random_query = random_query()
    st.sidebar.markdown("current query: '" + random_query + "'")
    results_old = ("old", search(random_query, version="old"))
    results_new = ("new", search(random_query, version="new"))
    results = [results_old, results_new]
    color_map = {"blue": results[0][0], "red": results[1][0]}

However, when someone clicks on a button, it does not print anything. I’ve tried changing the line if sidebar.button(“Random Query”) to a checkbox instead. In that case, the checkbox stays checked and it does print something, but it prints out values of the new mapping dictionary after the page reload instead of the original one that it was supposed to have had values loaded from.

It seems like when the button is clicked, the entire script is being reloaded and THEN the code under the conditional is being re-run. This behaviour is baffling. What am I doing wrong?