Streamlit button press returns None but function works in Jupyter

Hi all,

I am new to Streamlit and trying to make this simple on button click call this order function. When I try to run this via Streamlit it returns None as output.

But, if I run the api.place_single_order() function in Jupyter with same arguments that returns proper order details.

What could be wrong with this?

@st.cache_resource
def api(
    username,
    password,
):
    api = OrderSystem(
        userId=username,
        password=password,
    )
    return api


def display_dashboard(
    username,
    password,
):

    Store = api(
        username,
        password,
    )

    with st.form(key="my_form", clear_on_submit=True):
        product = st.selectbox("Select product", ("Apple", "Banana"))
        qty = st.number_input(label="Quantity", min_value=1)
        submit_buy = st.form_submit_button(label="Buy")


    if submit_buy:
       
        order = Store.place_single_order(
            product=product,
            qty=qty,
        )
        st.write(order)
        orderid = order.no
        st.write(f"Placed order with number  {orderid}")


if __name__ == "__main__":

    display_dashboard(
        username,
        password,
    )

st.write basically returns None. I also tried st.text and result is the same. The code runs outside in Jupyter perfectly fine.

The app is running in docker container. Python version is 3.11 and Streamlit version is Streamlit v1.31.1

Your code raises NameError because submit_buy_call is undefined. After defining it as True, it raises AttributeError because api is a function you just defined above and it does’t have an attribute place_single_order.

@Goyo Sorry, earlier I did not give full code. Can you re-check once, I have updated the code.

It works as I expected for me with my own implementation of OrderSystem.

Of course st.write returns None, but st.write(order) should write the order (and it does for me). What writing the order means exactly depends of what exactly order is, so I don’t know what to expect here. But note that if order were None, executing orderid = order.no would raise an exception.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.