通过 Ajax 'is not defined' 调用的 Python 函数

标签 python ajax django xlsxwriter

我正在调用一个 Python 函数,该函数使用 XlsxWriter 在我的 Django 应用程序中通过 Ajax 将一些数据写入 excel。我使用 jQuery 绑定(bind)我的其中一个按钮的 onclick 方法来调用此函数。但是,当我调用它时,Django 给我一个错误,提示“exportHistoExcel 未定义”。这让我感到困惑,因为我在同一个脚本中的所有其他函数都被识别并正在运行,但是由于某种原因没有定义一个函数。谁能帮帮我?

这是我的 python 脚本:

from xlsxwriter.workbook import Workbook

def exportHistoExcel(SE_filelocation, VTfile_location, filename):

    print('anything?')
    #new getFiringRates return statement:
    #    if generating_excel_file: return [bins, spikeanglebins, headanglebins, times, firingrates]
    #   else: return True
    #new getFiringRates parameter:
    #   generating_excel_file = False

    data = getFiringRates(SE_filelocation, VTfile_location, generating_excel_file = True)
    print(data)

    workbook = Workbook(filename)
    worksheet = workbook.add_worksheet()
    worksheet.write('A1', 'Bin (degrees)')
    worksheet.write('B1', '# of Spikes')
    worksheet.write('C1', '# of Samples')
    worksheet.write('D1', 'Time (sec)')
    worksheet.write('E1', 'Firing Rate')

    worksheet.write_column('A2', data[0])
    worksheet.write_column('B2', data[1])
    worksheet.write_column('C2', data[2])
    worksheet.write_column('D2', data[3])
    worksheet.write_column('E2', data[4])
    worksheet.write('A62', 360); worksheet.write('B62', '=$B$2')
    worksheet.write('C62', '=$C$2'); worksheet.write('D62', '=$D$2');   worksheet.write('E62', '=$E$2')


    histo = workbook.add_chart({'type': 'line'})
    histo.set_title({'name': 'Firing Rates'})
    histo.set_x_axis({'name': 'Bin (degrees)'})
    histo.set_y_axis({'name': 'Firing rate (sec^-1)'})
    histo.add_series({'values': '=Sheet1!$E$2:$E$61',
                      'line': {'color': 'black'},
                     'categories': '=Sheet1!$A$2:$A$61'})
    histo.set_legend({'delete_series': [0]})
    worksheet.insert_chart('F2', histo)


    workbook.close()

这是我的 ajax.py 文件:

from django.utils import simplejson
from dajaxice.decorators import dajaxice_register
from hipercic.apps.NeuroCiC import models
from django.core.files import storage
import file_analysis
import sys

@dajaxice_register
def export_excel_file(request, id):
  print 'TRIAL ID', id

  try:
    trial = models.Trial.objects.get(pk=id)
  except:
    'Could not find trial.'
  else:
    print 'Found Trial'
    print('~/hipercic/apps/NeuroCiC/uploads/' + trial.spikes_file.url)
    SE_loc = '~/hipercic/apps/NeuroCiC/uploads/' + trial.spikes_file.url
    VT_loc = '~/hipercic/apps/NeuroCiC/uploads/' + trial.led_file.url
    print(SE_loc)
    print(VT_loc)
    exportHistoExcel(SE_loc, VT_loc, "demo.xlsx")
    return

ajax中的print语句都是打印到终端,exportHitoExcel中的都不是。为什么它不能识别我的 python 函数?

最佳答案

当我改变的时候它起作用了

exportHistoExcel(SE_loc, VT_loc, "demo.xlsx")

这一行:

return simplejson.dumps({'scatter_data': file_analysis.exportHistoExcel("test.xlsx") })

关于通过 Ajax 'is not defined' 调用的 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17346343/

相关文章:

python - python中的帕斯卡三角形

python - 通过 Matplotlib 绘制正态密度的判别函数

python - 为什么第一个元素不起作用?

python - 如何将预测序列转换回keras中的文本?

Python 链表查询

jquery - 为什么我的 PUT 请求收到 404 状态错误?

javascript - JStree:如何将 AJAX 加载的根节点的状态设置为未确定

javascript - 纯文本形式的 Ajax responseText [jQuery + jQuery 表单插件]

Django oauth 工具包不适用于 apache

python - 尽管设置正确,媒体中的 Django 图像仍不显示