如何返回响应HttpResponse?

时间:2017-04-12 17:58:05

标签: javascript python

我是python / javascript的新手,这是我的第一篇文章。我想提交一个'课程名称' (即bio101)在html中,使用javascript发送'课程名称'到python,使用python进行mysql搜索以获取关于' course_name'的信息。然后计算一些关于课程的东西(即班上学生的DNA基因型)。然后我希望python将关于学生DNA基因型的总计算结果发送回javascript,并绘制一个表格来显示计算结果。我的代码返回对象:对象为javascript,而不是存储在' gwas'中的数据。字典。我认为问题出现在python脚本的最后一行代码中,但我无法弄清楚如何使这一行工作。

在javascript中,相关的行是:

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);

在python中,相关的行是:

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")

0 个答案:

没有答案