我正在尝试学习如何将我的Highchart从python脚本转移到Django。我一直在使用this solution thread去那里(但到目前为止都是徒劳的)。你知道吗
我有一个python脚本叫做脚本.py返回名为图表数据的对象。现在在Django,我想在我的视图.py执行脚本.py文件,抓取图表\数据对象并将其分配给一个名为“data”的新对象。你知道吗
我的脚本.py文件存储在我的项目根目录中名为/scripts/的目录中,它包含自己的\uuu init\uuuuuy.py文件。你知道吗
对于下面的代码,服务器返回一个错误,该错误说明:全局名称“chart\u data”未定义。你知道吗
如果能解释一下我做错了什么,我将不胜感激。你知道吗
### views.py
from __future__ import unicode_literals
import datetime
import subprocess
import scripts
def plot(request, chartID = 'chart_ID', chart_type = 'line', chart_height = 500):
raw = subprocess.Popen('~/scripts/script.py', shell=True)
data = chart_data
chart = {"renderTo": chartID, "type": chart_type, "height": chart_height,}
title = {"text": 'my title here'}
xAxis = {"title": {"text": 'xAxis name here'}, "categories": data['Freq_MHz']}
yAxis = {"title": {"text": 'yAxis name here'}}
series = [
{"name": 'series name here', "data": data['series name here']},
]
return render(request, '/chart.html', {'chartID': chartID, 'chart': chart,
'series': series, 'title': title,
'xAxis': xAxis, 'yAxis': yAxis})
是的,
chart_data
在这段代码的任何地方都没有定义。你知道吗您似乎将脚本作为一个外部进程运行,然后期望Django以某种方式知道它定义的变量。根本不是这样的。你知道吗
与其通过Popen调用脚本,不如像导入其他Python模块一样导入脚本。然后您可以调用它的任何函数,并将它们的返回值赋给您喜欢的任何函数。你知道吗
将脚本放入正在使用的Django应用程序中,然后导入:
相关问题 更多 >
编程相关推荐