StreamlitAPIException: st.session_state.user_question невозможно изменить после создания экземпляра виджета с ключом usePython

Программы на Python
Ответить
Anonymous
 StreamlitAPIException: st.session_state.user_question невозможно изменить после создания экземпляра виджета с ключом use

Сообщение Anonymous »

Я хотел очищать поле ввода при отправке вопроса, но получил следующую ошибку:
StreamlitAPIException: st.session_state.user_question cannot be modified after the widget with key user_question is instantiated

.....
def main():
st.set_page_config(page_title="ChatBot")
st.header("ChatBot")

# Use a form for user input
with st.form(key='user_input_form'):
user_question = st.text_input("Ask a Question", key="temp_user_question")
submit_button = st.form_submit_button(label='Submit')

if submit_button and st.session_state["temp_user_question"]:
user_input(st.session_state["temp_user_question"])
st.session_state["temp_user_question"] = '' # Clear the input field after submission

Другой подход:
def main():
st.set_page_config(page_title="ChatBot")
st.header("ChatBot")

# Use a form for user input
with st.form(key='user_input_form'):
user_question = st.text_input("Ask a Question", key="temp_user_question")
submit_button = st.form_submit_button(label='Submit')

if submit_button and st.session_state["temp_user_question"]:
user_input(st.session_state["temp_user_question"])
st.session_state["temp_user_question"] = None # Clear the input field after submission


Подробнее здесь: https://stackoverflow.com/questions/786 ... ed-after-t
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»