Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,23 @@
|
|
1 |
-
from pathlib import Path
|
2 |
import gradio as gr
|
3 |
import logging
|
|
|
4 |
|
5 |
# Configure logging
|
6 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
7 |
|
8 |
# Default directory to scan
|
9 |
-
DEFAULT_DATA_PATH = Path(
|
10 |
|
11 |
# Function to get file details and total storage usage
|
12 |
def get_storage(data_path: str):
|
13 |
# Convert to Path object
|
14 |
data_path = Path(data_path)
|
15 |
-
|
16 |
# Validate directory
|
17 |
if not data_path.exists() or not data_path.is_dir():
|
18 |
logging.error(f"Directory not found: {data_path}")
|
19 |
-
return [], "Error: Directory not found or inaccessible
|
20 |
-
|
21 |
# Collect file details and calculate total size
|
22 |
files = []
|
23 |
total_size = 0
|
@@ -33,11 +33,11 @@ def get_storage(data_path: str):
|
|
33 |
total_size += stats.st_size
|
34 |
except Exception as e:
|
35 |
logging.warning(f"Failed to process file: {file}. Error: {e}")
|
36 |
-
|
37 |
if not files:
|
38 |
logging.info(f"No files found in directory: {data_path}")
|
39 |
return [], "No files found in the specified directory."
|
40 |
-
|
41 |
# Total size in GB
|
42 |
usage = f"{total_size / (1024.0 ** 3):.3f} GB"
|
43 |
logging.info(f"Scanned {len(files)} files. Total size: {usage}")
|
@@ -48,7 +48,7 @@ with gr.Blocks() as app:
|
|
48 |
# Title and description
|
49 |
gr.Markdown("# π File Storage Viewer\n"
|
50 |
"Easily view files and calculate storage usage in a specified directory.")
|
51 |
-
|
52 |
with gr.Row():
|
53 |
# Input field for directory path
|
54 |
dir_input = gr.Textbox(
|
@@ -56,19 +56,18 @@ with gr.Blocks() as app:
|
|
56 |
label="Directory Path",
|
57 |
placeholder="Enter the directory path to scan.",
|
58 |
)
|
59 |
-
|
60 |
# Button to trigger file listing
|
61 |
fetch_btn = gr.Button("Fetch Files")
|
62 |
-
|
63 |
# Outputs: File list and total storage usage
|
64 |
with gr.Row():
|
65 |
file_table = gr.Dataframe(
|
66 |
headers=["Original Name", "Path", "Size (MB)"],
|
67 |
-
interactive=
|
68 |
label="Files",
|
69 |
)
|
70 |
storage_usage = gr.Textbox(label="Total Storage Usage", interactive=False)
|
71 |
-
|
72 |
# Click action to trigger the `get_storage` function
|
73 |
fetch_btn.click(
|
74 |
get_storage,
|
@@ -77,5 +76,6 @@ with gr.Blocks() as app:
|
|
77 |
show_progress=True
|
78 |
)
|
79 |
|
80 |
-
# Launch the Gradio app
|
81 |
-
app.launch(allowed_paths=[str(DEFAULT_DATA_PATH)], enable_queue=True)
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import logging
|
3 |
+
from pathlib import Path
|
4 |
|
5 |
# Configure logging
|
6 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
7 |
|
8 |
# Default directory to scan
|
9 |
+
DEFAULT_DATA_PATH = Path.home() # Use home directory as a dynamic default
|
10 |
|
11 |
# Function to get file details and total storage usage
|
12 |
def get_storage(data_path: str):
|
13 |
# Convert to Path object
|
14 |
data_path = Path(data_path)
|
15 |
+
|
16 |
# Validate directory
|
17 |
if not data_path.exists() or not data_path.is_dir():
|
18 |
logging.error(f"Directory not found: {data_path}")
|
19 |
+
return [], f"Error: Directory not found or inaccessible: {data_path}"
|
20 |
+
|
21 |
# Collect file details and calculate total size
|
22 |
files = []
|
23 |
total_size = 0
|
|
|
33 |
total_size += stats.st_size
|
34 |
except Exception as e:
|
35 |
logging.warning(f"Failed to process file: {file}. Error: {e}")
|
36 |
+
|
37 |
if not files:
|
38 |
logging.info(f"No files found in directory: {data_path}")
|
39 |
return [], "No files found in the specified directory."
|
40 |
+
|
41 |
# Total size in GB
|
42 |
usage = f"{total_size / (1024.0 ** 3):.3f} GB"
|
43 |
logging.info(f"Scanned {len(files)} files. Total size: {usage}")
|
|
|
48 |
# Title and description
|
49 |
gr.Markdown("# π File Storage Viewer\n"
|
50 |
"Easily view files and calculate storage usage in a specified directory.")
|
51 |
+
|
52 |
with gr.Row():
|
53 |
# Input field for directory path
|
54 |
dir_input = gr.Textbox(
|
|
|
56 |
label="Directory Path",
|
57 |
placeholder="Enter the directory path to scan.",
|
58 |
)
|
|
|
59 |
# Button to trigger file listing
|
60 |
fetch_btn = gr.Button("Fetch Files")
|
61 |
+
|
62 |
# Outputs: File list and total storage usage
|
63 |
with gr.Row():
|
64 |
file_table = gr.Dataframe(
|
65 |
headers=["Original Name", "Path", "Size (MB)"],
|
66 |
+
interactive=False,
|
67 |
label="Files",
|
68 |
)
|
69 |
storage_usage = gr.Textbox(label="Total Storage Usage", interactive=False)
|
70 |
+
|
71 |
# Click action to trigger the `get_storage` function
|
72 |
fetch_btn.click(
|
73 |
get_storage,
|
|
|
76 |
show_progress=True
|
77 |
)
|
78 |
|
79 |
+
# Launch the Gradio app
|
80 |
+
app.launch(allowed_paths=[str(DEFAULT_DATA_PATH)], enable_queue=True)
|
81 |
+
|