File size: 947 Bytes
7b9203f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import streamlit as st

HAS_CONFIRMED = "SAFELY_UPDATE"


def handle_on_click(on_click):
    """Handles on_click by waiting for the confirmation."""
    if st.session_state.get(HAS_CONFIRMED):
        return on_click
    else:

        def toggle_has_confirmed(*args, **kwargs):
            del args, kwargs  # unused.
            st.session_state[HAS_CONFIRMED] = not st.session_state.get(HAS_CONFIRMED)

        return toggle_has_confirmed


def button_with_confirmation(
    label: str,
    key: str = None,
    on_click=None,
    args=None,
    kwargs=None,
):
    """Implements a safe button that asks for confirmation before executing on_click."""
    st.button(
        label,
        on_click=handle_on_click(on_click),
        args=args,
        kwargs=kwargs,
        key=key,
        type="secondary",
    )
    if st.session_state.get(HAS_CONFIRMED):
        st.error(f"Do you really want to {label.lower()}? Click again to confirm.")