|
import streamlit as st |
|
|
|
|
|
|
|
|
|
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.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 |
|
|