DEV Community

Kanatbek-AKA
Kanatbek-AKA

Posted on • Updated on

Question: How to display Python output on a webpage with Node.js?

Hi all,

First of all, I would like to apologize for my question if it is not allowed to post questions on this website, then I have not seen and read in the code of conduct that to post a question is not allowed to.

Can someone please assisst me to resolve the issue with displaying a python output on a webpage with node.js using modules Python-Shell and express? I am novice in back-end
Codes:
`// routes file table.js
router.get("/", function (req, res, next) {
const test = py.PythonShell.run("pyextc.py", null, (error, res) => {
if (error) throw error;
return JSON.stringify(res);
});
res.render("table", { python: { ...test } }); <<<--- I assume the issue lies here which I could not figure out.

});

module.exports = router;

// table.ejs
<!DOCTYPE html>
<html>
<head>
<title>Test Python output</title>
</head>
<body>
<h1>Python output Api <pre><code><%= python %></code></pre>
<div><%= python %></div>
</body>
</html>


// pyextc.py
print("Hey, I am Python displaying on a table webpage running from Back-End.")
`
The code above display on the webpage this output:
Image description

Although if you run this code:
async function PyFile() {
try {
const test = await py.PythonShell.run("pyextc.py", null, (error, res) => {
if (error) throw error;
console.log(res.toString());
});
} catch (error) {
console.log(JSON.stringify("Gotcha error " + error));
}
}
PyFile();

The python output displays on the console, and if its working on console, it should also be working on a webpage, but thought as a novice - it does not return python output.

Thanks in advance for any hints and advise.

Best regards,
Kanatbek

Top comments (0)