Trying to store multiple structure from st_ketcher, but keep getting resets

I have a very simple streamlit ketcher implementation that draws a molecule, which I then want to have a button to store the content. This is the section of code that’s relevant, I’ve removed references to other elements not in play.

    scaffold_list = []
    scaffold = ""
    col1, col2, col3 = st.columns([50, 30, 20])

    with col1:
        scaffold = st_ketcher(height=650, molecule_format="MOLFILE", key="ketcher")

        if st.button(
            "Store Core",
        ):
            if len(scaffold) > 0 and scaffold not in scaffold_list:
                scaffold_list.append(scaffold)

        st.write("Cores to Run")

        for scaffold in scaffold_list:
            # DO STUFF

So far, so good, but what I’ve found is that, despite scaffold_list being global, every time I update the ‘scaffold’ ketcher object with apply, the list is cleared and I can’t store anything. What am I missing to keep that list of strings alive? I don’t normally have to put non Streamlit elements in session storage, but I’m wondering if that’s what’s needed

When you press the button, the script reruns and all the variables in the script are lost. scaffold_list seems to be session-scoped, so having it in session state looks like the right thing to do if you want to keep it across reruns.

1 Like

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