St.form_submit_button to update Postgres database

Started using Streamlit last week, instant fan. The app I’ve developed is very simple. I am collaborating with a group of scientists (app users) and need to have a simple platform for them to perform quality assessment (QA) review on some data. I use st.form that has some select boxes that lets users select a measurement from the project database for review. Once these options are selected, I have a st.form_submit_button that triggers the app to go get the data from the database based on the user input. It then writes out the data and makes a couple charts for them to look at, and then 2 drop down menus and a text box are provided to allow them to provide their input. A second st.form_submit_button is used to commit their input to the database.

Here’s the issue. This works perfectly in local development using an SQLite database. I updated the app to point to a Postgres database rather than my local test SQLite database. Everything still works perfectly except the second st.form_submit_button. It behaves totally differently now and does not commit anything to the database. Just to check, I put the database commit before the submit button and it commits, but only commits blanks (as if no input). For example, when put before the second submit button this successfully commits blank text ("") to the Postgres database even when there is user input:

CUR.execute("UPDATE table SET euid = ‘userinput1’ WHERE hashid = ‘userinput2’)
DBCON.commit()

While this does not commit anything to the Postgres database, clears the form (with clear_on_submit = False), and starts over without doing anything or raising any errors:

commitbutton = st.form_submit_button(label = ‘Click here to commit changes to database’)
if commitbutton:
CUR.execute("UPDATE table SET euid = ‘userinput1’ WHERE hashid = ‘userinput2’)
DBCON.commit()

Only the second st.form_submit_button is in question here. I haven’t changed anything else about the app except the database that it points to.

Have you had any luck figuring this out as I have exactly the same issue.

I have the following form code and writing to a SQLite db:

def FaddCase():

with st.form(key = "addData"):

    FcaseName = st.text_input ("Case Ref...")

    Fcris = st.text_input ("CRIS Number...")

    FclientName = st.text_input ("Client Name...")

    Ftrt = st.text_input ("TRT...")

    FagreedHours = st.number_input ("Agreed Hours...")

    FactualHours = st.number_input ("Actual Hours...")

    FcostCode = st.text_input ("Cost Code...")

    FoffType = st.text_input ("Offence Type...")

    Fstatus = st.text_input ("Status...")

    Foic = st.text_input ("OIC...")

    _returnedData = [FcaseName, Fcris, FclientName, Ftrt, FagreedHours, FactualHours, FcostCode, FoffType, Fstatus, Foic]

    curs.execute("INSERT INTO Tcases VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", _returnedData)

    conn.commit()

    _submitForm = st.form_submit_button("Commit to Database")

It merely puts in a blank record to the db :frowning:
Any help/update would be appreciated.
Thanks.

I am trying to update sqlite which is embedded database of python. Post submit button values are not going into insert statement in my case also.

I tried printing the values of each input field, no output was observed, however I used st.info(body) to check the values where I could see the values of fields entered in the form. However all the values were showing underlined, type also did not reveal the details.

There is something which is missing here which has not been documented and/or demonstrated by the streamlit team.

In this case I am fairly certain the curs.execute and commit need to be below the form_submit_button. Running the code the way you have it clears the app/form and then puts the list with empty values into the database. I use an if condition, e.g.,

if _submitForm:
curs.execute(…)
conn.commit(…)

This way these get triggered and run before the app refreshes itself on submit.

Hi. Without looking into your code, I don’t know if I can offer specific help. However, I started developing a GitHub page that would demo basic CRUD operations in streamlit. That is linked below (assumes you have a database set up, but the db.py code can create one for you). I want to add more to that repository, but the current app.py code shows a basic structure for do a select and update. One must be careful. It is tricky to get Streamlit working correctly with a database, but it can work nicely.

https://github.com/makerofmaps1/Streamlit

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.