Initialize with local file which is being passed to file_uploader()

Summary

I am following a YouTube video to build an app with Streamlit (here). The problem I face is more general I think: I want to pass a local path to the file_uploader method if no file has been uploaded yet by default and canā€™t wrap my head around it. Iā€™ve tried to search the forums but donā€™t know what term to look for exactly.

Steps to reproduce

Disclaimer: I donā€™t think the code snippet is important at all so feel free to skip to ā€œAdditional Informationā€ instead.

Code snippet:

import streamlit as st
import ifcopenshell
from PIL import Image

def callback_upload():
    st.session_state["is_file_uploaded"] = True
    st.session_state["array_buffer"] = st.session_state["uploaded_file"].getvalue() #gets content of ifc file
    st.session_state["ifc_file"] = ifcopenshell.file.from_string(st.session_state["uploaded_file"].getvalue().decode("utf-8"))


def main():      
    st.set_page_config(
        layout= "wide",
        page_title="Test",
    )
    
    st.markdown(
        """
        ### Test
        """
    )
    st.image(img)

    uploaded_file = st.sidebar.file_uploader("Chose file.", type=["ifc"], key="uploaded_file", on_change = callback_upload)
    print(type(uploaded_file))
#    callback_upload(uploaded_file)

    if "is_file_uploaded" in st.session_state and st.session_state["is_file_uploaded"]:
        st.sidebar.success("File uploaded successfully.")

if __name__ == "__main__":
    img = Image.open("./assets/dashboard.jpg").rotate(180)
    img = img.resize([int(img.size[0]*0.25), int(img.size[1]*0.25)])
    main()

Additional information

So my idea is that if no file has been uploaded yet, I want to pass the file to the file_uploader instead. That would give me the benefit of working with it in my other pages using the session_state. If this is a bad idea, please let me know how to do it instead/what the best practice would be.

My long term aim is to not allow any uploads at all and instead use a local file, however, on multiple pages. The user is supposed to be able to alter that file (file A) and save it (as file B). This is supposed to trigger a reload of the newly saved file (file B) which is being loaded instead of the initial file (file A) which should still be on the server.

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