display csv data into flask api

  1. Import necessary modules:
from flask import Flask, jsonify
import csv
  1. Create a Flask application:
app = Flask(__name__)
  1. Define a route to handle CSV data:
@app.route('/api/csv_data', methods=['GET'])
  1. Create a function to read CSV data:
def get_csv_data():
    data = []
    with open('your_csv_file.csv', 'r') as file:
        csv_reader = csv.DictReader(file)
        for row in csv_reader:
            data.append(row)
    return data
  1. Define the route function to return CSV data as JSON:
def csv_to_json():
    csv_data = get_csv_data()
    return jsonify(csv_data)
  1. Run the Flask application:
if __name__ == '__main__':
    app.run(debug=True)

Make sure to replace 'your_csv_file.csv' with the actual path or filename of your CSV file.