【问题标题】:unable to find the path for uploading a file using streamlit python code无法找到使用 streamlit python 代码上传文件的路径
【发布时间】:2022-11-27 15:18:05
【问题描述】:

我正在编写一个简单的 python 应用程序,用户从他们的本地文件管理器中选择一个文件并尝试使用 streamlit 上传

我能够成功获取用户使用 streamlit.uploader 提供的文件并将文件存储在 stramlit 应用程序文件夹的临时目录中,但问题是我无法提供存储在新创建目录中的文件的路径为了将应用程序发送到我的 gcp 云存储桶

在任何帮助下添加我的 sn-p 表示赞赏:)

def main():
    if file is not None:
        file_details = {"FileName":file.name,"FileType":file.type}
        st.write(file_details)
        with open(os.path.join("tempDir",file.name),"wb") as f: 
          f.write(file.getbuffer())         
        st.success("Saved File")
        print(cwd+file.name)
        file_dir= ("tempDir")
        
        
    def upload():
        file_name = os.path.join(file_dir, file.name)
        read_file(file_name)
        st.write (file_name)
        st.session_state["upload_state"] = "Saved successfully!"
        object_name_in_gcs_bucket = bucket.blob("gcp-bucket-destination-path"+ file.name)
        object_name_in_gcs_bucket.upload_from_filename(cwd+"/"+file.name)
       
       
    st.write("Youre uploading to bucket",bucketName)
    st.button("Upload file to GoogleCloud", on_click=upload)


if __name__ == "__main__":
    main()   

我尝试使用 cwd 命令导入文件的路径,也尝试了文件路径的 os 库,但没有任何效果

【问题讨论】:

    标签: python python-3.x api google-cloud-storage streamlit


    【解决方案1】:

    您的代码中有一些变量,我想您知道它们代表什么。试试这个,否则请确保将所有相关信息添加到问题和代码 sn-p 中。

    def main():
        file = st.file_uploader("Upload file")
        if file is not None:
            file_details = {"FileName":file.name,"FileType":file.type}
            st.write(file_details)
            
            file_path = os.path.join("tempDir/", file.name)
            with open(file_path,"wb") as f: 
              f.write(file.getbuffer())         
            st.success("Saved File")
    
            print(file_path)
    
    
        def upload():
            file_name = file_path
            read_file(file_name)
            st.write(file_name)
    
            st.session_state["upload_state"] = "Saved successfully!"
            object_name_in_gcs_bucket = bucket.blob("gcp-bucket-destination-path"+ file.name)
            object_name_in_gcs_bucket.upload_from_filename(file_path)
            
        st.write("Youre uploading to bucket", bucketName)
        st.button("Upload file to GoogleCloud", on_click=upload)
    
    
    if __name__ == "__main__":
        main()  
    

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2019-04-18
      • 1970-01-01
      相关资源
      最近更新 更多