looking for some solutions? You are welcome.

SOLVED: node.js child process python pands Unexpected end of JSON input

Adminstrator Fermi:

I am running node.js server and want to run python data analysis file in node.js server using "child_process"

I want to communicate between node.js and python with "json". Problems are when I tried to send large amount of json from python to node.js, there is an indent error such as below. Therefore, "SyntaxError: Unexpected end of JSON input" error is called when JSON.parse.

[{"smp_price":89.3823248294},{"smp_price":89.3624296981},{"s
    mp_price":89.3425345667},{"smp_price":89.3226394354},{"smp_price":89.302744304},{"smp_price":89.2828491727},{"smp_price":89.2828491727},{"smp_price":89.2629540413},{"smp_price":48.8719007985}]

This problem does not appear when the data is significantly small. But appears in large data(more than 100 arrays. What shoud I do? Is there any other way to run python data analysis in node.js server?

server.js
const cp= require("child_process")
const express = require('express');
const app = express();
const path=require("path")

app.use('/',express.static(path.resolve(__dirname, './public')));
app.get('/',(req,res)=>{
  res.send("thest")
});

app.get('/python',(req,res)=>{
  res.setHeader('Content-Type', 'application/json');
  console.log("python url!")
  let child=cp.spawn("python",[path.resolve(__dirname,"../data-analysis/simulation.py")]);
  let sent_data= [1,2,3,4,5,6,7,8];
  child.stderr.on('data',(err)=>{
    console.log(`error:${err}`)
  })
  child.stdin.write(JSON.stringify(sent_data));
  child.stdin.end();
  child.stdout.on('data',(data)=>{
    let input=data.toString();
    console.log(input)
    let parsed=JSON.parse(input);
    res.json(parsed)
  })
})

app.listen(3000,()=>console.log("app is running!"))


data-analysis.py
model=smf.ols(formula = 'smp_price~oil_price+elec_supply', data = data).fit()
forecast=fetch_forecast(["WEO_450","supply7"])
forecast=forecast.rename(columns={"WEO_450":"oil_price","supply7":"elec_supply"})

def make_scenario(model,forecast):
    pred=model.predict(forecast)
    output=pd.DataFrame({'smp_price':pred})
    output=output.astype(float)
    return(output)

pred=make_scenario(model,forecast)
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

def read_in():
    lines =sys.stdin.readlines()
    parsed=json.loads(lines[0])
    print(parsed)

def main(input):
    export_data=input.to_json(orient='records',date_format="iso")
    print(export_data)

if __name__ == '__main__':
    main(pred)



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent