node calls basic python function

This commit is contained in:
2022-04-26 13:37:00 -05:00
parent 8e65dcb06d
commit e7058e2d2d
4 changed files with 53 additions and 11 deletions

55
app.js
View File

@@ -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}`);
});

View File

@@ -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()

4
test.py Normal file
View File

@@ -0,0 +1,4 @@
import sys
print("Can we get the thing to work?")
print("First argument: " + sys.argv[1])