modified snakeapi

This commit is contained in:
yoshi 2025-04-29 11:40:06 -07:00
parent 55b2398370
commit a3f0d83304
4 changed files with 49 additions and 20 deletions

View file

@ -1,19 +1,15 @@
FROM python:3.11-slim FROM python:3.11-slim
WORKDIR /app WORKDIR /app
# Install Jupyter, Flask, AWS CLI RUN pip install --no-cache-dir jupyter flask awscli flask_cors nbconvert nbformat
RUN pip install --no-cache-dir jupyter flask awscli
# Copy runner scripts and notebook (relative to this Dockerfile)
COPY entrypoint.sh . COPY entrypoint.sh .
COPY battlesnake_server.py . COPY battlesnake_server.py .
COPY notebook.ipynb . COPY notebook.ipynb .
RUN chmod +x entrypoint.sh RUN chmod +x entrypoint.sh
# Set and expose the port
ENV PORT=3006 ENV PORT=3006
EXPOSE 3006 EXPOSE 3006
# Start the notebook-to-Flask loop
CMD ["./entrypoint.sh"] CMD ["./entrypoint.sh"]

View file

@ -1,18 +1,22 @@
#!/usr/bin/env bash #!/usr/bin/env bash
last_mod=0
while true; do while true; do
# Fetch the latest notebook from S3 (common bucket + prefix)
aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \ aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \
s3 cp "s3://$BUCKET_NAME/$INSTANCE_PREFIX/notebook.ipynb" notebook.ipynb s3 sync "s3://$BUCKET_NAME/$INSTANCE_PREFIX" . --exclude "*" --include "notebook.ipynb"
# Execute all cells, including run_server(...) in the last cell if [ -f "notebook.ipynb" ]; then
jupyter nbconvert \ new_mod=$(stat -c %Y notebook.ipynb)
--to notebook \ else
--execute \ new_mod=0
--inplace \ fi
--ExecutePreprocessor.timeout=0 \
notebook.ipynb
if [ "$new_mod" -ne "$last_mod" ]; then
last_mod=$new_mod
jupyter nbconvert --to notebook --execute --inplace --ExecutePreprocessor.timeout=0 notebook.ipynb
echo "Notebook executed; restarting..." echo "Notebook executed; restarting..."
fi
sleep 1 sleep 1
done done

View file

@ -0,0 +1,17 @@
app = ""
kill_signal = "SIGINT"
kill_timeout = 5
[build]
dockerfile = "Dockerfile"
[env]
PORT = "3006"
[[services]]
internal_port = 3006
protocol = "tcp"
[[services.ports]]
handlers = ["http"]
port = 80

View file

@ -1,7 +1,11 @@
import os import os
import logging import logging
import typing import typing
from flask import Flask, request import json
import nbformat
from nbconvert import PythonExporter
import subprocess
from flask import Flask, request, jsonify
from flask_cors import CORS from flask_cors import CORS
def run_server(handlers: typing.Dict): def run_server(handlers: typing.Dict):
@ -26,10 +30,18 @@ def run_server(handlers: typing.Dict):
handlers["end"](request.get_json()) handlers["end"](request.get_json())
return "ok" return "ok"
@app.after_request @app.get("/notebook")
def identify_server(response): def get_notebook():
response.headers["server"] = "battlesnake/github/starter-snake-python" with open("notebook.ipynb", "r", encoding="utf-8") as f:
return response content = f.read()
return content, 200, {"Content-Type": "application/json"}
@app.post("/notebook")
def update_notebook():
notebook_json = request.get_json()
with open("notebook.ipynb", "w", encoding="utf-8") as f:
json.dump(notebook_json, f)
return {"status": "saved"}, 200
port = int(os.environ.get("PORT", "3006")) port = int(os.environ.get("PORT", "3006"))
logging.getLogger("werkzeug").setLevel(logging.ERROR) logging.getLogger("werkzeug").setLevel(logging.ERROR)