awacke1 commited on
Commit
5168b60
·
verified ·
1 Parent(s): 9b9638d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +194 -0
app.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import pandas as pd
4
+ import plotly.graph_objects as go
5
+ import asyncio
6
+ from datetime import datetime
7
+ import re
8
+ import pathlib
9
+
10
+ # --- Configuration ---
11
+ # Update frequency: how many files to process before updating the UI
12
+ UPDATE_INTERVAL = 250
13
+
14
+ def parse_filename_words(filename):
15
+ """
16
+ Extracts contiguous groups of letters from a filename, ignoring numbers and symbols.
17
+ Example: "Aarons123File-482.md" -> "Aarons, File, md"
18
+ """
19
+ # Find all sequences of letters
20
+ words = re.findall('[A-Za-z]+', filename)
21
+ return ", ".join(words) if words else "N/A"
22
+
23
+ def get_file_info(path, root_path):
24
+ """
25
+ Gathers required information for a single file.
26
+ Returns a dictionary or None if path is not a file or is inaccessible.
27
+ """
28
+ try:
29
+ if not os.path.isfile(path):
30
+ return None
31
+
32
+ stat = os.stat(path)
33
+ size = stat.st_size
34
+
35
+ # Skip empty files
36
+ if size == 0:
37
+ return None
38
+
39
+ # Determine the top-level directory for color grouping
40
+ try:
41
+ relative_path = os.path.relpath(path, root_path)
42
+ top_level_dir = relative_path.split(os.sep)[0]
43
+ except ValueError:
44
+ top_level_dir = os.path.basename(root_path)
45
+
46
+ # Get parent directory relative to the root for treemap structure
47
+ parent_path = str(pathlib.Path(*pathlib.Path(relative_path).parts[:-1]))
48
+ if parent_path == ".":
49
+ parent_path = top_level_dir
50
+
51
+ return {
52
+ 'path': path,
53
+ 'label': os.path.basename(path),
54
+ 'parent': parent_path,
55
+ 'size': size,
56
+ 'color_group': top_level_dir,
57
+ 'created': datetime.fromtimestamp(stat.st_ctime).strftime('%Y-%m-%d %H:%M:%S'),
58
+ 'modified': datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S'),
59
+ 'keywords': parse_filename_words(os.path.basename(path))
60
+ }
61
+ except (OSError, FileNotFoundError):
62
+ return None
63
+
64
+ def create_treemap_figure(df):
65
+ """
66
+ Generates the Plotly treemap figure from a DataFrame of file info.
67
+ """
68
+ if df.empty:
69
+ return go.Figure(go.Treemap(
70
+ labels=["Your scan will appear here."],
71
+ parents=[""],
72
+ values=[1]
73
+ ))
74
+
75
+ # Ensure the root of the treemap is visible
76
+ root_label = os.path.basename(df.iloc[0]['path'])
77
+
78
+ fig = go.Figure(go.Treemap(
79
+ ids=df['path'],
80
+ labels=df['label'],
81
+ parents=df['parent'],
82
+ values=df['size'],
83
+ marker_colors=df['color_group'], # Color by top-level folder
84
+ tiling_method='squarified', # Use the squarified algorithm
85
+ root_label=root_label,
86
+ customdata=df[['size', 'modified', 'created', 'keywords']],
87
+ hovertemplate=(
88
+ "<b>%{label}</b><br>"
89
+ "Size: %{customdata[0]:.2s}B<br>"
90
+ "Modified: %{customdata[1]}<br>"
91
+ "Created: %{customdata[2]}<br>"
92
+ "Keywords: %{customdata[3]}<br>"
93
+ "Path: %{id}<extra></extra>"
94
+ ),
95
+ pathbar={'visible': True} # Show breadcrumb trail
96
+ ))
97
+
98
+ fig.update_layout(
99
+ margin=dict(t=50, l=25, r=25, b=25),
100
+ title="File System Treemap"
101
+ )
102
+ return fig
103
+
104
+ async def scan_directory(directory, stop_flag_state, progress=gr.Progress(track_ τότε=True)):
105
+ """
106
+ Asynchronously scans a directory, yielding updates to the UI.
107
+ """
108
+ if not directory or not os.path.isdir(directory):
109
+ yield create_treemap_figure(pd.DataFrame()), "Invalid directory path.", pd.DataFrame()
110
+ return
111
+
112
+ file_list = []
113
+ processed_count = 0
114
+
115
+ # Reset stop flag at the beginning of a new scan
116
+ stop_flag_state['stop'] = False
117
+
118
+ # Create a DataFrame to hold results
119
+ df = pd.DataFrame()
120
+
121
+ progress(0, desc="Starting scan...")
122
+
123
+ for root, _, files in os.walk(directory, topdown=True):
124
+ if stop_flag_state['stop']:
125
+ progress(1.0, "Scan stopped by user.")
126
+ break
127
+
128
+ for name in files:
129
+ file_path = os.path.join(root, name)
130
+ info = get_file_info(file_path, directory)
131
+ if info:
132
+ file_list.append(info)
133
+ processed_count += 1
134
+
135
+ # Yield updates periodically to keep the UI responsive
136
+ if processed_count % UPDATE_INTERVAL == 0:
137
+ df = pd.DataFrame(file_list)
138
+ yield create_treemap_figure(df), f"Scanned {processed_count} files...", df
139
+ await asyncio.sleep(0.01) # Allow other tasks to run
140
+
141
+ # Final update after loop finishes or is stopped
142
+ df = pd.DataFrame(file_list)
143
+ final_status = f"Scan complete. Found {len(df)} files."
144
+ if stop_flag_state['stop']:
145
+ final_status = f"Scan stopped. Displaying {len(df)} found files."
146
+
147
+ yield create_treemap_figure(df), final_status, df
148
+
149
+ def stop_scan(stop_flag_state):
150
+ """Sets the stop flag to True."""
151
+ stop_flag_state['stop'] = True
152
+ return stop_flag_state, "Stopping scan..."
153
+
154
+ # --- Gradio UI ---
155
+ with gr.Blocks(theme=gr.themes.Soft(), title="File System Treemap") as app:
156
+
157
+ stop_flag = gr.State({'stop': False})
158
+
159
+ gr.Markdown("# 📁 File System Treemap Visualizer")
160
+ gr.Markdown("Enter a directory path to generate a squarified treemap. The visualization will build in real-time.")
161
+
162
+ with gr.Row():
163
+ path_input = gr.Textbox(
164
+ label="Directory Path",
165
+ placeholder="e.g., C:\\Users\\YourUser\\Documents",
166
+ scale=3
167
+ )
168
+ start_button = gr.Button("Start Scan", variant="primary", scale=1)
169
+ stop_button = gr.Button("Stop Scan", variant="stop", scale=1)
170
+
171
+ status_label = gr.Label("Status: Ready")
172
+
173
+ with gr.Tabs():
174
+ with gr.TabItem("Treemap Visualization"):
175
+ plot_output = gr.Plot(interactive=True)
176
+ with gr.TabItem("Data Table"):
177
+ data_output = gr.DataFrame(wrap=True)
178
+
179
+ # Event Handlers
180
+ start_button.click(
181
+ fn=scan_directory,
182
+ inputs=[path_input, stop_flag],
183
+ outputs=[plot_output, status_label, data_output]
184
+ )
185
+
186
+ stop_button.click(
187
+ fn=stop_scan,
188
+ inputs=[stop_flag],
189
+ outputs=[stop_flag, status_label],
190
+ cancels=[start_button.click] # This cancels the running 'scan_directory' event
191
+ )
192
+
193
+ if __name__ == "__main__":
194
+ app.launch()