Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ def format_size(size_bytes):
|
|
| 18 |
if size_bytes < 1024.0:
|
| 19 |
return f"{size_bytes:.2f} {unit}"
|
| 20 |
size_bytes /= 1024.0
|
| 21 |
-
return f"{size_bytes:.2f} PB"
|
| 22 |
|
| 23 |
def list_files(sort_by="Date (Newest)"):
|
| 24 |
"""Fetch files from the bucket, extract accurate metadata, and sort."""
|
|
@@ -28,7 +28,6 @@ def list_files(sort_by="Date (Newest)"):
|
|
| 28 |
|
| 29 |
data = []
|
| 30 |
for f in files:
|
| 31 |
-
# Skip directories
|
| 32 |
if f.get("type") == "directory":
|
| 33 |
continue
|
| 34 |
|
|
@@ -37,17 +36,21 @@ def list_files(sort_by="Date (Newest)"):
|
|
| 37 |
size_str = format_size(raw_size)
|
| 38 |
|
| 39 |
# --- AGGRESSIVE DATE EXTRACTION ---
|
| 40 |
-
# Different storage backends (like S3/Xet) use different dictionary keys.
|
| 41 |
-
# We check all possible variations to ensure we catch the Upload Date.
|
| 42 |
raw_date = None
|
| 43 |
-
date_keys = ["last_modified", "LastModified", "updated", "updatedAt", "client_modified", "created_at"]
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
for k in date_keys:
|
| 46 |
if k in f and f[k] is not None:
|
| 47 |
raw_date = f[k]
|
| 48 |
break
|
| 49 |
|
| 50 |
-
# If missing,
|
| 51 |
if raw_date is None:
|
| 52 |
try:
|
| 53 |
info = fs.info(f["name"])
|
|
@@ -67,15 +70,14 @@ def list_files(sort_by="Date (Newest)"):
|
|
| 67 |
|
| 68 |
df = pd.DataFrame(data)
|
| 69 |
if not df.empty:
|
| 70 |
-
# Safely parse dates
|
| 71 |
-
dt_col = pd.to_datetime(df["_raw_date"], errors='coerce').dt.tz_localize(None)
|
| 72 |
df["_raw_date"] = dt_col
|
| 73 |
|
| 74 |
# Format display column, filling NaT back to "Unknown"
|
| 75 |
-
df["
|
| 76 |
|
| 77 |
# --- EXPLICIT SORTING ---
|
| 78 |
-
# We sort against the raw hidden values, NOT the formatted strings
|
| 79 |
if sort_by == "Name (A-Z)":
|
| 80 |
df = df.sort_values(by="File Name", ascending=True)
|
| 81 |
elif sort_by == "Name (Z-A)":
|
|
@@ -91,14 +93,14 @@ def list_files(sort_by="Date (Newest)"):
|
|
| 91 |
|
| 92 |
# Clean up raw columns and enforce column order
|
| 93 |
df = df.drop(columns=["_raw_date", "_raw_size"])
|
| 94 |
-
df = df[["File Name", "
|
| 95 |
|
| 96 |
file_names = df["File Name"].tolist() if not df.empty else []
|
| 97 |
return df, gr.update(choices=file_names)
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
print(f"Error fetching files: {e}")
|
| 101 |
-
return pd.DataFrame(columns=["File Name", "
|
| 102 |
|
| 103 |
def download_file(filename):
|
| 104 |
"""Download the selected file from the bucket to a temporary local file."""
|
|
@@ -137,9 +139,9 @@ with gr.Blocks() as app:
|
|
| 137 |
interactive=True
|
| 138 |
)
|
| 139 |
|
| 140 |
-
# Interactive table
|
| 141 |
file_table = gr.Dataframe(
|
| 142 |
-
headers=["File Name", "
|
| 143 |
interactive=False,
|
| 144 |
label="Bucket Contents"
|
| 145 |
)
|
|
@@ -156,11 +158,8 @@ with gr.Blocks() as app:
|
|
| 156 |
file_output = gr.File(label="Ready for Download")
|
| 157 |
|
| 158 |
# --- EVENT TRIGGERS ---
|
| 159 |
-
# Trigger refresh
|
| 160 |
refresh_btn.click(fn=list_files, inputs=[sort_dropdown], outputs=[file_table, file_dropdown])
|
| 161 |
-
# Trigger sort instantly when dropdown changes
|
| 162 |
sort_dropdown.change(fn=list_files, inputs=[sort_dropdown], outputs=[file_table, file_dropdown])
|
| 163 |
-
# Load data automatically on startup
|
| 164 |
app.load(fn=list_files, inputs=[sort_dropdown], outputs=[file_table, file_dropdown])
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
|
|
|
| 18 |
if size_bytes < 1024.0:
|
| 19 |
return f"{size_bytes:.2f} {unit}"
|
| 20 |
size_bytes /= 1024.0
|
| 21 |
+
return f"{size_bytes:.2f} PB"
|
| 22 |
|
| 23 |
def list_files(sort_by="Date (Newest)"):
|
| 24 |
"""Fetch files from the bucket, extract accurate metadata, and sort."""
|
|
|
|
| 28 |
|
| 29 |
data = []
|
| 30 |
for f in files:
|
|
|
|
| 31 |
if f.get("type") == "directory":
|
| 32 |
continue
|
| 33 |
|
|
|
|
| 36 |
size_str = format_size(raw_size)
|
| 37 |
|
| 38 |
# --- AGGRESSIVE DATE EXTRACTION ---
|
|
|
|
|
|
|
| 39 |
raw_date = None
|
|
|
|
| 40 |
|
| 41 |
+
# Prioritizing CREATED date keys as requested, falling back to modified keys
|
| 42 |
+
date_keys = [
|
| 43 |
+
"created", "created_at", "CreatedAt", "CreationDate", "ctime", "date_created",
|
| 44 |
+
"last_modified", "LastModified", "updated", "updatedAt", "client_modified"
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
# 1. Check the primary file metadata dictionary
|
| 48 |
for k in date_keys:
|
| 49 |
if k in f and f[k] is not None:
|
| 50 |
raw_date = f[k]
|
| 51 |
break
|
| 52 |
|
| 53 |
+
# 2. If still missing, ping info() directly for deeper hidden keys
|
| 54 |
if raw_date is None:
|
| 55 |
try:
|
| 56 |
info = fs.info(f["name"])
|
|
|
|
| 70 |
|
| 71 |
df = pd.DataFrame(data)
|
| 72 |
if not df.empty:
|
| 73 |
+
# Safely parse dates. utc=True prevents timezone conflicts from turning valid dates into "Unknown" (NaT)
|
| 74 |
+
dt_col = pd.to_datetime(df["_raw_date"], errors='coerce', utc=True).dt.tz_localize(None)
|
| 75 |
df["_raw_date"] = dt_col
|
| 76 |
|
| 77 |
# Format display column, filling NaT back to "Unknown"
|
| 78 |
+
df["Date Created"] = dt_col.dt.strftime('%Y-%m-%d %H:%M:%S').fillna("Unknown")
|
| 79 |
|
| 80 |
# --- EXPLICIT SORTING ---
|
|
|
|
| 81 |
if sort_by == "Name (A-Z)":
|
| 82 |
df = df.sort_values(by="File Name", ascending=True)
|
| 83 |
elif sort_by == "Name (Z-A)":
|
|
|
|
| 93 |
|
| 94 |
# Clean up raw columns and enforce column order
|
| 95 |
df = df.drop(columns=["_raw_date", "_raw_size"])
|
| 96 |
+
df = df[["File Name", "Date Created", "Size"]]
|
| 97 |
|
| 98 |
file_names = df["File Name"].tolist() if not df.empty else []
|
| 99 |
return df, gr.update(choices=file_names)
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
print(f"Error fetching files: {e}")
|
| 103 |
+
return pd.DataFrame(columns=["File Name", "Date Created", "Size"]), gr.update(choices=[])
|
| 104 |
|
| 105 |
def download_file(filename):
|
| 106 |
"""Download the selected file from the bucket to a temporary local file."""
|
|
|
|
| 139 |
interactive=True
|
| 140 |
)
|
| 141 |
|
| 142 |
+
# Interactive table (Headers updated)
|
| 143 |
file_table = gr.Dataframe(
|
| 144 |
+
headers=["File Name", "Date Created", "Size"],
|
| 145 |
interactive=False,
|
| 146 |
label="Bucket Contents"
|
| 147 |
)
|
|
|
|
| 158 |
file_output = gr.File(label="Ready for Download")
|
| 159 |
|
| 160 |
# --- EVENT TRIGGERS ---
|
|
|
|
| 161 |
refresh_btn.click(fn=list_files, inputs=[sort_dropdown], outputs=[file_table, file_dropdown])
|
|
|
|
| 162 |
sort_dropdown.change(fn=list_files, inputs=[sort_dropdown], outputs=[file_table, file_dropdown])
|
|
|
|
| 163 |
app.load(fn=list_files, inputs=[sort_dropdown], outputs=[file_table, file_dropdown])
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|