如何传递python json.dump()以html格式打印?

2024-09-24 08:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Python:

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>

Tags: 字符串nameimportjsonagemyhtml场景
1条回答
网友
1楼 · 发布于 2024-09-24 08:23:36

您可以像下面这样使用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 )

相关问题 更多 >