简体   繁体   English

如何在JavaScript中调用/使用python函数返回的数据?

[英]How to call/use data returned by python function in javascript?

I've defined a function in views.py like this 我像这样在views.py中定义了一个函数

def ma_fonction_bokeh(request, numero = None):
    CDR_COMMON = settings.DBCON[settings.MONGO_CDRSTATS['CDR_COMMON']]
    acc = CDR_COMMON.find()
    donnees = [] # on initialise une liste
    for i in acc:
        if i['accountcode'] == numero:
            donnees.append([calendar.timegm(i["start_uepoch"].timetuple()), i["duration"]])

    data = simplejson.dumps(donnees)

    return render(request,"frontend/mongraphe.html", {"name": numero, "data": data})

Then in the mongraphe.html file I've created a javascript code for a plot that I got from http://www.highcharts.com/stock/demo/dynamic-update What I want is to customize the plot with my own data which is returned by the function that I defined at first... so how can I do that? 然后在mongraphe.html文件中,我为从http://www.highcharts.com/stock/demo/dynamic-update获得的绘图创建了javascript代码。我想要的是使用自己的数据自定义绘图这是我最初定义的函数返回的...我该怎么做? I've tried the ajax function but without result 我已经尝试过ajax函数,但是没有结果

You can transfer data from your Python code to JavaScript in several ways. 您可以通过多种方式将数据从Python代码传输到JavaScript。

The "proper" way is to create another endpoint which only returns the JSON data and then use an AJAX request (for example, jQuery.getJSON(…) ) to request it. “正确”的方法是创建另一个仅返回JSON数据的终结点,然后使用AJAX请求(例如jQuery.getJSON(…) )来请求它。

A quicker way is to modify your template ( frontend/mongraphe.html ) to include the JSON data as a variable. 一种更快的方法是修改模板( frontend/mongraphe.html )以将JSON数据作为变量包括在内。 Since you didn't specify what Python library you're using, I'm going to assume you're using something like Django. 由于您没有指定要使用的Python库,因此我假设您使用的是Django。 Here's what you would add to your template to make the data available in JavaScript: 这是您要添加到模板中以使数据在JavaScript中可用的内容:

<script>
var data = {{data}};
</script>

Make sure that you put the <script> tag above some place before you try to access the data. 尝试访问数据之前 ,请确保将<script>标记放在某个位置上方。 The {{data}} part will output the JSON string that you created in your Python script so that the final output will be something like var data = {"hello": "world"}; {{data}}部分将输出您在Python脚本中创建的JSON字符串,因此最终输出将类似于var data = {"hello": "world"};

Now you should be able to access the data variable in your JavaScript and put the data into the Highcharts.js chart. 现在,您应该能够访问JavaScript中的data变量,并将数据放入Highcharts.js图表​​中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用javascript函数并在.net代码中使用返回的结果 - call javascript function and use the returned result in .net code 使用javascript调用python函数并在网页上显示返回的字符串 - Call python function with javascript and display returned string on web page 如何使用return函数之外的Ajax POST调用中返回的数据? - How can I use data returned in an Ajax POST call outside the return function? 使用从 API 调用返回的数据 Promise.then() function - Use data returned from API call outside of Promise.then() function 使用 Python 调用网页上的 JavaScript 函数 - Use Python to call a JavaScript function that is on a webpage 如何在 JavaScript 异步获取调用中使用返回的 JSON 错误 - How to use returned JSON error in a JavaScript async fetch call 如何在 ReactJS JSX 中调用从 NodeJS 返回的 JavaScript Function - How To Call A JavaScript Function Returned From NodeJS in ReactJS JSX 如何使用普通 JavaScript 访问 AJAX 调用中返回的数据? - How do I access the data returned in an AJAX call with vanilla JavaScript? 如何将生成器函数用作fetch调用返回的promise中的回调? - How to use a generator function as a callback inside a promise returned by fetch call? 如何从 function 调用中返回的 Promise 中提取数据 - How to extract data from Promise returned in function call in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM