如何使JavaScript和Python协同工作?

时间:2017-11-16 12:54:01

标签: javascript jquery python python-3.x

我想为JavaScript和Python制作双向通信模型。 我希望JavaScript处理数据并将其发送到Python,然后Python处理它并将其发送回JavaScript。我不知道如何实现这一目标以及如何在两种语言之间进行沟通。

考虑一下这个例子:

阅读评论以了解模型的清晰度!

var files = [];
const div_files = document.getElementById("files");
const div_results = document.getElementById("results");

function fetchNames() {
  div_files.innerHTML = ""; // clears the file div
  div_results.innerHTML = ""; // clears the results div

  var checkbox_elements = document.getElementsByClassName('filename');

  Array.from(checkbox_elements).forEach(function(k) {
    if (k.checked) {

      files.push(k.value);
      div_files.innerHTML += k.value + '<br>';
    }

  });

  // the files array should now be shared with python 
  
  //console.log(files);

  // JavaScript SHOULD WAIT while PYTHON processes the data and shares the cal_array with JS

  // when the cal_array is available with JS only then it shall start processing the code mentioned below
  
  var cal_array = [
    [2231, 11640, 104621],
    [2231, 11640, 104621],
    [9, 494, 3339]
  ];
  Array.from(cal_array).forEach(function(k) {
    div_results.innerHTML += k + '<br>';
  })



};
<form>
  <input type="checkbox" class="filename" value="./first.html">First File<br>
  <input type="checkbox" class="filename" value="./second.html">Second File<br>
  <input type="checkbox" class="filename" value="./third.html">Third File<br>
  <br>
  <input type="button" value="Submit" onclick="fetchNames()">
</form>
<br>
The selected file(s) are:
<div id="files"></div>
<br> The result shows the number of lines, words, and characters of the respective files mentioned above:
<div id="results"></div>

Python脚本如下:

import os

files = ['./first.html','./second.html','./firstfile.txt'] #this array should be passed into python by the JS script
cal_array = [] #this array should be shared with JS after data has been entered into it
def calculation(): # this function calculates the number of lines, words and characters of the selected file(s)
	for val in files:
		file_details = []
		fname=(val)
		infile=open(fname, 'r')
		lines=0
		words=0
		characters=0
		for line in infile:
		    line = line.strip(os.linesep)
		    wordslist=line.split()
		    lines=lines+1
		    words=words+len(wordslist)
		    characters=characters+ len(line)
		file_details.append(lines)
		file_details.append(words)
		file_details.append(characters)
		cal_array.append(file_details)

calculation()


print(cal_array)

# some code here to share cal_array with javascript

我是Python中的菜鸟,但我想知道如何使两种语言相互交互?我真的很感激任何帮助!

2 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,这是一个非常广泛的问题。由于JavaScript存在于浏览器中并且python在您的计算机上运行,​​因此您必须设置某种类型的Web服务器以便两者一起通信。最常见也很简单的方法是通过正常的HTTP请求响应方法。最简单的组合是python站点上的Flask和JavasScript端的AJAX。但是,还有其他一百种方法可以做到这一点。我建议你阅读:

REST API: https://www.codecademy.com/courses/javascript-beginner-en-EID4t/0/4

Ajax(JavaScript) https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp

Flask(Python)http://flask.pocoo.org/

答案 1 :(得分:1)

如果你想要的是创建一个web-app,然后通过那里将数据发送到你的服务器。 AJAX,你将在Python中处理数据,你需要的是Flask

如果您想要创建一个也运行Python代码的Web应用程序,您需要一个用JavaScript编写的python解释器,例如pyjs