Default value for a column as timestamp inside st.form

Hi,
I’m implementing an app with a st.form that allows users to update data of a given table in Snowflake. Once a new row is created, the timestamp of that moment must be filled in a audit column.
The source table has the column casted as TIMESTAMP_NTZ(9), where 9 is the precision (I don’t need it to be so precise, just hh:mm:ss is fine).

In the script bellow I try do update a df and the behavior is the same: I click submit and the new row simply disappears. Can you help me finding a solution for this?

# Import python packages
import streamlit as st
import pandas as pd
from datetime import datetime

now = datetime.now()

df = pd.DataFrame(
    [
       {"col1": "A", "tmstmp": now},
       {"col1": "B", "tmstmp": now},
       {"col1": "C", "tmstmp": now},
   ]
)

with st.form("DF"):
    df = st.data_editor(df,num_rows="dynamic",column_config={
        "col1": st.column_config.TextColumn(
            "col1",
            default='Z'
         ),
        "tmstmp": st.column_config.DatetimeColumn(
            "tmstmp",
            default=now,
            disabled=True
         )
    })
    submit_button = st.form_submit_button("Submit")

if submit_button:
    st.success("Success")

I think the issue comes from the now = datetime.now() line. The df and the st.column_config object are regenerated at each app rerun because now is recalculated. Setting now to some constant value, e.g., now = datetime(2025, 1, 1, 6, 30, 0), would fix the problem, though that beats the purpose of the timestamp.

1 Like

Using st.time_input fixes the problem, but it creates a input box in the interface. Is there an transparent alternative for the user?

now = st.time_input(label='Loggedin Time', value='now', disabled=True)