Skip to content

Commit

Permalink
Merge pull request #144 from martius-lab/fkloss/hockey-game-req
Browse files Browse the repository at this point in the history
Provide Apptainer images for comprl and comprl-web-reflex + some fixes
  • Loading branch information
luator authored Jan 13, 2025
2 parents 1b0e06a + 63175f3 commit 8a27d5a
Show file tree
Hide file tree
Showing 31 changed files with 145 additions and 15 deletions.
32 changes: 32 additions & 0 deletions comprl-hockey-game/container.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
bootstrap: docker
from: ubuntu:24.04

%arguments
COMPRL_GIT_BRANCH=main

%post
set -e
export DEBIAN_FRONTEND=noninteractive

echo "deb http://archive.ubuntu.com/ubuntu noble universe" >> /etc/apt/sources.list
apt-get update

apt-get install -y python3-dev python3-pip python3-full swig git

git clone https://github.com/martius-lab/teamproject-competition-server --depth 1 -b "{{ COMPRL_GIT_BRANCH }}" /comprl

python3 -m venv /venv
. /venv/bin/activate

pip install /comprl/comprl
pip install -r /comprl/comprl-hockey-game/requirements.txt


# cleanup
apt-get clean
pip cache purge


%runscript
. /venv/bin/activate
"$@"
3 changes: 3 additions & 0 deletions comprl-hockey-game/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
comprl
numpy
hockey @ git+https://[email protected]/martius-lab/laser-hockey-env.git@cc48daa135e2cca3e620b90431e57870f61d043c
64 changes: 53 additions & 11 deletions comprl-web-reflex/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
CompRL Web Interface with Reflex
================================

comprl-web-reflex is a re-implementation of the original web interface that was done
with Node.js.
It uses [reflex](reflex.dev), a Python framework for building web applications. The
benefit of using Python is that it directly use components of the comprl package (e.g.
for database access), thus reducing the need for redundant code. Further, it is easier
to maintain as everything is done with one language (Python).


**Important:** This is still work in progress and not yet ready for productive use!
comprl-web-reflex is a re-implementation of the original web interface that was
done with Node.js. It uses [reflex](reflex.dev), a Python framework for
building web applications. The benefit of using Python is that it directly use
components of the comprl package (e.g. for database access), thus reducing the
need for redundant code. Further, it is easier to maintain as everything is
done with one language (Python).


## Features
Expand All @@ -27,8 +24,8 @@ pip install ../comprl
pip install -r requirements.txt
```

The web interface needs an additional database table to manage login sessions. To add
it to an existing comprl database, run the following command:
The web interface needs an additional database table to manage login sessions.
To add it to an existing comprl database, run the following command:
```
python3 ./create_database_tables.py path/to/comprl/database.db
```
Expand All @@ -45,3 +42,48 @@ export COMPRL_REGISTRATION_KEY="12345"
reflex run
```

## Run with Apptainer

Build the Apptainer image:
```
apptainer build comprl-web-reflex.sif ./comprl-web-reflex.def
```

For running, you'll need to use a writable overlay (as Reflex will do some
installation and building stuff when being run). The example below simply uses
an overlay directory. It might be better to use an overlay image in some
cases, please refer to the Apptainer documentation.
```
sudo apptainer run -ec --overlay /tmp/overlay \
--bind /path/to/your/comprl-database:/comprl-config \
--env API_URL=http://your-server.de \
comprl-web-reflex.sif
```

In the example above, `/path/to/your/comprl-database` is expected to be a
directory on the host system, that contains the comprl database. It is
expected to be called "comprl.db". You can set a different name by adding the
following argument: ```
--env COMPRL_DB_PATH=/comprl-config/my_database.db
```
Note from the [Reflex container example, which was used as a base for
this](https://github.com/reflex-dev/reflex/tree/main/docker-example/simple-two-port):
> This container should be used with an existing load balancer or reverse proxy
> to route traffic to the appropriate port inside the container.
>
> For example, the following Caddyfile can be used to terminate TLS and forward
> traffic to the frontend and backend from outside the container.
>
> my-domain.com
>
> encode gzip
>
> @backend_routes path /_event/* /ping /_upload /_upload/*
> handle @backend_routes {
> reverse_proxy localhost:8000
> }
>
> reverse_proxy localhost:3000
56 changes: 56 additions & 0 deletions comprl-web-reflex/comprl-web-reflex.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Container to run comprl-web-reflex. Based on
# https://github.com/reflex-dev/reflex/tree/main/docker-example/simple-two-port

bootstrap: docker
from: python:3.12

%arguments
COMPRL_GIT_BRANCH=main

%post
set -e
export DEBIAN_FRONTEND=noninteractive

apt-get update
apt-get install -y git caddy redis-server

git clone https://github.com/martius-lab/teamproject-competition-server --depth 1 -b "{{ COMPRL_GIT_BRANCH }}" /comprl

python3 -m venv /venv
. /venv/bin/activate

pip install /comprl/comprl
pip install -r /comprl/comprl-web-reflex/requirements.txt

mkdir /comprl-config

# set up reflex
cd /comprl/comprl-web-reflex

# database path and registration key must be set (and db file must exist),
# otherwise `reflex export` will fail. However, the database doesn't need
# to be populated and the key can be overwritten at runtime
export COMPRL_DB_PATH="/comprl-config/comprl.db"
touch $COMPRL_DB_PATH
export COMPRL_REGISTRATION_KEY="BUILDTIME"

# Deploy templates and prepare app
reflex init
# Download all npm dependencies and compile frontend
reflex export --frontend-only --no-zip


# cleanup
apt-get clean
pip cache purge

%environment
export REDIS_URL=redis://localhost
export COMPRL_DB_PATH="/comprl-config/comprl.db"
export COMPRL_REGISTRATION_KEY="rl-lecture"

%runscript
. /venv/bin/activate
cd /comprl/comprl-web-reflex
redis-server --daemonize yes
reflex run --env prod "$@"
2 changes: 1 addition & 1 deletion comprl-web-reflex/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
reflex==0.6.6.post3
reflex==0.6.8
passlib
bcrypt
3 changes: 0 additions & 3 deletions comprl/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ dev = [
"ruff",
]

[tool.setuptools]
packages = ["comprl"]

[tool.setuptools.package-data]
comprl = ["py.typed"]

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8a27d5a

Please sign in to comment.