Adding new row to editable tables

Hey ,
I’m trying to present every time sorted data and then allow the user to add it and add a new row and then save it into dataframe.

The changing is working but when I add a new row and then want to change it , it does’nt appear .

This is my code :

@st.cache_data
def extract_data(query):
conn = prestodb.dbapi.connect(
host=‘prestointeractive.us-east-1.aws.scene53il.com’,
port=8889,
user=‘audience_api’,
catalog=‘hive’,
schema=‘default’,
)
cursor = conn.cursor()
query_string = query
cursor.execute(query_string)
query_results = cursor.fetchall()
column_names = [part[0] for part in cursor.description]
df = pd.DataFrame(query_results, columns=column_names)
return df

def load_and_edit_dataframe():

budget_data = f'''
            select case WHEN app_name = 'popslot' then 'popslots' else app_name END as app_name,
 date_trunc('month',cast(b."month" as date)) as target_month,
-- insertion_time,
platform,
channel_type,
lower(channel) channel,
      sum(cast(b.daily as decimal) ) AS daily_target,
      SUM (cast(b.monthly as decimal)) as monthly_target
from postgresql.prod_global_ua.ua_budgets_v b 
WHERE daily != ''    and  monthly  != '' and b."month" != '' and b.channel != '' and b.platform != '' and b.channel_type != ''
     AND cast(substr(b."month",1,4) as integer) > 2022
     --and app_name not like '%tetris2%' 
     --AND channel = 'adaction' AND platform = 'Android'
group by app_name, 
         insertion_time,
         platform,
         channel_type,
         channel,
         date_trunc('month',cast(b."month" as date)) '''

budget_data = extract_data(budget_data)

df = pd.DataFrame(budget_data)
df = pd.DataFrame(df, columns=['app_name','target_month','platform','channel_type','channel','daily_target','monthly_target'])
return df

Create a Streamlit app

st.title(“Budget Data Editor”)

budget = load_and_edit_dataframe()

App = [“popslots”, “mgms”, “bingo”, “bgpoker”, “myvegas”, “konami”, “tetris”, “tetris3”, “brn_solitaire”, “brn_spider”,
“tetris_blocks”]
Month = [“2023-01-01”, “2023-02-01”, “2023-03-01”, “2023-04-01”, “2023-05-01”, “2023-06-01”, “2023-07-01”, “2023-08-01”,
“2023-09-01”, “2023-10-01”, “2023-11-01”, “2023-12-01”,
“2024-01-01”, “2024-02-01”, “2024-03-01”, “2024-04-01”, “2024-05-01”, “2024-06-01”, “2024-07-01”, “2024-08-01”,
“2024-09-01”, “2024-10-01”, “2024-11-01”, “2024-12-01”
]
Type = [“A-UA”, “B-RT”]
channel_type = [“CPE”, “Direct Deal”, “DSP”, “Native”, “Preload”, “Retargeting”, “Search”]
channel = [“kashKick”, “prodege”, “blindferret”, “bluestacks”, “mistplay”, “bigabid”, “moloco”, “taboola”, “aura”,
“digital”, “kayzen”, “tiktok”, “youAppi”, “applesearchads”, “google_ads”, “snapchat”, “liftoff”,“tyrrwards”,“zorka”,“persona.ly”,“applovin”,]
platform = [“Android”, “iOS”]

filter_app = st.selectbox(“App name:”, options=App)
filter_month = st.selectbox(“Month:”, options=Month)
sort_df = budget[budget[‘app_name’] == filter_app]
sort_df = sort_df[sort_df[‘target_month’] == filter_month]

editable_df = sort_df

st.session_state.persistent_df = editable_df

edited_data = {}

column_config = {
‘app_name’ : st.column_config.SelectboxColumn(‘App name’, options=App ,required=True),
‘target_month’ : st.column_config.SelectboxColumn(‘Target month’, options=Month,required=True),
‘platform’ : st.column_config.SelectboxColumn(‘Platform’, options=platform,required=True),
‘channel_type’ : st.column_config.SelectboxColumn(‘Channel type’, options=channel_type,required=True),
‘channel’ : st.column_config.SelectboxColumn(‘Channel’, options=channel,required=True),
‘daily_target’ : st.column_config.NumberColumn(‘Daily target’,min_value=0, format=“$%d”),
‘monthly_target’ : st.column_config.NumberColumn(‘Monthly target’,min_value=0, format=“$%d”),

}

#filter_text = st.selectbox(“App name:”, editable_df[“app_name”].unique())

#if filter_text in edited_data:

filtered_df = edited_data[filter_text]

#else:

filtered_df = editable_df[editable_df[‘app_name’] == filter_text]

def make_table(editable_df,column_config):

edited_df = st.data_editor(editable_df, column_config=column_config,
                       key='data_editor',num_rows="dynamic",hide_index=True)
return edited_df

edited_df = make_table(editable_df,column_config)

if st.button(“Save Changes”):
st.dataframe(edited_df)
print(edited_df)
#st.write(edited_df)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.