Guys,
I’m trying to use STqdm to display real-time file uploading progress, but my code always doesn’t work, could any one help me? My purpose is to use st.file_uploader to upload file from local disk to Google Cloud Storage, here’s my current code which referred from this topic:
import streamlit as st
from google.cloud import storage
import tempfile
import time
from stqdm import stqdm
KB = float(1024)
uploaded_file = st.file_uploader("choose file", type=("mp4", "jpg", "png","gif"))
def hook(t):
"""
params:
t (tqdm): Accepts tqdm class
"""
def inner(bytes_amount):
bytes_amount = bytes_amount / KB
t.update(bytes_amount)
return inner
def upload_to_gcs(upload_obj):
"""upload you file to Google Cloud Storage"""
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(uploaded_file.read())
temp_file_name = temp_file.name
bucket_name = "my-bucket-name"
source_file_name = uploaded_file.name
destination_blob_name = uploaded_file.name
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
with stqdm(total=upload_obj.size / KB,
unit='kB',
unit_scale=True,
unit_divisor=KB,
desc=upload_obj.name,
leave=True) as t:
upload = blob.upload_from_file(
io.BytesIO(upload_obj.getvalue()),
size=upload_obj.size,
client=storage_client,
callback=hook(t)
)
#remove temp file
os.remove(temp_file_name)
return f"gs://{bucket_name}/{destination_blob_name}"
with st.form("mysidebarform"):
submitted = st.form_submit_button("upload")
if submitted:
gs_uri = upload_to_gcs(uploaded_file)
st.success(f"The file has been uploaded into {gs_uri}")
Thank you very much!