Saving text and download it

Hi
I am creating a notepad feature for my project.
I am unable to save and download the text written in the text_area of my note pad.
The code uses the download button it just saves the blank text file and the hard coded input Notes:-

Please help

code

    if st.button("Notes"):
        notes = "Notes:- "
        with st.sidebar:
            st.title("Notes")
            st.text_area("Text", notes, height=300)
            st.download_button(
                label="Download Notes",
                data=notes,
                file_name="my_notes.txt")

the variable notes never changes and it does not reflect the contents in st.text_area. You could try something like:

with st.sidebar:
    st.title("Notes")
    notes = st.text_area("Text", "Notes:- ", height=300)
    st.download_button(label="Download Notes", data=notes, file_name="my_notes.txt")
1 Like

Thank you for your time @edsaac

No change, facing the same issue.

1 Like

At the right corner of the text box, I am getting “press ⌘+enter to apply”
If I do so the text box disappears.

1 Like

That is a different issue, probably coming from the button reverting to False after interacting with the text area.

Check explanation 1 in the 10 most common explanations on the Streamlit forum. Imo, replacing the button with a toogle is usually enough.

1 Like

@edsaac, thanks the toggle did worked, the changes in the text_area box also gets saved and the text box doesn’t disappears.

Wanting to know
Why ‘button’ didn’t worked but ‘toggle’ did?
is there any way we can achieve it with the button ?

1 Like

Because buttons do not retain their state, whereas toggles do. Here is another link where that is explained in detail: Button behavior and examples - Streamlit Docs.

2 Likes