Writing to a csv

Hi Streamlit world!

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)

In the callback the state you’re using is the old state. You should use state for the keys, ‘note’ and ‘ticker’.

I end up with the correct state and dictionary of notes but I just am not able to write to the file at all and I’m not sure why

A couple of ideas:

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

  2. 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:
functionForm

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)