Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import python packages for script called with nodejs

I'm trying to use a python script with NodeJS. the code below works, but I need a way to do a 'pip install pandas' on 'npm install'

nodeJS

router.get('/', (req, res) => {
  const filePath = 'python/testing2.py' 
  const spawn = require("child_process").spawn;
  const pythonProcess = spawn('python3',[filePath, '-l']); 

  util.log('readingin')
  pythonProcess.stdout.on('data', (data) => { 
    const textChunk = data.toString('utf8');// buffer to string
    util.log(textChunk);
    res.json({'working': true, 'data': textChunk})
  });
});

python:

import sys 
from pandas import read_csv
from pandas import datetime    

def parser(x):
    return datetime.strptime('190'+x, '%Y-%m')    

print("Output from Python") 
series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
print (series)
sys.stdout.flush()
like image 836
user3662456 Avatar asked Nov 19 '25 08:11

user3662456


1 Answers

you can use postinstall property of scripts in your package.json file to perform such operation. Here's small demo code for that.(assuming pip command will work in you cmd)

"scripts": {
 "postinstall": "pip install pandas"
}
like image 187
Dhaval Chaudhary Avatar answered Nov 21 '25 22:11

Dhaval Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!