Spaces:
Sleeping
Sleeping
Feat: Improve CSV preview to show first 5 lines
Browse files
app.py
CHANGED
|
@@ -205,12 +205,26 @@ def process_input_updated(url_or_id, source_type, depth, output_format_selection
|
|
| 205 |
preview_content = raw_content # Default to markdown
|
| 206 |
if output_format_selection == "JSON":
|
| 207 |
preview_content = convert_to_json(raw_content, url_or_id)
|
| 208 |
-
elif output_format_selection == "CSV":
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
progress(1, desc="Processing complete.")
|
| 216 |
return f"Successfully processed: {url_or_id}", preview_content, output_file_path
|
|
|
|
| 205 |
preview_content = raw_content # Default to markdown
|
| 206 |
if output_format_selection == "JSON":
|
| 207 |
preview_content = convert_to_json(raw_content, url_or_id)
|
| 208 |
+
elif output_format_selection == "CSV" and output_file_path:
|
| 209 |
+
try:
|
| 210 |
+
with open(output_file_path, 'r', encoding='utf-8') as f_csv:
|
| 211 |
+
# Read the first 5 lines for preview
|
| 212 |
+
csv_preview_lines = [next(f_csv) for _ in range(5)]
|
| 213 |
+
preview_content = "".join(csv_preview_lines)
|
| 214 |
+
if not preview_content: # Handle empty or very short CSV
|
| 215 |
+
preview_content = "[CSV content is empty or very short]"
|
| 216 |
+
except StopIteration: # Handle files with less than 5 lines
|
| 217 |
+
# If StopIteration occurs, it means we've read all lines.
|
| 218 |
+
# We need to re-open and read all lines if it was less than 5.
|
| 219 |
+
with open(output_file_path, 'r', encoding='utf-8') as f_csv:
|
| 220 |
+
preview_content = f_csv.read()
|
| 221 |
+
if not preview_content:
|
| 222 |
+
preview_content = "[CSV content is empty]"
|
| 223 |
+
except Exception as e_csv_preview:
|
| 224 |
+
preview_content = f"[Error reading CSV for preview: {str(e_csv_preview)}]"
|
| 225 |
+
elif output_format_selection == "CSV" and not output_file_path:
|
| 226 |
+
preview_content = "[CSV file path not available for preview]"
|
| 227 |
+
|
| 228 |
|
| 229 |
progress(1, desc="Processing complete.")
|
| 230 |
return f"Successfully processed: {url_or_id}", preview_content, output_file_path
|