I have a piece of code that let’s a user select a file to upload, and then it carries on processing.
Is there a way to revoke this, and I upload the file myself when the application begins, through some sort of initialization function?
This is for a Streamlit page. When a webpage is refreshed by the user, i’m assuming the app.py file gets re-run? If so, the ‘initialization function’ gets re-run too correct, unless there’s a way to stop this. This is the code i run currently:
def main():
if source == "PDF":
pdffile = st.file_uploader("Upload here:", type="pdf", accept_multiple_files=True)
if st.button("Spinner"):
with st.spinner("Processing.."):
raw_text = get_pdf_text(pdffile)
I need this to run once, or just eliminate the idea that the user has any selection. I’d like to just load one or multiple files upfront to be worked with in the rest of the code
Here’s how you might structure your app to automatically process files from a specific directory at startup:
import streamlit as st
import os
def get_pdf_text(files):
return "Processed text from PDF"
def main():
files_directory = 'predefined_files'
if 'initialized' not in st.session_state or not st.session_state.initialized:
file_paths = [os.path.join(files_directory, f) for f in os.listdir(files_directory) if f.endswith('.pdf')]
pdffiles = [] # This would be a list of file paths or file objects
for file_path in file_paths:
pdffiles.append(file_path) # Adjust this part to open the file if needed for processing
raw_text = get_pdf_text(pdffiles)
st.session_state.initialized = True # Mark initialization as done to avoid re-running
st.write(raw_text)
else:
st.write("Initialization already done.")
if __name__ == "__main__":
main()
Hope this helps!
Kind Regards,
Sahir Maharaj
Data Scientist | AI Engineer