mirror of
https://github.com/langchain-ai/streamlit-agent.git
synced 2026-07-01 09:25:05 -04:00
ff03902ba4
This reverts commit 4a5e48b7bb.
33 lines
934 B
Python
33 lines
934 B
Python
import streamlit as st
|
|
|
|
|
|
# A hack to "clear" the previous result when submitting a new prompt. This avoids
|
|
# the "previous run's text is grayed-out but visible during rerun" Streamlit behavior.
|
|
class DirtyState:
|
|
NOT_DIRTY = "NOT_DIRTY"
|
|
DIRTY = "DIRTY"
|
|
UNHANDLED_SUBMIT = "UNHANDLED_SUBMIT"
|
|
|
|
|
|
def get_dirty_state() -> str:
|
|
return st.session_state.get("dirty_state", DirtyState.NOT_DIRTY)
|
|
|
|
|
|
def set_dirty_state(state: str) -> None:
|
|
st.session_state["dirty_state"] = state
|
|
|
|
|
|
def with_clear_container(submit_clicked: bool) -> bool:
|
|
if get_dirty_state() == DirtyState.DIRTY:
|
|
if submit_clicked:
|
|
set_dirty_state(DirtyState.UNHANDLED_SUBMIT)
|
|
st.experimental_rerun()
|
|
else:
|
|
set_dirty_state(DirtyState.NOT_DIRTY)
|
|
|
|
if submit_clicked or get_dirty_state() == DirtyState.UNHANDLED_SUBMIT:
|
|
set_dirty_state(DirtyState.DIRTY)
|
|
return True
|
|
|
|
return False
|