Hi! My app is a book catalog + recommendation system for books, and I want to give the option for users to submit a form with additional books or authors that are not currently featured in the project. Some parts of the form works, but every time a new user submits a new response the csv file overwrites that entry, so I only have 1 row of data.
This is the code I use:
# submit button
clickSubmit = st.button(‘Submit’)
d = {‘Book Name(s)’: [newBookName],
‘Book Author(s)’: [newBookAuthor],
‘Feedback’: [newFeedback],
‘Name’: [emailName],
‘Email’: [emailAddress]}
df = pd.DataFrame(data=d)
if clickSubmit == True:
st.markdown('<h3>Thank you for your feedback!</h3>', unsafe_allow_html=True)
st.markdown('Submitted responses:')
st.write(df)
open('df.csv', 'w').write(df.to_csv())
else:
st.markdown("Click submit to save form responses.")
And this is my app if you want to check out the current form (go to the ‘Add Books or Authors’ section).
Still working on the book app! That’s great Seriously, your app is looking AWESOME! and the rows of recommended book covers seems to work perfectly! Love the styling and all the visuals!
(hey did you see that you can now make your own theme with custom theming?! That might be right up your alley for this. You just need to upgrade to the newest Streamlit version, check out the post here: Announcing Theming for Streamlit apps! 🎨)
Ok enough of my chit chat and lets get your question answered
So this is an easy fix, and is actually a pandas issue, your line here:
This is actually overwriting your pandas dataframe each time someone does this, its not appending the data at the end of the database. To append is a super quick fix though. You should just be able to directly append that new data on to your df. (Note: I believe you will need to create df as a pandas data frame before you run this, so if you run into an error that could be why)
Thank you so much, @Marisa_Smith! I had to take a 2ish week break on this project but getting back to it now, hoping to implement a few more features, very excited about it I didn’t see the templates until now and will def check them out!
I changed my code to this to make it work (I was getting an assignment error by appending a df that didn’t exist):
df = pd.read_csv(‘df.csv’)
if clickSubmit == True:
d = {'Book Name(s)': [newBookName],
'Book Author(s)': [newBookAuthor],
'Feedback': [newFeedback],
'Name': [emailName],
'Email': [emailAddress]}
st.markdown('<h3>Thank you for your feedback!</h3>', unsafe_allow_html=True)
df = df.append(d, ignore_index = True)
open('df.csv', 'w').write(df.to_csv())
I’m reading a csv with existing data as a df, and appending the new row given the user input. This works in my local server, but not when I commit changes and deploy it on the app the python file is in the same folder as df.csv, so don’t think it’s a path issue. Any thoughts here?
You won’t have write permissions on the root app folder when running on a server (security!). There should be a ~/static folder into which you could write/read a file dynamically, but usually that’s also locked down. The recommended approach would be to use a cloud file store, such as AWS S3, Firebase, Azure Blob Storage, Airtable, etc. There are Python libraries that make this super easy. Search for the Firebase solution posted in this discussion forum for one approach.
@vclugoar Can you please tell in detail how to save the google form details into the csv.
In my case, i have an already existing csv file which has some details and I want to keep appending the google form details.
I’ve needed to update a dataframe and CSV using user data the same way as @vclugoar. I have been using the code from @vclugoar 's last entry.
It almost works to a T but I have a very annoying issue.
Everytime a user submits a new entry, a new column is created at the beginning of the dataframe (" Unnamed: 0.X ".
There are as many new columns as there are new entries.
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.