From 2a50b9c58157376a3729e5cb514be5d41fe43fd4 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Tuan Date: Sun, 18 Aug 2024 16:30:46 +0700 Subject: [PATCH] Update scripts, PyProcess.js --- backend/install.sh | 2 ++ backend/nodejs/scripts/start.sh | 2 ++ backend/nodejs/src/classes/PyProcess.js | 7 +++++-- backend/python/scripts/install.sh | 7 ++++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/install.sh b/backend/install.sh index 4fa064e..5eb9fd2 100644 --- a/backend/install.sh +++ b/backend/install.sh @@ -23,8 +23,10 @@ infoln "Done!" cd /home/backend # Create `temp` and `uploads` folders +infoln "Create \`temp\` and \`uploads\` folders..." mkdir temp mkdir uploads +infoln "Done!" # Start server infoln "Booting server..." diff --git a/backend/nodejs/scripts/start.sh b/backend/nodejs/scripts/start.sh index 35a71e4..b4124d1 100644 --- a/backend/nodejs/scripts/start.sh +++ b/backend/nodejs/scripts/start.sh @@ -8,6 +8,8 @@ export NVM_DIR="$HOME/.nvm" # Change directory to `nodejs` cd /home/backend/nodejs +export PYTHON_PATH="/home/backend/python/venv/bin/python" + # Start NodeJS Server npm start diff --git a/backend/nodejs/src/classes/PyProcess.js b/backend/nodejs/src/classes/PyProcess.js index f4d8841..cdafd68 100644 --- a/backend/nodejs/src/classes/PyProcess.js +++ b/backend/nodejs/src/classes/PyProcess.js @@ -16,7 +16,9 @@ export class PyProcess { DatatableImageToText: root + "/python/datatable_image_to_text.py", }; - constructor() {} + constructor() { + this.pythonPath = process.env.PYTHON_PATH || "python3"; + } /** * Dùng phương thức này để tạo ra một python executer. @@ -26,10 +28,11 @@ export class PyProcess { */ exec(script, ...args) { // Use python3 (Linux) - const py_process = spawn("python3", [ + const py_process = spawn(this.pythonPath, [ PyProcess.ScriptPaths[script], ...args, ]); + try { return new Promise(function (res, rej) { py_process.stdout.on("data", (data) => { diff --git a/backend/python/scripts/install.sh b/backend/python/scripts/install.sh index df105e9..8b6a188 100644 --- a/backend/python/scripts/install.sh +++ b/backend/python/scripts/install.sh @@ -15,9 +15,14 @@ printf "Python has been just installed: $(python3 --version)" apt install python3-pip printf "PIP has just been installed: $(node -v)" -# Change directory to `backend` +# Change directory to `/home/backend/python` cd /home/backend/python +## Install virtualenv and create virtual environment +pip3 install virtualenv +virtualenv venv +source venv/bin/activate + ## Install packages in `requirements.txt` pip3 install -r requirements.txt