Im using python-shell in order to communicate between node app and python script Firstly I send a variable to the python script. Based on it i create a dataframe. Then I want to send back the resulting dataframe to the node.js app and finally display it on the page. So the problem is that while in the console it prints the whole dataframe, it renders only last line on the webpage. Here I include the code
node.js code:
var pyvar = data["Search"][0]["imdbID"]
console.log(pyvar)
var pyshell = new PythonShell('script3.py');
pyshell.send(JSON.stringify(pyvar))
pyshell.on('message', function (message) {
jsondata = message
console.log(message);
});
pyshell.end(function (err) {
if (err){
throw err;
};
console.log('finished');
});
app.get("/data", function(req, res) {
res.render("data", {data: jsondata} )
})
EJS:
<h1> Table </h1>
<%=data%>
python script3.py
import sys, json
import pandas as pd
def read_in():
lines = sys.stdin.readlines()
return json.loads(lines[0])
def main():
lines = read_in()
array1=[1,2,3,4,5]
array2=["a","b","c","d","e"]
df=pd.DataFrame({"onecolumn": lines,
"second":array1,
"third": array2
})
print (df)
if __name__ == '__main__':
main()
In the console I get:
onecolumn second third
0 pyvar 1 a
1 pyvar 2 b
2 pyvar 3 c
3 pyvar 4 d
4 pyvar 5 e
finished
However /data displays:
Table
4 pyvar 5 e
The pyshell.on('message', function(message){})
part of this is the problem. Every time it gets a message, it is replacing it replaces jsondata with the value it receives. It's most likely receiving data from the python program line-by-line.
Instead, you're going to want to concatenate whatever you're getting from pyshell.
var jsonData = ""
pyshell.on("messsage", function(message){
jsonData += message;
});
pyshell.end(function(err){
console.log(jsonData);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.