I’m sorry if this is a very basic issue but I’m trying to write to a file every time a form is submitted. I’m creating a note-taking application and I want to write to a file and save notes.
I have a file called notes.csv that exists in my github repository. I’ve been trying to open the file and write to it in my callback function whenever the button is clicked (on-click).
But everytime I try to do it, the file does not update at all. I’m wondering why its not being written to?
if 'note_state' not in st.session_state:
st.session_state.note_state = {}
def form_callback(stock_ticker_input, note_input):
st.session_state.note_state[stock_ticker_input] = [note_input]
st.session_state.note_state
f = open('notes.csv', 'w') #write mode
writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow([stock_ticker_input, note_input])
f.close()
with st.form(key="my_form"):
st.write("Enter Note")
stock_ticker_input = st.text_input('Stock', key='ticker')
note_input = st.text_input('Note', key='note')
submitted = st.form_submit_button("Submit", on_click=form_callback(stock_ticker_input, note_input), args=[stock_ticker_input, note_input])
if submitted:
st.write("Note", note_input, "stock_ticker", stock_ticker_input)
The on_click parameter that you need to pass to the button is the function per se (i.e., form_callback), not to call the function (i.e., form_callback(xxx)). Imo, it’s easier to make the function call directly after the if submitted: statement and not using that on_click kwarg. That way you make sure that you pass the most updated values from the other widgets in the form.
Not sure how csv.writer works but I’d expect that opening a file in writing mode (i.e., open('notes.csv', 'w')) will overwrite it every time, so you always end up with a one-line file. Probably the 'a+' mode to append to the file is what is needed here.
Demo:
Code:
import streamlit as st
import pandas as pd
def form_callback(data1, data2):
with open('notes.csv', 'a+') as f: #Append & read mode
f.write(f"{data1},{data2}\n")
with st.form(key="my_form",clear_on_submit=True):
st.write("Enter Note")
stock_ticker_input = st.text_input('Stock', key='ticker')
note_input = st.text_input('Note', key='note')
submitted = st.form_submit_button("Submit")
if submitted:
st.write("Note", note_input, "stock_ticker", stock_ticker_input)
form_callback(stock_ticker_input,note_input)
st.info(" #### Show contents of the CSV file :point_down:")
st.dataframe(pd.read_csv("notes.csv",names=["Stock","Note"]),height=300)
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.