# Updating/saving csv to local machine with user input

**URL:** <https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408>\
**Category:** Using Streamlit\
**Tags:** pandas\
**Created:** [April 29, 2021, 7:26pm UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408 "2021-04-29T19:26:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathea](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Mathea](https://discuss.streamlit.io/u/Mathea)\
**Post date:** [April 29, 2021, 7:26pm UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408/1 "2021-04-29T19:26:33Z")

</div>

I’m making a bike to work tracking tool for my workplace.

I want users to be able to update the spreadsheet through streamlit with km’s biked every week, and hit a save button to save the points in a master spreadsheet.

Something like  
if st.button(‘Save your information’):  
df.to\_csv(‘Bike to Work Spreadsheet’)

I’ve searched the message board extensively, and tried out this:

```
df = pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']})

st.write(df)
if st.button('save dataframe'):
    open('df.csv', 'w').write(df.to_csv())

```

But no luck. Is this a possibility?

---

<div class="post-metadata">

**Author:** ![BeyondMyself](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/beyondmyself/32/7525_2.png) [@BeyondMyself](https://discuss.streamlit.io/u/BeyondMyself)\
**Post date:** [April 30, 2021, 6:43am UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408/2 "2021-04-30T06:43:12Z")

</div>

here is one solution for you to download df as a csv:

> [@How to download file in streamlit](https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806/78):
>
> in the snippet, part of the code looks like this: prim\_color = st.config.get\_option('theme.primaryColor') or '#F43365' bg\_color = st.config.get\_option('theme.backgroundColor') or '#000000' sbg\_color = st.config.get\_option('theme.secondaryBackgroundColor') or '#f1f3f6' txt\_color = st.config.get\_option('theme.textColor') or '#000000' font = st.config.get\_option('theme.font') or 'sans serif' st.config.get… takes colors from the file ‘config.toml’, so if you define colors …

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [May 3, 2021, 3:36pm UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408/3 "2021-05-03T15:36:11Z")

</div>

Hi @Mathea, welcome to the Streamlit community!

We showed a similar example here, where you basically are reading and writing to the same file to increment a counter:

> [@Incrementing Counter for button clicks](https://discuss.streamlit.io/t/incrementing-counter-for-button-clicks/8047/2):
>
> Hi @Bishop_Sankhe, welcome to the Streamlit community! This took a little more thinking than I expected! But you should be able to do this using the following: import streamlit as st with open("file.txt", "r") as f: a = f.readline() # starts as a string a = 0 if a == "" else int(a) # check if its an empty string, otherwise should be able to cast using int() if st.button("Click me"): a += 1 with open("file.txt", "w") as f: f.truncate() f.write(f"{a}") a

That example overwrites the file each time, so that the counter increments. But you could take the same pattern to append to a file, where you write each additional record to the end of the file.

Best,  
Randy

---

<div class="post-metadata">

**Author:** ![Mathea](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Mathea](https://discuss.streamlit.io/u/Mathea)\
**Post date:** [May 3, 2021, 10:27pm UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408/4 "2021-05-03T22:27:24Z")

</div>

Hi Randy,

Thank you for taking the time.

This solution didn’t stick for me. Perhaps I interpreted the solution incorrectly?

```
if st.button('Save your information'):
    with open("Bike to Work Spreadsheet", "w") as f:
        f.truncate()
        f.write( df.to_csv('Bike to Work Spreadsheet'))

```

Before I give up hope, would you know if it is possible to have this kind of functionality work when I host the app through a service like Heroku?

Thanks,  
M

---

<div class="post-metadata">

**Author:** ![snehankekre](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/snehankekre/32/6504_2.png) [@snehankekre](https://discuss.streamlit.io/u/snehankekre)\
**Post date:** [May 4, 2021, 4:07am UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408/5 "2021-05-04T04:07:34Z")

</div>

Hi @Mathea 😄 You’re on the right track!!

What I’ve done below is first create a text file with the value 0. Then, for every click of the button, I increment the value in the text file by 1. We use this number as the row index of the work spreadsheet that we want to read/write to. With every click of the button, we increment the counter in the progress text file and save the work spreadsheet. Here’s the pseudocode:

```auto
import streamlit as st
import pandas as pd

# save annotated results after every button click
def save_results(results_df, button_press, kms_biked, location_visited):
    results_df.at[button_press, 'kms_biked'] = kms_biked
    results_df.at[button_press, 'location_visited'] = location_visited
    results_df.to_csv('bike_to_work.csv', index=None)
    return None

# load spreadsheet with data to be annotated
@st.cache
def load_data():
    # If this is your first run, create an empty csv file with
    # columns kms_biked and location_visited
    df = pd.read_csv('bike_to_work.csv')
    return df

results_df = load_data()

# track which row of results_df to read
with open("progress.txt", "r") as f:
        button_press = f.readline() # starts as a string
        button_press = 0 if button_press == "" else int(button_press) # check if its an empty string, otherwise should be able to cast using int()

# enter your biking info for the day
kms_biked = st.number_input("km biked today")
location_visited = st.text_input("Where I biked today")

if st.button("Save your information"):
    button_press += 1
    save_results(results_df, button_press, kms_biked, location_visited)

# track which row of results_df to write to
with open("progress.txt", "w") as f:
        f.truncate()
        f.write(f"{button_press}")

```

Hope this helps with your biking tracking tool!

_Happy Streamlit-ing!_ 🎈  
_Snehan_

---

<div class="post-metadata">

**Author:** ![asehmi](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/asehmi/32/1771_2.png) [@asehmi](https://discuss.streamlit.io/u/asehmi)\
**Post date:** [May 4, 2021, 5:10pm UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408/6 "2021-05-04T17:10:49Z")

</div>

Would be so much simpler with a little SQLite DB. Don’t you think? 😉

---

<div class="post-metadata">

**Author:** ![Kareem\_Rasheed\_babat](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/kareem_rasheed_babat/32/44649_2.png) [@Kareem\_Rasheed\_babat](https://discuss.streamlit.io/u/Kareem_Rasheed_babat)\
**Post date:** [August 6, 2021, 3:37pm UTC](https://discuss.streamlit.io/t/updating-saving-csv-to-local-machine-with-user-input/12408/7 "2021-08-06T15:37:04Z")

</div>

Have you tried this @Mathea ?? I am looking for a better solution as well
