node calls basic python function
This commit is contained in:
55
app.js
55
app.js
@@ -1,16 +1,53 @@
|
||||
const fs = require('fs');
|
||||
const http = require('http');
|
||||
|
||||
http.createServer((req, res) => {
|
||||
res.write("Server is active.");
|
||||
res.end();
|
||||
}).listen(8080);
|
||||
|
||||
const express = require('express');
|
||||
const spawn = require('child_process').spawn;
|
||||
const read_pdf = spawn('python', ['./parse_text/read_pdf.py']);
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
read_pdf.stdout.on('data', (data) => {
|
||||
fs.write('./recipe_text/caught.txt', 'caught', err => {
|
||||
if (err) console.log(err);
|
||||
const pyPromise = async (data) => {
|
||||
const python = spawn('python', ['test.py']);
|
||||
|
||||
python.stdout.on("data", (data) => {
|
||||
resolve(data.toString());
|
||||
});
|
||||
|
||||
python.stderr.on("data", (data) => {
|
||||
reject(data.toString());
|
||||
});
|
||||
}
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send("Python stuff?")
|
||||
})
|
||||
|
||||
app.get('/:name', (req, res) => {
|
||||
const { name } = req.params;
|
||||
const python = spawn('python3', ['test.py', name]);
|
||||
|
||||
python.stdout.on('data', (data) => {
|
||||
res.send(data.toString());
|
||||
})
|
||||
|
||||
python.stderr.on("data", (data) => {
|
||||
res.send(data.toString());
|
||||
})
|
||||
})
|
||||
|
||||
// app.get('/', (req, res) => {
|
||||
// let dataToSend;
|
||||
// const python = spawn('python', ['test.py']);
|
||||
// python.stdout.on('data', (data) => {
|
||||
// dataToSend = data.toString();
|
||||
// console.log('caught');
|
||||
// });
|
||||
// python.on('close', (code) => {
|
||||
// console.log(`All close with code ${code}`);
|
||||
// res.send(dataToSend);
|
||||
// })
|
||||
// })
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Listening on port ${PORT}`);
|
||||
});
|
||||
@@ -6,8 +6,9 @@ def read_PDF(path):
|
||||
text = ""
|
||||
for page in doc:
|
||||
text += page.get_text()
|
||||
with open(f'../recipe_text/{text[:20]}.txt', 'w') as f:
|
||||
with open(f'recipe_text/{text[:20]}.txt', 'w') as f:
|
||||
f.write(text)
|
||||
|
||||
read_PDF('../recipes/sample_recipe.pdf')
|
||||
read_PDF('recipes/TheBestOfBaseballAwards.pdf')
|
||||
|
||||
sys.stdout.flush()
|
||||
Reference in New Issue
Block a user