From 4abb783c33a62f5c07eea9c5fc89bf4305f77d0e Mon Sep 17 00:00:00 2001 From: Mathieu Loiseau <mathieu.loiseau@liris.cnrs.fr> Date: Tue, 30 Apr 2024 12:36:50 +0200 Subject: [PATCH] set working directory --- README.md | 29 ++++++++++++++++++++--------- src/app.py | 13 +++++++++++++ src/config.py | 5 +++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ac827d5..cb07af7 100755 --- a/README.md +++ b/README.md @@ -39,43 +39,54 @@ source lq-w-extr/bin/activate ### 3. Install dependencies -``` +```bash pip install -r requirements.txt ``` _Since `wiktextract` and its dependency `wikitextprocessor` are not regularly published as a Python package, it's a challenge to fix them to a specific version. From `requirements.txt`, the latest version will always be installed. Attention: This might mean that after reinstalling, the output schema of `wiktextract` might have slightly changed._ -### 4. Load templates from dump files +### 4. Congigure server +[config.py](https://gitlab.liris.cnrs.fr/lex-game/live-query-wiktextract/-/blob/main/src/config.py) contains : +* server settings (`host`, `port` and `debug` (boolean)) +* supported wiktionary language +* working directory (this can be useful if the server is launched by another server using absolute paths to handle virtual environment) +### 5. Load templates from dump files -Run the script `src/load_dumps.py` to load the most recent dumpfile (for each [supported language](https://gitlab.liris.cnrs.fr/lex-game/live-query-wiktextract/-/blob/main/src/config.py#L5)) into an sqlite database that will be used by `wiktextract`. +Run the script `src/load_dumps.py` to load the most recent dumpfile (for each [supported wiktionary language](https://gitlab.liris.cnrs.fr/lex-game/live-query-wiktextract/-/blob/main/src/config.py#L5)) into an sqlite database that will be used by `wiktextract`. -``` +```bash python src/load_dumps.py ``` -### 5. Start flask app +### 6. Start flask app -``` +```bash flask --app src/app.py run ``` You might want to use to ensure flask runs in your currently active virtual environment. -``` +```bash python -m flask --app src/app.py run ``` +You can run directly in your virtual environment using absolute paths (in case another server needs to launch this one in one command), example of such command +```bash +sh -c nohup /var/www/live-query-wiktextract/lq-w-extr/bin/python3 /var/www/live-query-wiktextract/src/app.py +``` + + ## Using Docker Alternatively the app can also be containerized using Docker. You still have to provide the dump files in `dumps/`. Then performs the two steps: -### 2. Build image +### 1. Build image ``` docker build -t live-query-wiktextract . ``` -### 3. Run image +### 2. Run image ``` docker run -p 5000:80 live-query-wiktextract diff --git a/src/app.py b/src/app.py index 618efdf..b2d9c8b 100755 --- a/src/app.py +++ b/src/app.py @@ -4,6 +4,17 @@ from flask_cors import CORS import config from wiktextract_wrapper import Wiktextract +# import logging +# logging.basicConfig(filename='flask.log', level=logging.DEBUG) + +# setting working directory +import os +try: + if config.wd != None: + os.chdir(config.wd) +except Exception as e: + print(f"Working directory for wiktextract_live_query was not specified (config.wd), setting ignored\n{e}") + app = Flask(__name__) CORS(app) @@ -46,6 +57,7 @@ def search(lang, word): @app.route("/search/<wiktlang>/<wordlang>/<word>/<format>", methods=["GET"]) def search_and_format(wiktlang, wordlang, word, format): + # app.logger.info('Received a request from /search/<wiktlang>/<wordlang>/<word>/<format>') if wiktlang not in config.supported_wiktlangs: return jsonify({"error": f"Language {wiktlang} not supported"}), 400 wiktextractor = Wiktextract(wiktlang, wordlang) @@ -88,3 +100,4 @@ def search_and_format(wiktlang, wordlang, word, format): if __name__ == "__main__": app.run(host=config.host, port=config.port, debug=config.debugging) + # app.run(host='0.0.0.0') diff --git a/src/config.py b/src/config.py index 80c9e30..026e4a8 100755 --- a/src/config.py +++ b/src/config.py @@ -1,5 +1,6 @@ host = "0.0.0.0" -port = 80 -debugging = True +port = 5000 +debugging = False #True supported_wiktlangs = ["en", "fr", "de", "es", "ru"] +wd = "/var/www/live-query-wiktextract" -- GitLab