Skip to content

Conversation

kumaradarsh2
Copy link

@kumaradarsh2 kumaradarsh2 commented Oct 19, 2025

Closes #3

This pull request introduces a new lightweight endpoint, POST /process/preview, designed to generate a quick preview of a CSV/Excel file. This feature allows a frontend application to quickly display column names and the first few rows of data after a user provides a file, URL, or raw text, enhancing the user experience for data import workflows.

Implementation Details

  • New Endpoint: Created the POST /process/preview route in app/process.py.
    • It accepts the same input methods as the main /process/csv endpoint (file upload, url, or raw_csv) for consistency.
    • It includes robust error handling that returns a structured JSON error with a 400 status code, matching the existing API's error response format.
  • New Helper Function: Added a generate_preview() function to app/processing.py.
    • This function efficiently reuses the existing parse_input() logic to handle any data source and create a pandas DataFrame.
    • It then extracts the column headers and the first 5 rows (df.head(5)).
    • Finally, it formats the output into the required JSON structure: {"columns": [...], "rows": [...]}.

All acceptance criteria outlined in the issue have been met.

Testing and Validation

I have successfully tested the endpoint locally. I used a curl command for the final verification because I encountered a 422 Unprocessable Entity error when using the interactive API docs at /docs.

It appears the docs page sends placeholder strings for the optional file and url parameters, which causes the backend validation to fail. The curl command allowed me to construct the request correctly by sending only the intended parameter (file or raw_csv).

Here is the screenshot from the successful curl test in my terminal:

image

How to Test

This endpoint can be tested by sending a sample CSV file using curl.

Note: Testing via the interactive docs at /docs is currently unreliable due to an issue where the UI sends extra empty parameters, causing a 422 validation error. The curl method below is the recommended way to verify the functionality.

  1. Create a sample data file: In the project's root directory, create a new file named data.csv with the following content:

    "Transaction_ID","Product","Price","Date"
    1,"Laptop",1200,"2024-01-15"
    2,"Mouse",25,"2024-01-15"
    3,"Keyboard",75,"2024-01-16"
    4,"Monitor",300,"2024-01-17"
    5,"Webcam",50,"2024-01-17"
    
  2. Run the server locally:

    uvicorn main:app --reload
  3. Execute the curl command: In a new terminal window, run the following command to upload the data.csv file to the file parameter:

    curl -X 'POST' \
      '[http://127.0.0.1:8000/process/preview](http://127.0.0.1:8000/process/preview)' \
      -H 'accept: application/json' \
      -H 'Content-Type: multipart/form-data' \
      -F 'file=@data.csv'
  4. Verify the response: Confirm that the server responds with a 200 OK status and the correctly formatted JSON preview, including the column names and the first five rows of data.

@Hrishikesh-Dalal
Copy link
Contributor

The changes look good! Just add screenshots of the result with the sample input file

@kumaradarsh2
Copy link
Author

Hi @Hrishikesh-Dalal , I've added the screenshot as requested and included a note about the testing process. The endpoint is working as expected!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a CSV preview endpoint to return column names + first N rows

2 participants