Python Snippets

FastAPI Endpoint for Uploading and Validating CSV Files

This snippet demonstrates how to create a FastAPI endpoint that accepts CSV file uploads, validates the file structure, and returns a summary of the data. This is useful for applications where users need to upload data files that must conform to a specific format.

from fastapi import FastAPI, UploadFile, File, HTTPException
import pandas as pd
from io import StringIO
from typing import List

app = FastAPI()

@app.post("/upload-csv/")
async def upload_csv(file: UploadFile = File(...)):
    # Check if file is CSV
    if not file.filename.endswith(".csv"):
        raise HTTPException(status_code=400, detail="File must be a CSV")
    
    try:
        # Read CSV into DataFrame
        contents = await file.read()
        data = StringIO(contents.decode("utf-8"))
        df = pd.read_csv(data)
        
        # Validate columns (example: require 'id' and 'name' columns)
        required_columns = ["id", "name"]
        if not all(col in df.columns for col in required_columns):
            missing = [col for col in required_columns if col not in df.columns]
            raise HTTPException(status_code=400, detail=f"Missing required columns: {missing}")
        
        # Return summary statistics
        return {
            "filename": file.filename,
            "columns": list(df.columns),
            "row_count": len(df),
            "summary_stats": df.describe().to_dict()
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")

Explanation

What It Does

  1. File Type Validation: Ensures the uploaded file is a .csv file.
  2. CSV Parsing: Uses Pandas to read the file into a DataFrame.
  3. Column Validation: Checks if required columns (id, name) are present.
  4. Data Summary: Returns metadata about the file (columns, row count, basic statistics).

Why It’s Useful

How to Run

  1. Install dependencies:
    pip install fastapi pandas python-multipart uvicorn
    
  2. Save the code in main.py.
  3. Start the server:
    uvicorn main:app --reload
    
  4. Test the endpoint using curl or a tool like Postman:
    curl -X POST -F "file=@data.csv" http://localhost:8000/upload-csv/
    

Note: Replace data.csv with a CSV file containing at least id and name columns.