Saving user data from a live intake form

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).

Any thoughts on how to get around this issue?

@vclugoar hey!

Still working on the book app! That’s great :smile_cat: 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 :rofl:

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)

df = df.append(pd.DataFrame(data=d))

here is the pandas docs if you needed more info on this: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html

Happy Streamlit-ing!
Marisa

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 :slight_smile: 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 :frowning: 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.

Thankyou!

Hello @vclugoar, your app looks really awesome! Would you mind to share the code?
Thanks and have a nice day!
Harry

Hi,

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.


Anyone have an idea on what the issue might be?
Many thanks