Determine if currently executing within the scope of a st.fragment

How does one tell if one is currently executing within a st.fragment? Is there an easy function or variable that dictates this?

I want to use a single function that will call st.rerun(scope=app') if used outside of a fragment and call st.rerun(scope='fragment') when operating inside of a fragment without having to pass another parameter to the function.

Seems like a good way is to do. Don’t know if this the recommended way that people should be doing this though.

from streamlit.runtime.scriptrunner_utils.script_run_context import get_script_run_ctx

def inside_fragment():
    return bool(get_script_run_ctx().current_fragment_id)

#Outside fragment
st.write(inside_fragment()) # False

@st.fragment()
def frag():
    st.write(inside_fragment()) # True

frag()