From e7058e2d2d5373e88e4c3f482bc446dd293577e7 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Tue, 26 Apr 2022 13:37:00 -0500 Subject: [PATCH] node calls basic python function --- app.js | 55 ++++++++++++++++--- parse_text/read_pdf.py => read_pdf.py | 5 +- ...ff Platfo.txt => Off Platform Project.txt} | 0 test.py | 4 ++ 4 files changed, 53 insertions(+), 11 deletions(-) rename parse_text/read_pdf.py => read_pdf.py (64%) rename recipe_text/{Off Platfo.txt => Off Platform Project.txt} (100%) create mode 100644 test.py diff --git a/app.js b/app.js index b92d558..3ab35b3 100644 --- a/app.js +++ b/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}`); }); \ No newline at end of file diff --git a/parse_text/read_pdf.py b/read_pdf.py similarity index 64% rename from parse_text/read_pdf.py rename to read_pdf.py index af63d66..5ce5611 100644 --- a/parse_text/read_pdf.py +++ b/read_pdf.py @@ -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() \ No newline at end of file diff --git a/recipe_text/Off Platfo.txt b/recipe_text/Off Platform Project.txt similarity index 100% rename from recipe_text/Off Platfo.txt rename to recipe_text/Off Platform Project.txt diff --git a/test.py b/test.py new file mode 100644 index 0000000..3d70315 --- /dev/null +++ b/test.py @@ -0,0 +1,4 @@ +import sys + +print("Can we get the thing to work?") +print("First argument: " + sys.argv[1]) \ No newline at end of file