我这个简单的场景我试图通过python创建并转储json字符串然后解析json转储并通过html打印它。
蟒:
import json
pythonDictionary = {'name':'Bob', 'age':44, 'isEmployed':True}
dictionaryToJson = json.dumps(pythonDictionary)
html& JS
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p id="messages"></p>
<p>My first paragraph.</p>
<script>
document.getElementById("messages").innerHTML = dictionaryToJson;
</script>
</body>
</html>
答案 0 :(得分:0)
您可以像以下
一样使用python-shell
var PythonShell = require('python-shell');
var pyshell = new PythonShell("code.py");
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
document.getElementById("messages").innerHTML = message;
});
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err){
throw err;
};
console.log('finished');
});
我假设code.py
包含python代码
import json
pythonDictionary = {'name':'Bob', 'age':44, 'isEmployed':True}
dictionaryToJson = json.dumps(pythonDictionary)
print(dictionaryToJson )