refined snakeapi_service files
This commit is contained in:
parent
61650658eb
commit
0a143b3dc7
4 changed files with 127 additions and 29 deletions
|
|
@ -5,6 +5,7 @@ RUN pip install --no-cache-dir jupyter flask awscli flask_cors nbconvert nbforma
|
|||
|
||||
COPY entrypoint.sh .
|
||||
COPY notebooks ./notebooks
|
||||
COPY snakeapi_server.py .
|
||||
|
||||
RUN chmod +x entrypoint.sh
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,16 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
NOTEBOOK_DIR="notebooks"
|
||||
mkdir -p ${NOTEBOOK_DIR}
|
||||
mkdir -p "${NOTEBOOK_DIR}"
|
||||
|
||||
while true; do
|
||||
aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \
|
||||
s3 sync "s3://$BUCKET_NAME/$INSTANCE_PREFIX/notebooks/" "${NOTEBOOK_DIR}/"
|
||||
# fetch latest notebook
|
||||
aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \
|
||||
s3 sync "s3://$BUCKET_NAME/$INSTANCE_PREFIX/notebooks/" "${NOTEBOOK_DIR}/"
|
||||
|
||||
latest_notebook=$(ls -t ${NOTEBOOK_DIR}/*.ipynb | head -1)
|
||||
# convert to Python script for dynamic import
|
||||
latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -1)
|
||||
if [ -n "$latest_ipynb" ]; then
|
||||
jupyter nbconvert --to script "$latest_ipynb" --output "${NOTEBOOK_DIR}/notebook.py"
|
||||
fi
|
||||
|
||||
if [ -n "$latest_notebook" ]; then
|
||||
jupyter nbconvert --to notebook --execute --inplace --ExecutePreprocessor.timeout=0 "$latest_notebook"
|
||||
fi
|
||||
|
||||
sleep 5
|
||||
done
|
||||
# start the Flask server
|
||||
python snakeapi_server.py
|
||||
|
|
|
|||
44
deployment-service/snakeapi_service/snakeapi_server.py
Normal file
44
deployment-service/snakeapi_service/snakeapi_server.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# snakeapi_service/snakeapi_server.py
|
||||
import os
|
||||
import importlib.util
|
||||
from flask import Flask, request
|
||||
from flask_cors import CORS
|
||||
|
||||
# load notebook code as module
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"nb_module", os.path.join("notebooks", "notebook.py")
|
||||
)
|
||||
nb = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(nb)
|
||||
|
||||
handlers = {
|
||||
"info": nb.info,
|
||||
"start": nb.start,
|
||||
"move": nb.move,
|
||||
"end": nb.end
|
||||
}
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
def on_info():
|
||||
return handlers["info"]()
|
||||
|
||||
@app.route("/start", methods=["POST"])
|
||||
def on_start():
|
||||
handlers["start"](request.get_json())
|
||||
return "ok"
|
||||
|
||||
@app.route("/move", methods=["POST"])
|
||||
def on_move():
|
||||
return handlers["move"](request.get_json())
|
||||
|
||||
@app.route("/end", methods=["POST"])
|
||||
def on_end():
|
||||
handlers["end"](request.get_json())
|
||||
return "ok"
|
||||
|
||||
if __name__ == "__main__":
|
||||
port = int(os.environ.get("PORT", "3006"))
|
||||
app.run(host="0.0.0.0", port=port)
|
||||
Loading…
Add table
Add a link
Reference in a new issue