🔍 Code Extractor

function upload_data

Maturity: 52

Flask route handler that accepts file uploads via POST request, validates the file, saves it with a timestamp, and loads the data into an analysis session.

File:
/tf/active/vicechatdev/full_smartstat/app.py
Lines:
142 - 190
Complexity:
moderate

Purpose

This endpoint handles data file uploads for statistical analysis sessions. It validates file types against allowed extensions, securely saves uploaded files with timestamped filenames, creates a DataSource object, and initiates data loading through the analysis service. The function returns metadata about the uploaded data including shape, columns, and a summary.

Source Code

def upload_data(session_id):
    """Upload data file for analysis"""
    try:
        if 'file' not in request.files:
            return jsonify({'success': False, 'error': 'No file provided'}), 400
        
        file = request.files['file']
        if file.filename == '':
            return jsonify({'success': False, 'error': 'No file selected'}), 400
        
        if not allowed_file(file.filename):
            return jsonify({
                'success': False, 
                'error': f'File type not allowed. Supported: {app_config.ALLOWED_EXTENSIONS}'
            }), 400
        
        # Save uploaded file
        filename = secure_filename(file.filename)
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"{timestamp}_{filename}"
        file_path = os.path.join(app_config.UPLOAD_FOLDER, filename)
        file.save(file_path)
        
        # Create data source
        data_source = DataSource(
            source_type=DataSourceType.FILE_UPLOAD,
            file_path=file_path
        )
        
        # Load data for session
        result = analysis_service.load_data_for_session(session_id, data_source)
        
        if result['success']:
            return jsonify({
                'success': True,
                'message': 'Data uploaded and processed successfully',
                'data_summary': result['data_summary'],
                'shape': result['shape'],
                'columns': result['columns']
            })
        else:
            return jsonify(result), 500
            
    except Exception as e:
        logger.error(f"Error uploading data: {str(e)}")
        return jsonify({
            'success': False,
            '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 associate the uploaded data with a specific user session. This parameter is extracted from the URL path and passed to the analysis service to load data for the correct session.

Return Value

Returns a Flask JSON response tuple containing (response_body, status_code). On success (200): {'success': True, 'message': str, 'data_summary': dict, 'shape': tuple, 'columns': list}. On error (400/500): {'success': False, 'error': str}. Status codes: 200 for success, 400 for validation errors (no file, empty filename, invalid file type), 500 for processing errors.

Dependencies

  • flask
  • werkzeug
  • pandas
  • pathlib
  • datetime
  • os
  • logging
  • typing

Required Imports

from flask import request, jsonify
from werkzeug.utils import secure_filename
from datetime import datetime
import os
import logging

Usage Example

# Client-side usage (JavaScript fetch example):
const formData = new FormData();
formData.append('file', fileInput.files[0]);

fetch('/upload_data/session_123', {
  method: 'POST',
  body: formData
})
.then(response => response.json())
.then(data => {
  if (data.success) {
    console.log('Data shape:', data.shape);
    console.log('Columns:', data.columns);
    console.log('Summary:', data.data_summary);
  } else {
    console.error('Upload failed:', data.error);
  }
});

# Server-side setup required:
# app_config.py
ALLOWED_EXTENSIONS = {'.csv', '.xlsx', '.json', '.txt'}
UPLOAD_FOLDER = '/path/to/uploads'

# Helper function
def allowed_file(filename):
    return Path(filename).suffix.lower() in app_config.ALLOWED_EXTENSIONS

# Initialize services
analysis_service = StatisticalAnalysisService()
logger = logging.getLogger(__name__)

Best Practices

  • Always validate file extensions using allowed_file() before processing to prevent security vulnerabilities
  • Use secure_filename() to sanitize user-provided filenames and prevent directory traversal attacks
  • Timestamp filenames to prevent collisions and maintain upload history
  • Ensure UPLOAD_FOLDER exists and has appropriate write permissions before deployment
  • Implement file size limits in Flask configuration (MAX_CONTENT_LENGTH) to prevent DoS attacks
  • Clean up old uploaded files periodically to manage disk space
  • Consider implementing virus scanning for uploaded files in production environments
  • Log errors with sufficient context for debugging but avoid logging sensitive data
  • Return appropriate HTTP status codes (400 for client errors, 500 for server errors)
  • Handle the case where analysis_service.load_data_for_session() fails gracefully
  • Consider implementing rate limiting to prevent abuse of the upload endpoint

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function upload_analysis_dataset 83.7% similar

    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.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function upload_data_section_dataset 77.8% similar

    Flask API endpoint that handles CSV file uploads for data section analysis, processes the file, extracts metadata, and stores it in the data section for persistence.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function smartstat_upload_data 76.6% similar

    Flask route handler that uploads CSV or Excel data files to a SmartStat analysis session, with support for multi-sheet Excel files and session recovery.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function api_upload_document_v1 72.4% similar

    Flask API endpoint that handles document file uploads, validates file type and size, stores the file temporarily, and extracts basic text content for processing.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function smartstat_upload_files 71.2% similar

    Flask API endpoint that handles multi-file uploads (CSV, Excel, PDF, Word, PowerPoint) to a SmartStat session, processing data files as datasets and documents as information sheets.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
← Back to Browse