mkhorasani / Streamlit-Authenticator

A secure authentication module to manage user access in a Streamlit application.
Other
1.66k stars 258 forks source link

streamlit.errors.StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, and st.form cannot be set using st.session_state. #21

Closed Anti-Apple4life closed 2 years ago

Anti-Apple4life commented 2 years ago

When I run my code, Streamlit throws an API exception. Here is the code that is concerns this problem:

import sqlite3

from CRM.main import crm, stauth
from projectBoard.toDo import *

connproj = sqlite3.connect('projectBoard/projectData.db')
cproj = connproj.cursor()
conncrm = sqlite3.connect('CRM/userData.db')
ccrm = conncrm.cursor()
st.session_state.update(st.session_state)
selectedPage = st.sidebar.selectbox("Page", ["Project Board", "CRM (unfinished)"])
loginbutton = st.sidebar.button("Log in")
if loginbutton:
    crm(conncrm, ccrm)
    selectedPage = "Log In"
elif selectedPage == "Project Board":
    projectboard(cproj, connproj)
import streamlit as st
import streamlit_authenticator as stauth

from CRM.setupdb import loaddb

def crm(conn, c):
    st.title("Log In")
    loaddb(conn, c)
    print(c.execute("SELECT email FROM userTable").fetchall())
    authenticator = stauth.Authenticate(c.execute("SELECT email FROM userTable").fetchall(), c.execute("SELECT username FROM userTable").fetchall(), c.execute("SELECT password FROM userTable").fetchall(), "CRM", "CRM")
    name, authentication_status = authenticator.login('Login', 'main')
mkhorasani commented 2 years ago

I am not sure exactly what is happening, but I suspect that it is cause by the line 'st.session_state.update(st.session_state)'. If you are trying to update a session state in Streamlit, it is best to do it as such: st.session_state['key'] = 'new value'.

LarryStewart2022 commented 1 year ago

@mkhorasani i get the same error when using the following

def clear_session():
    st.session_state['doctxt'] = ''
    st.session_state['DOCfiles'] =  []

raise

_StreamlitAPIException(SESSION_STATE_WRITES_NOT_ALLOWED_ERROR_TEXT) streamlit.errors.StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, st.data_editor, st.chat_input, and st.form cannot be set using st.sessionstate.

I'm new to streamlit, and I thought this would be an easy thing to do, as it is in most languages. I just need to clear everything out after a user is done processing so they can start a new entry. Is there a way to call a function in streamlit to reset or clear all the streamlit controls?