function upload_analysis_dataset
Flask API endpoint that handles file upload for data analysis sessions, accepting CSV and Excel files, validating user access, and processing the dataset through a data analysis service.
/tf/active/vicechatdev/vice_ai/new_app.py
5964 - 6009
moderate
Purpose
This endpoint serves as the file upload handler for data analysis sessions in a Flask web application. It authenticates users, verifies session ownership, validates uploaded files (CSV/Excel only), temporarily stores the file, processes it through the data analysis service, and returns the processing result. It includes comprehensive error handling for missing files, invalid formats, and service unavailability.
Source Code
def upload_analysis_dataset(session_id):
"""Upload a dataset for analysis"""
if not DATA_ANALYSIS_AVAILABLE:
return jsonify({'error': 'Data analysis service not available'}), 503
user_email = get_current_user()
# Get session and verify ownership
session = data_analysis_service.get_analysis_session(session_id)
if not session or session.user_id != user_email:
return jsonify({'error': 'Session not found or access denied'}), 404
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
if not file.filename.lower().endswith(('.csv', '.xlsx', '.xls')):
return jsonify({'error': 'Only CSV and Excel files are supported'}), 400
try:
# Save uploaded file temporarily
import tempfile
from werkzeug.utils import secure_filename
filename = secure_filename(file.filename)
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(filename)[1]) as tmp_file:
file.save(tmp_file.name)
# Process the dataset
result = data_analysis_service.upload_dataset(
session_id=session_id,
file_path=tmp_file.name,
original_filename=filename
)
# Clean up temp file
os.unlink(tmp_file.name)
return jsonify(result)
except Exception as e:
logger.error(f"Error uploading dataset: {e}")
return jsonify({'error': str(e)}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
session_id |
- | - | positional_or_keyword |
Parameter Details
session_id: String identifier for the analysis session. Used to retrieve the session from the data analysis service and verify user ownership. Must correspond to an existing session owned by the authenticated user.
Return Value
Returns a Flask JSON response. On success (200): JSON object containing the result from data_analysis_service.upload_dataset() with dataset processing information. On error: JSON object with 'error' key and appropriate HTTP status code - 503 if service unavailable, 404 if session not found or access denied, 400 if file validation fails, 500 if processing exception occurs.
Dependencies
flaskwerkzeugtempfileoslogging
Required Imports
from flask import request, jsonify
import os
import logging
import tempfile
from werkzeug.utils import secure_filename
Conditional/Optional Imports
These imports are only needed under specific conditions:
import tempfile
Condition: imported inside the try block for temporary file handling
Required (conditional)from werkzeug.utils import secure_filename
Condition: imported inside the try block for filename sanitization
Required (conditional)Usage Example
# This is a Flask route handler, not called directly
# Example client-side usage:
import requests
session_id = 'abc123-session-id'
file_path = 'data.csv'
with open(file_path, 'rb') as f:
files = {'file': f}
headers = {'Authorization': 'Bearer YOUR_TOKEN'}
response = requests.post(
f'http://localhost:5000/api/analysis-sessions/{session_id}/upload',
files=files,
headers=headers
)
if response.status_code == 200:
result = response.json()
print(f"Dataset uploaded: {result}")
else:
print(f"Error: {response.json()['error']}")
Best Practices
- Always clean up temporary files using os.unlink() after processing to prevent disk space issues
- Use secure_filename() to sanitize user-provided filenames and prevent directory traversal attacks
- Verify session ownership before allowing file uploads to prevent unauthorized access
- Validate file extensions before processing to ensure only supported formats are accepted
- Use NamedTemporaryFile with delete=False to ensure file persists during processing but can be manually cleaned up
- Implement proper error handling and logging for debugging and monitoring
- Return appropriate HTTP status codes (503, 404, 400, 500) for different error scenarios
- Check DATA_ANALYSIS_AVAILABLE flag before processing to handle service unavailability gracefully
- Use try-except blocks to catch and handle processing exceptions without crashing the application
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function upload_data_section_dataset 86.3% similar
-
function upload_data 83.7% similar
-
function smartstat_upload_data 78.6% similar
-
function export_analysis_content 76.0% similar
-
function get_analysis_files 74.4% similar