How to get text to replace on a timer

The battery function:

# Start and stop streaming functions
        def get_battery():
                
            battery0 = senTSS.getBatteryPercentageRemaining(logicalID=0)
            battery1 = senTSS.getBatteryPercentageRemaining(logicalID=1)

            #print("Zero:"+ str(battery0))
            #print("One:"+ str(battery1))

            if( battery0 != -1 and battery1 != -1):
                with st.empty():
                    bat_percent0 = battery0[-1]
                #print("Battery0:"+ str(bat_percent0))
                    st.write("Battery Percentage of Sensor 00:")
                    st.write(f"{bat_percent0}")
                    bat_percent1 = battery1[-1]
                #print("Battery1:"+ str(bat_percent1))
                    st.write("Battery Percentage of Sensor 01:")
                    st.write(f"{bat_percent1}")

calling the battery function in the code:

                    if senTSS:
                        senTSS.comClass.sensor.reset_input_buffer()
                        for id in LOGICAL_IDS:
                            senTSS.startStreaming(logicalID=id)

                        last_battery_check_time = time.time() 
                        last_time_check = time.time() 
                        # Collect data in the background
                        while st.session_state.data_collection_active:
                            record_data(senTSS)
                            battery_current_time = time.time()
                            time_clock_time = time.time()
                            if battery_current_time - last_battery_check_time >= 5:  # 60 seconds
                                get_battery()  # Call get_battery function
                                last_battery_check_time = battery_current_time  # Update last check time
                            if time_clock_time - last_time_check >=21600: #stops the data collection automatically after 6 hours 21600
                               print("Got to the timer")
                               stop_recording()  
                        sleep(0.01)  # Adjust this based on your desired sample rate

Currently with the st.empty function, it is not replacing the value, but continually adding a line

Is there a way to replace the value without adding lines

Check the run_every argument in st.fragment:

Using this st.fragment, the examples show the press of buttons doing different things without rerunning the whole program, how would one write a statement to the screen using the st.fragment function? If I want the program to run, with the exception of the of the battery display being continuous, but just being replaced on the screen.

The simplest example I can think of right now is setting a st.fragment that runs every so often while exhausting a generator. That could be the battery indicator control from your example.

run_every

Code:
import streamlit as st
from datetime import datetime


def battery_depleater():
    battery_level = 10
    while battery_level > 0:
        yield battery_level
        battery_level -= 1


@st.fragment(run_every=0.5)
def check_battery():
    ctn = st.empty()
    with ctn.container():
        st.write(f"Time is {datetime.now().strftime('%H:%M:%S')}")
        try:
            battery_level = next(st.session_state.battery)
            st.metric("Battery Level", battery_level)
        except StopIteration:
            st.write("Battery depleated :skull:")


def main():
    if "battery" not in st.session_state:
        st.session_state["battery"] = battery_depleater()

    st.title("`run_every` example")
    lcol, rcol = st.columns(2)

    with lcol:
        st.subheader("This column does not change", divider=True)
        st.write(f"Time was {datetime.now().strftime('%H:%M:%S')}")
        st.image(
            "https://placecats.com/millie_neo/600/200", use_container_width=True, caption="A cat"
        )

    with rcol:
        st.subheader("This column auto-updates every 0.5s", divider="rainbow")
        check_battery()


if __name__ == "__main__":
    main()