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:
We showed a similar example here, where you basically are reading and writing to the same file to increment a counter:
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.
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:
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}")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.