0

I am new to python/javascript and this is my first post. I would like to submit a 'course name' (i.e. bio101) in html, use javascript to send the 'course name' to python, use python to do a mysql search to get info on 'course_name' and then calculate some stuff about the course (i.e. DNA genotypes of the students in the class). Then I want python to send the summed calculations about the student's DNA genotypes back to javascript, and draw a table showing the calculations. The code I have returns object:Object to javascript, not the data stored in the 'gwas' dictionary. I think the problem is somewhere in the last line of code in the python script, but I can't figure out how to make this line work.

In javascript, the relevant lines are:

click_course_retrieve: function(event, object, view) {
    var output = {};
    output['course-name'] = $('#course-name-textarea').val();
    response = $.get('/submit/get_course_data/', output, check_submission);

In python, the relevant lines are:

def get_course_data(request):
    course_name_request = request.GET.get('course-name')

    cursor = connections['default'].dict_cursor()

    query = """
    select *
    from interpretome_exercises.class_gwas
    where course_name = '%s';
    """ % course_name_request

    cursor.execute(query)

    data = cursor.fetchall()

    snps = [('A', 'G'),
           ('A', 'G'),
           ('C', 'G'),
           ('C', 'T'),
           ('A', 'G')]

    snp_name = ['4988235', '7495174', '713598', '17822931', '4481887']

    phenotypes = ['earwax', 'eyes', 'asparagus', 'bitter', 'lactose']

    traits = [  ('Wet', "Dry"),
                ('Brown/Other', 'Blue/Green'),
                ('Yes', 'No'),
                ('Yes', 'No'),
                ('Yes', 'No')]

    gwas = dict()

    for snp in snp_name:
        gwas[snp] = dict()
        gwas[snp]['earwax'] = [0, 0, 0, 0]
        gwas[snp]['eyes'] = [0, 0, 0, 0]
        gwas[snp]['asparagus'] = [0, 0, 0, 0]
        gwas[snp]['bitter'] = [0, 0, 0, 0]
        gwas[snp]['lactose'] = [0, 0, 0, 0]


    for row in data:
        for i, snp in enumerate(snp_name):
            for allele in row[snp]:

                if allele.find(snps[i][0]) != -1:
                    if row['lactose'] == 'lactose_yes':
                        gwas[snp]['lactose'][0] += 1
                    else:
                        gwas[snp]['lactose'][1] += 1

                    if row['bitter'] == 'bitter_yes':
                        gwas[snp]['bitter'][0] += 1
                    else:
                        gwas[snp]['bitter'][1] += 1

                    if row['asparagus'] == 'asparagus_yes':
                        gwas[snp]['asparagus'][0] += 1
                    else:
                        gwas[snp]['asparagus'][1] += 1

                    if row['eyes'] == 'brown' or row['eyes'] == 'other':
                        gwas[snp]['eyes'][0] += 1
                    else:
                        gwas[snp]['eyes'][1] += 1

                    if row['earwax'] == 'earwax_wet':
                        gwas[snp]['earwax'][0] += 1
                    else:
                        gwas[snp]['earwax'][1] += 1

                if allele.find(snps[i][1]) != -1:
                    if row['lactose'] == 'lactose_yes':
                        gwas[snp]['lactose'][2] += 1
                    else:
                        gwas[snp]['lactose'][3] += 1

                    if row['bitter'] == 'bitter_yes':
                        gwas[snp]['bitter'][2] += 1
                    else:
                        gwas[snp]['bitter'][3] += 1

                    if row['asparagus'] == 'asparagus_yes':
                        gwas[snp]['asparagus'][2] += 1
                    else:
                        gwas[snp]['asparagus'][3] += 1

                    if row['eyes'] == 'brown' or row['eyes'] == 'other':
                        gwas[snp]['eyes'][2] += 1
                    else:
                        gwas[snp]['eyes'][3] += 1

                    if row['earwax'] == 'earwax_wet':
                        gwas[snp]['earwax'][2] += 1
                    else:
                        gwas[snp]['earwax'][3] += 1

#     want to return gwas[];
    print(gwas)
    return http.HttpResponse(simplejson.dumps(gwas), mimetype = "application/json")
stuart kim
  • 53
  • 4
  • http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Daniel Lizik Apr 12 '17 at 17:59
  • You can also return JsonResponse object like `return JsonResponse(gwas)` https://docs.djangoproject.com/en/1.10/ref/request-response/#jsonresponse-objects – Ravi Kumar Apr 12 '17 at 18:00
  • 2
    Just so you know, the type of a dictionary in JavaScript is `object`. Are you sure you're not getting the right thing in the response, but when you print it, it says `Object {}` or `[Object]` or whatever your browser uses? – Carl Smith Apr 12 '17 at 18:03
  • The "relevant" lines of JS seem to be missing the definition of `check_submission`. – Daniel Roseman Apr 12 '17 at 18:07

0 Answers0