Save date when rerunning whole script

i am new to Pyhton and Streamlit so i would be greatfull for a push in the right direction.
Also i might lack vocabulary so i try to explain what i am looking for.

I got a Pyhtonscript with Streamlit running that reads multiple CSV Files that are saved in DataFrames.
Then the user can select with a dropdown selection one entry and with a datepicker a second.
Calculations are done according to the selection.

Now i want to save the data with calculations in a dataFrame so i can at the end of the session save it to CSV (Basicaly a log file of all selections with calculation results)

for this i made a button. When i push this button the selection is appended to the DataFrame.

However everytime the selection changes, the whole script reruns, essentially wiping all previous data.
How can i make permanent variables.
Or can you just make sections of the script that shoudl rerun?

kind regards

data procing should finished by python script, the result will not change again after previous action is done.
you can see the result through the AgGrid
for example:

import streamlit as st
from st_aggrid import AgGrid
import pandas as pd

import base64
from io import BytesIO
def to_excel(df):
output = BytesIO()
writer = pd.ExcelWriter(output, engine=‘xlsxwriter’)
df.to_excel(writer, sheet_name=‘Sheet1’)
processed_data = output.getvalue()
return processed_data
def get_table_download_link(df):
“”“Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframeout: href string“””

val = to_excel(df)
b64 = base64.b64encode(val) # val looks like b’…’
return f’Download csv file’ # decode b’abc’ => abc

st.markdown(get_table_download_link(df), unsafe_allow_html=True)

hmm, im not sure i completely understand this.

you write everything to a xls and then read it again.

this could work, but i dont want to write to disk yet. i want to make maybe a couple selections, store them in a Dataframe and then push a button → run some code, check for duplicates, and then Save to CSV

this is a section of my code where i would to “store” the data from abfrage in log

 #dictionary with data from selectionbox
    ab = {"K-Nummer": [knummer], "Date": [date_near],"Basis": [lme_basis],"LME": [lme_option], "MTZ": [mtz_option]}

log = pd.DataFrame() #initialize logfile for this session
abfrage = pd.DataFrame(ab) #generate the entry from selection after using st.selectbox,...

if st.button('Speichere Daten'):    
    log = log.append(abfrage)     #if button is pushed append to log
    st.write('nicht gespeichert')