Saturday, 6 May 2017

parseError when running Python script from node.js

I am constructing a movie recommender. My recommender engine is written in Python. And I am running it from website through node.js (Express).

The python code itselfs work and here is the output when I am running it from a console. It is using pandas and numpy for calculations it returns a matrix with title of movie and its similarity to a chosen movie, and I also print hello:

Python command code

On my website I have following HTML in body:

<form class="test" method="post" action="/test">
   <input type="text" name="user[name]">
   <input class="button" type="submit" value="Submit">
</form>

JS Client side

(function($) {

  $(document).ready(function () {
      var btn = $('.button'),
          input = $('input');
      btn.on('click', function() {
        e.preventDefault();
      })
  })
})(jQuery)

JS server side, with Express

var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var PythonShell = require('python-shell');

var options = {
  mode: 'text',
  pythonOptions: ['-u'],
  scriptPath: "E:/Praca Magisterska/Python",
};

app.use(express.static(path.join(__dirname, '')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname+'/index.html'));
})

app.post('/test', function (req, res) {
  console.log(req.body);

  PythonShell.run('similarMovies.py', options, function (err, results) {
    if (err) throw err;
    // results is an array consisting of messages collected during execution
    console.log('results: %j', results);
  });

})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
})

So, how it works. On clicking submit btn I am firing my node.js to run a python script and then console.log the results. Unfortunately I am getting errors, image at the end.

However, when I do not display my matrix and instead of it I write at the end of my Python

print "hello"
print 2

the results of code are parsed good.

Image of commond an results

What could be an issue? Some formating of this matrix? Even when I tried to set options in pythonshell instead of text to json or calling from pandas a function to_json nothing changed.

If needed I can provide a python code + 2 csv files which are needed.



via Pacxiu

No comments:

Post a Comment