diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c7b6a78..457dce1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: with: python-version: '3.8' architecture: x64 - - run: make build-game + - run: make build - run: pip3 install poetry - run: poetry build - run: poetry publish --username=__token__ --password=${{ secrets.PYPI_TOKEN }} diff --git a/.gitignore b/.gitignore index c4216b7..f93e95a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ __pycache__/ *.py[cod] *$py.class -# src/train_invaders/game.wasm # C extensions *.so diff --git a/Dockerfile b/Dockerfile.game similarity index 72% rename from Dockerfile rename to Dockerfile.game index cd60f3e..4394ed5 100644 --- a/Dockerfile +++ b/Dockerfile.game @@ -1,6 +1,8 @@ -FROM ubuntu as build +# Build game +FROM ubuntu as game_builder RUN apt-get update && \ apt-get install -y git python3 && \ + apt-get install libatomic1 && \ mkdir /build && \ cd /build && \ git clone https://github.com/emscripten-core/emsdk.git @@ -15,6 +17,9 @@ ENV PATH="${PATH}:/build/emsdk/upstream/bin" RUN cd /project && \ chmod +x /build/emsdk/emsdk_env.sh && \ make compile-game +RUN cd /project/public && \ + base64 game.wasm -w 0 > game.txt +# Export game FROM scratch AS export -COPY --from=build /project/public/game.wasm . +COPY --from=game_builder /project/public/game.txt . diff --git a/Dockerfile.view b/Dockerfile.view new file mode 100644 index 0000000..4beeaa7 --- /dev/null +++ b/Dockerfile.view @@ -0,0 +1,15 @@ +# Build view +FROM node:12 as view_builder +RUN mkdir -p /app +WORKDIR /app +COPY ./src/view/package.json /app +COPY ./src/view/package-lock.json /app +RUN npm install +COPY ./src/view /app +RUN npm run build +RUN cd /app/dist && \ + base64 index.html -w 0 > view.txt + +# Export view.txt +FROM scratch AS export +COPY --from=view_builder /app/dist/view.txt . diff --git a/Dockerfile.view.dockerignore b/Dockerfile.view.dockerignore new file mode 100644 index 0000000..158d5b6 --- /dev/null +++ b/Dockerfile.view.dockerignore @@ -0,0 +1,4 @@ +src/assets +src/game +src/train_invaders +src/view/node_modules diff --git a/Makefile b/Makefile index e11b51c..c687518 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ SHELL := /bin/bash DEFAULT_VERSION=1.0.0 -# Build wasm game +# Compile wasm game compile-game: cd src && \ clang \ @@ -24,7 +24,14 @@ compile-game: # Build game build-game: - DOCKER_BUILDKIT=1 docker build --file Dockerfile --output ./src/train_invaders . + DOCKER_BUILDKIT=1 docker build --file Dockerfile.game --output ./src/view/src/assets . + +# Build view +build-view: + DOCKER_BUILDKIT=1 docker build --file Dockerfile.view --output ./src/train_invaders . + +# Build TrainInvaders +build: build-game build-view # Install dependencies install-deps: diff --git a/README.md b/README.md index d36dbd0..6682c6d 100644 --- a/README.md +++ b/README.md @@ -77,10 +77,11 @@ NO. The game will start and be played **in parallel to the training** and will e Have an awesome idea for a new feature? PRs are more than welcome! 1. Clone the project -2. Implement your idea -3. Made changes in the game? re-compile it with `make build-game` -4. Run `pip install . --upgrade` -5. Test the changes in your notebook +2. Run `make build-game` to get a local and compiled copy of the game (if not exists) +2. Enter `src/view` directory and run `npm run serve` to run the local environment +2. Implement your ideas +3. Made changes in the game (C files)? Re-run `make build-game` from root dir and check them out +5. Enter root directory, run `make build`, `pip install . --upgrade` and test the changes in your notebook 6. PR us! ## Thanks to 🙏 @@ -90,7 +91,9 @@ Have an awesome idea for a new feature? PRs are more than welcome! [Vue](https://github.com/vuejs/vue) for the awesome FE framework. -## You may also like... ❤️ +## You may also ❤️ [Aporia](https://www.aporia.com/?utm_source=train-invaders&utm_medium=docs&utm_campaign=train-invaders) - Customized monitoring for your ML models. +[MLNotify](https://mlnotify.aporia.com/) - Get notified when training is complete. + [MLOps Toys](https://mlops.toys/?utm_source=train-invaders&utm_medium=docs&utm_campaign=train-invaders) - A curated list of useful MLOps tools. diff --git a/pyproject.toml b/pyproject.toml index d7a2cc4..435db3f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "train_invaders" -version = "v1.0.2" +version = "v1.0.3" description = "Jupyter Notebook + Space Invaders!?" authors = ["Aporia"] readme = "README.md" @@ -13,7 +13,7 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.6" -ipython = "^7.0.0" +ipython = "<=7.16" [tool.poetry.dev-dependencies] flake8 = "^3.7.9" diff --git a/src/train_invaders/assets/aporia-logo.png b/src/train_invaders/assets/aporia-logo.png deleted file mode 100644 index ea1e39f..0000000 Binary files a/src/train_invaders/assets/aporia-logo.png and /dev/null differ diff --git a/src/train_invaders/assets/bg.png b/src/train_invaders/assets/bg.png deleted file mode 100644 index 022f342..0000000 Binary files a/src/train_invaders/assets/bg.png and /dev/null differ diff --git a/src/train_invaders/assets/heart.png b/src/train_invaders/assets/heart.png deleted file mode 100644 index 4181362..0000000 Binary files a/src/train_invaders/assets/heart.png and /dev/null differ diff --git a/src/train_invaders/assets/success.png b/src/train_invaders/assets/success.png deleted file mode 100644 index f68cd43..0000000 Binary files a/src/train_invaders/assets/success.png and /dev/null differ diff --git a/src/train_invaders/index.html b/src/train_invaders/index.html deleted file mode 100644 index f2feadb..0000000 --- a/src/train_invaders/index.html +++ /dev/null @@ -1,43 +0,0 @@ - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/train_invaders/index.js b/src/train_invaders/index.js index 95c80ad..d0b96c2 100644 --- a/src/train_invaders/index.js +++ b/src/train_invaders/index.js @@ -13,6 +13,7 @@ if (typeof window !== 'undefined' && 'Jupyter' in window) { if (!cell.__gameInitiated) { cell.__isRunning = true + // On cell execution stop Jupyter.notebook.events.on('finished_execute.CodeCell', function (evt, data) { if (data.cell.cell_id === cellId) { @@ -21,12 +22,15 @@ if (typeof window !== 'undefined' && 'Jupyter' in window) { cell.__isRunning = false } }); + // On cell execution start Jupyter.notebook.events.on('execute.CodeCell', function (evt, data) { if (data.cell.cell_id === cellId) { cell.__isRunning = true } }); + + // Notify view when training is finished window.addEventListener("message", event => { if (event.data.event === `gameTrainingState`) { if (!cell.__isRunning) { @@ -35,10 +39,12 @@ if (typeof window !== 'undefined' && 'Jupyter' in window) { } } }); + cell.__gameInitiated = true } } +// Handle game closing if(!window.__isThereGameCloseListener) { function gameCloseHandler(event) { if (event.data.event === `gameClose`) { @@ -52,11 +58,6 @@ if(!window.__isThereGameCloseListener) { window.__isThereGameCloseListener = true } -// If online, run the game -if(navigator.onLine) { - getGameIframe().contentDocument.write(atob("$$GAME_HTML_BASE64$$")) - getGameIframe().focus() -} else { - getGameIframe().height = 25 - getGameIframe().contentDocument.write(`[TrainInvaders] No internet connection :(`) -} +// Inject base64 view to the iframe +getGameIframe().contentDocument.write(atob("$$GAME_HTML_BASE64$$")) +getGameIframe().focus() diff --git a/src/train_invaders/start.py b/src/train_invaders/start.py index 2e18c50..2a572fb 100644 --- a/src/train_invaders/start.py +++ b/src/train_invaders/start.py @@ -1,4 +1,3 @@ -import base64 from pathlib import Path import sys from types import FrameType @@ -7,7 +6,6 @@ from IPython.display import display, HTML, Javascript train_invaders_dir = Path(__file__).parent -assets_dir = train_invaders_dir / "assets" iframe_html = """