Home > OS >  Django AttributeError: 'list' object has no attribute 'sort_values'
Django AttributeError: 'list' object has no attribute 'sort_values'

Time:10-31

I'm getting AttributeError: 'list' object has no attribute 'sort_values' in below code,

task.py:

from __future__ import absolute_import,unicode_literals
from celery import shared_task
from time import sleep
import eda
import os

@shared_task
def aync_task(amz_columns_dict, download_path, file_name, data):
    sleep(10)
    eda_object = eda.eda(col_dict=amz_columns_dict)
    save_path = download_path
    name_of_file = file_name 
    file_path = os.path.join(save_path, name_of_file ".html")     
    eda_object.create_report(data=data, filename=file_path)
    return 'task complete'

views.py :

def eda_flow(request):
    path = '/Unilever/satyajit/us_amz.csv'
    mode = 'rb'
    df = pd.read_csv("/home/satyajit/Desktop/opensource/data/us_amz.csv", low_memory=False)
    df = df.head(100)
    json_records = df.reset_index().to_json(orient ='records')
    data = []
    data = json.loads(json_records)
    context = {'data': data, 'message': 'data loaded successfully.'}
    if request.method == 'POST':
        id_col = request.POST.get('id_col')
        file_name = request.POST.get('file_name')
        download_path = request.POST.get('download_path')
        amz_columns_dict = {'id_col': id_col}
        try:   
            if os.path.exists(download_path):
                status = aync_task.delay(amz_columns_dict, download_path, file_name, data)
                return render(request,'home/index.html', {'message': 'Save Complete'})
            else:
                return render(request,'home/index.html', {'message': 'download path is not exist'})
        except Exception as e:
            print('error is---->', e)
            return render(request,'home/index.html', {'message': 'Error while generating EDA'})
    return render(request, "home/tables-simple.html", context)

The error of this code on below as screenshot:

enter image description here

I've also tried to search similar question here (similar question) but that does not helpful to me.

Any help would be much appreciated. thanks in advance.

CodePudding user response:

Your data variable that is being passed to the async_task method is being set using data = json.loads(json_records) so it's a normal Python list. You must convert this to a Pandas dataframe before using it with eda_object.create_report.

CodePudding user response:

Perhaps it's just the order in which you are doing these that is causing all the errors. For the context you can convert to json, but for your aync_task pass the dataframe:

def eda_flow(request):
    path = '/Unilever/satyajit/us_amz.csv'
    mode = 'rb'
    df = pd.read_csv("/home/satyajit/Desktop/opensource/data/us_amz.csv", low_memory=False)
    df = df.head(100)

    # Don't change to json here, leave json_records as dataframe
    json_records = df.reset_index()

    # To create the data in json format
    data = json.loads(json_records.to_json(orient ='records'))

    context = {'data': data, 'message': 'data loaded successfully.'}
    if request.method == 'POST':
        id_col = request.POST.get('id_col')
        file_name = request.POST.get('file_name')
        download_path = request.POST.get('download_path')
        amz_columns_dict = {'id_col': id_col}
        try:   
            if os.path.exists(download_path):

                # pass the dataframe json_records here
                status = aync_task.delay(amz_columns_dict, download_path, file_name, json_records)

                return render(request,'home/index.html', {'message': 'Save Complete'})
            else:
                return render(request,'home/index.html', {'message': 'download path is not exist'})
        except Exception as e:
            print('error is---->', e)
            return render(request,'home/index.html', {'message': 'Error while generating EDA'})
    return render(request, "home/tables-simple.html", context)
  • Related