# St.download button replace by st.button

**URL:** <https://discuss.streamlit.io/t/st-download-button-replace-by-st-button/40782>\
**Category:** Using Streamlit\
**Tags:** file-download\
**Created:** [April 5, 2023, 8:40am UTC](https://discuss.streamlit.io/t/st-download-button-replace-by-st-button/40782 "2023-04-05T08:40:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arief\_Surya](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Arief\_Surya](https://discuss.streamlit.io/u/Arief_Surya)\
**Post date:** [April 5, 2023, 8:40am UTC](https://discuss.streamlit.io/t/st-download-button-replace-by-st-button/40782/1 "2023-04-05T08:40:04Z")

</div>

Hi There,  
If i using python, and i run this code, i can get new file with name "howtodownload.pdf)

```auto
import requests

URL = "https://www.codingem.com/python-download-file-from-url/"
response = requests.get(URL)
open("howtodownload.pdf", "wb").write(response.content)

```

NOW, my problem, i want to make st.button, when st. button clicked can download my url and then do some code.

```auto
saveto_gsheet=st.button("Save to Google Sheet")
if saveto_gsheet:
import requests
URL = "https://www.codingem.com/python-download-file-from-url/"
response = requests.get(URL)
open("instagram.pdf", "wb").write(response.content)
(and then some code to save to google sheet)

```

With this code, when i click button “Save to Google Sheet”, i can’t get my file (that code isn’t download my file). Why ?

If i not using st. button i can get that file, now if i using st.button i can’t get that file. thanks before

---

<div class="post-metadata">

**Author:** ![CarlosSerrano](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/carlosserrano/32/49007_2.png) [@CarlosSerrano](https://discuss.streamlit.io/u/CarlosSerrano)\
**Post date:** [April 6, 2023, 7:18pm UTC](https://discuss.streamlit.io/t/st-download-button-replace-by-st-button/40782/2 "2023-04-06T19:18:29Z")

</div>

You can try a callback function instead and encapsulate your saving to sheet in the function, then use the on\_click parameter for the button to call the function.

---

<div class="post-metadata">

**Author:** ![blackary](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/blackary/32/47725_2.png) [@blackary](https://discuss.streamlit.io/u/blackary)\
**Post date:** [April 6, 2023, 7:44pm UTC](https://discuss.streamlit.io/t/st-download-button-replace-by-st-button/40782/3 "2023-04-06T19:44:58Z")

</div>

It seems like it’s working fine. Here I saved it to a text file instead of a pdf:

```python
from pathlib import Path

import requests
import streamlit as st

saveto_gsheet = st.button("Save to Google Sheet")
if saveto_gsheet:
    URL = "https://www.codingem.com/python-download-file-from-url/"
    response = requests.get(URL)
    open("instagram.txt", "w").write(response.text)
    st.write("Saved!")
    # (and then some code to save to google sheet)

st.write("Contents of instagram.txt:")

p = Path("instagram.txt")

if p.exists():
    st.code(p.read_text())

else:
    st.write("Not found!")

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [April 5, 2024, 7:44pm UTC](https://discuss.streamlit.io/t/st-download-button-replace-by-st-button/40782/4 "2024-04-05T19:44:59Z")

</div>

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