1

I created a python script that parses a website(IMDB) and organizes it into a dataframe. I also have a node.js app that allows me to find a variable (movie ID based on movie name in the code called pyvar) that I would include in the python script. So how can I include this variable that I get after running javascript app into python script, run the script and then send the result back to the node.js app? (that would be dataframe converted to lets say json)

Node.js app

var express = require("express")
var app = express()
var request = require("request")
app.set("view engine", "ejs")

app.get("/", function(req, res){
    res.render("search")
})

app.get("/results", function(req, res){
    var query = req.query.search
    var url = "http://www.omdbapi.com/?s=" + query + "&apikey=thewdb"
    
    request(url, function(error, response, body){
        if(!error && response.statusCode == 200){
            var data = JSON.parse(body)
            res.render("results", {data: data})
            var pyvar = data["Search"][0]["imdbID"]
        }    
    })
})

app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Movie App has started!!!");
})

The python script in a nutshell looks like this:

url = 'website.org/' + pyvar + '/blah'
parse(url)
return dataframe

After that I would send the dataframe in some form back to the node.js app and display the results or even better if it would allow me to download the dataframe converted to xlsx but it might be too complicated.

1
  • 1
    Scripts usually take input in the form of parameters or read from stdin. Scripts should output to stdout. If you update your script to work this way you can use nodejs.org/api/child_process.html. Commented Oct 16, 2017 at 21:38

1 Answer 1

1

You can use child_process spawn to execute your python script, as Felix Kling suggest in his comment, and return it result to your nodejs app. Then you could use a package like node-xlsx to transform the data to an Excel file.

Something like that:

app.js

// ...
const { spawn } = require('child_process');
const xlsx = require('node-xlsx');

// ...    
app.get("/results", (req, res) => {
  let query = req.query.search;
  let url = "http://www.omdbapi.com/?s=" + query + "&apikey=thewdb";

  request(url, (error, response, body) => {
    if (!error && response.statusCode == 200) {
      let data = JSON.parse(body);
      let pyvar = data["Search"][0]["imdbID"];

      // Call the python script
      let pythonScript = spawn('./script.py', [pyvar]);

      pythonScript.stdout.on('data', data => {
        // Here transform the datatable to xls sheet
        let xlsx = xlsx.build([{ name: "myXlsxSheet", data: data.toString() }])
        // And send the file
        res.end(new Buffer(xlsx, 'binary'));
      });
    }
  })

})

// ...

script.py

#!/usr/bin/python

import sys
import pandas

pyvar = sys.argv[1]

# Here the script that parse the website
url = 'website.org/' + pyvar + '/blah'
data = parse(url)

print pandas.DataFrame(data)
Sign up to request clarification or add additional context in comments.

2 Comments

Actually I took similar appriach but used the python shell library but the basics are there
I did not know about python-shell which seems even more handy for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.