Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

extract Kaggle files by using flask API in python

I am creating an API using Python Flask API. below is the kaggle url which I am extracting.
https://www.kaggle.com/datasets/alphiree/cardiovascular-diseases-risk-prediction-dataset?select=CVD_cleaned.csv

Below is the script which I am using.
I am passing parameters through API to extract the kaggle csv public file.

from flask import Flask, request, jsonify
import pandas as pd
from kaggle.api.kaggle_api_extended import KaggleApi

app = Flask(__name__)

api = KaggleApi()
api.authenticate()

def get_file_from_kaggle(dataset_value, select):
    dataset = str(dataset_value)
    file_name = str(select)
    path = 'Assessment/files/'
    api.dataset_download_files(dataset, file_name, path, unzip=True)
    csv_file = f'./{select}'
    return pd.read_csv(csv_file)

@app.route("/get-kaggle-data/<dataset_value>", methods=["GET"])
def get_kaggle_data(dataset_value):
    try:
        select = request.args.get("select")
        if select:
            df = get_file_from_kaggle(dataset_value, select)
            return df.to_json(orient='records')
        else:
            return jsonify({'error': 'Missing "select" parameter'}), 400
    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == "__main__":
    app.run(debug=True)

below is the URL which I am testing.
http://127.0.0.1:5000/get-kaggle-data/alphiree/cardiovascular-diseases-risk-prediction-dataset?select=CVD_cleaned.csv

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But getting below error:
404 Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

>Solution :

Normally, the / character is used to separate components of the request path. That means that a request for /one/two/three is indicates a different resource than /one/two/four.

In your code, your kaggle dataset includes / characters, so Flask is looking for a route that matches /get-kaggle-data/alphiree/..., and no such route exists.

If you want a parameter to consume everything after a particular path component, even if it includes /, you need to tell Flask by using a path parameter type:

@app.route("/get-kaggle-data/<path:dataset_value>", methods=["GET"])
def get_kaggle_data(dataset_value):

With the above code, a request for /get-kaggle-data/one/two/three will match this function and will result in dataset_value having the value one/two/three.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading