Below code is working completely fine and updating my excel file available in local system. But, when I deployed this on streamlit using github and uploaded code and “BOOKINGS.csv” file, below code is not updating file “BOOKINGS.csv” stored in github. Do I need to change df.to_write(file.csv) to something else? Please help.
Steps to reproduce
Code snippet:
import streamlit as st
import pandas as pd
df = pd.read_csv("BOOKINGS.csv")
with st.form(key = "Contact Form",clear_on_submit=True):
fullName = st.text_input(label = 'Full Name',placeholder="Please enter your full name")
email = st.text_input(label = 'Email Address',placeholder="Please enter your email address")
submit_res = st.form_submit_button(label = "Submit")
if submit_res == True:
new_data = {"fullName" : fullName,"email" : email}
df = df.append(new_data,ignore_index=True)
df.to_csv("BOOKINGS.csv",index=False)
Yes, of course not.
That’s not how it works, the Streamlit Cloud App doesn’t run “directly” on the GitHub code, but on a file system created by git clone. You would have to push the file back to GitHub with git commit and git push e.g. with the GitPython library. However, I wouldn’t even try this, because it will lead to conflicts if more than one user is using the streamlit app. This is the wrong approach.
Your example would be a typical use-case for an (external) database.
Yes, although it does not necessarily have to be an SQL database. But be aware, that a database doesn’t run neither on GitHub nor on Streamlit Cloud directly, you have to use another external service (DBaaS) for that. Another lightweight option would be to use Google Sheets, which also has an API.
Other possibilities that come to mind off the top of my head:
Firebase
AWS DynamoDB or other DBaaS from AWS (RDS etc.)
DBaaS from various cloud providers (Google Cloud, Azure, MongoDB and many more)
The list is certainly not exhaustive…
Some of them may have a free tier.
Here are a few examples:
Below code can be used to insert into to google sheets using it’s api in your streamlit app.py
import streamlit as st
from google.oauth2 import service_account
import gspread
import pandas as pd
# Create a connection object.
credentials = service_account.Credentials.from_service_account_info(
st.secrets["gcp_service_account"],
scopes=[
"https://www.googleapis.com/auth/spreadsheets",
],
)
gc = gspread.authorize(credentials)
# Get the Google Sheet by URL.
sheet_url = st.secrets["private_gsheets_url"]
sheet = gc.open_by_url(sheet_url)
# Function to find the last filled row in the worksheet.
def find_last_filled_row(worksheet):
return len(worksheet.get_all_values()) + 1
# Function to insert data into the Google Sheet after the last filled row.
def insert_data_into_sheet(dataframe):
worksheet = sheet.get_worksheet(0) # Replace 0 with the index of your desired worksheet
values = dataframe.values.tolist()
# Find the last filled row
last_filled_row = find_last_filled_row(worksheet)
# Insert the data after the last filled row
worksheet.insert_rows(values, last_filled_row)
# Your DataFrame with data to be inserted
df = pd.DataFrame(results, columns=['query', 'batch_index', 'index_of_audio_output_tensor', 'audio_file_name', 'similarity_score_by_model', 'user_relevance_score'])
# Call the function to insert data into the Google Sheet
insert_data_into_sheet(df)
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.