CultriX commited on
Commit
9f97f87
·
1 Parent(s): fd5e0f7

Feat: Improve CSV preview to show first 5 lines

Browse files
Files changed (1) hide show
  1. app.py +20 -6
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
- # For CSV preview, maybe just show a message or first few lines
210
- preview_content = f"CSV file generated. Path: {output_file_path}\nFirst few lines might be shown here in a real app."
211
- # Or read a bit of the CSV for preview:
212
- # with open(output_file_path, 'r', encoding='utf-8') as f_csv:
213
- # preview_content = "".join(f_csv.readlines()[:5])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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