如何传递python json.dump()在html中的<p> </p>中打印?

时间:2018-04-24 21:55:39

标签: javascript python json

我这个简单的场景我试图通过python创建并转储json字符串然后解析json转储并通过html打印它。

蟒:

import json

pythonDictionary = {'name':'Bob', 'age':44, 'isEmployed':True}
dictionaryToJson = json.dumps(pythonDictionary)

html&amp; 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>

1 个答案:

答案 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 )