Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# What never goes into an image: the key, the environment, data and builds.
.env
.env.*
!.env.example
secrets
*.map_key
.git
.venv
.venv-core
data
_site
dist
**/__pycache__
**/node_modules
**/.ipynb_checkpoints
demo/web/.next
.pytest_cache
.mypy_cache
.ruff_cache
htmlcov
47 changes: 47 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,50 @@ FIRMS_MAP_KEY=
# --- Bulletins with Claude (extra firms-client[claude]) ---------------------
# Only for ClaudeWriter: drafts of alert bulletins. Paid API.
# ANTHROPIC_API_KEY=

# --- Docker (docker compose up, make up) -----------------------------------
# In the containers the data live in /data (DATA_ROOT above is for running
# without Docker). They go to the volume firms-data, or to a folder of this
# machine written here (it must be writable by the user of the image, uid 1000).
# FIRMS_DATA_VOLUME=/srv/firms-client
# The MAP_KEY as a file instead of FIRMS_MAP_KEY: the folder mounted read-only
# at /run/secrets/firms, and the name of the file in it (one line, the key).
# The key is never copied into an image.
FIRMS_SECRETS_DIR=./secrets
FIRMS_MAP_KEY_NAME=map_key
# The ports, on 127.0.0.1 only: the page, the notebooks, the MCP server.
FIRMS_UI_PORT=3005
FIRMS_NOTEBOOK_PORT=8890
FIRMS_MCP_PORT=8769
# The token of JupyterLab. Empty: a random one, in `docker compose logs notebook`.
FIRMS_NOTEBOOK_TOKEN=

# --- The page of a Comune (http://127.0.0.1:3005) --------------------------
# The Comune, in decimal degrees: here Palermo, which is also the default.
# The page reads one area around it: the fires of the last days (with a
# MAP_KEY), of every archive year, and how far to trust a detection there.
FIRMS_UI_LAT=38.1157
FIRMS_UI_LON=13.3615
# The radius of the area, in km, 1 to 30 (default 10).
FIRMS_UI_RADIUS_KM=
# The name in the header.
FIRMS_UI_PLACE="Comune di Palermo"
# The country of the archives, as FIRMS writes it (default Italy).
FIRMS_UI_COUNTRY=
# The source of the last days: VIIRS_SNPP_NRT (default), VIIRS_NOAA20_NRT,
# VIIRS_NOAA21_NRT or MODIS_NRT.
FIRMS_UI_NRT_SOURCE=
# 1 (default): download the ESA WorldCover tiles of the area (about 60 MB
# each, once) to say what land burnt and how far to trust a point. 0: no.
FIRMS_UI_LAND_COVER=1
# The zoom the map opens at, 5 to 16 (default: the whole area).
FIRMS_UI_ZOOM=
# The language: it or en (default it). The visitor can switch.
FIRMS_UI_LANG=it
# Who may show the page in an iframe, as a CSP frame-ancestors list, such as
# "https://www.comune.palermo.it". Default: this site only.
FIRMS_UI_FRAME_ANCESTORS=
# The tiles of the background map, and their credit (default: OpenStreetMap,
# which then sees the address of every visitor).
FIRMS_UI_TILES=
FIRMS_UI_TILES_CREDIT=
41 changes: 40 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
with:
python-version: ${{ matrix.python }}
enable-cache: true
- run: uv sync --locked --all-extras
- run: uv sync --locked --all-extras --group demo
- run: uv run ruff check
- run: uv run ruff format --check
- run: uv run mypy
Expand Down Expand Up @@ -81,3 +81,42 @@ jobs:
python-version: "3.12"
enable-cache: true
- run: make docs

# The two notebooks that need no network, on the crops of the benchmark.
notebooks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Install uv
uses: astral-sh/setup-uv@v7
with:
python-version: "3.12"
enable-cache: true
- run: make notebooks-offline

# The page of a Comune as a Comune installs it: build, start without a key,
# and the API and the page must answer.
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
# No WorldCover download in CI: the page must answer without it.
- run: cp .env.example .env && sed -i 's/^FIRMS_UI_LAND_COVER=1/FIRMS_UI_LAND_COVER=0/' .env
- run: docker compose up -d --build api ui
- name: The API and the page answer
run: |
for i in $(seq 1 60); do
if docker compose exec -T api python -c "import urllib.request; urllib.request.urlopen('http://localhost:8010/api/health', timeout=4)" \
&& curl -fsS -o /dev/null http://127.0.0.1:3005/; then
break
fi
sleep 3
done
docker compose exec -T api python -c "import urllib.request, json; h = json.load(urllib.request.urlopen('http://localhost:8010/api/health')); print(h['map_key'], h['data']['state']); assert h['map_key'] is False"
curl -fsS http://127.0.0.1:3005/api/health
curl -fsS http://127.0.0.1:3005/ | grep -q "firms-client"
- name: Logs
if: always()
run: docker compose logs --tail=100
- run: docker compose down -v
if: always()
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ htmlcov/
_site/
.env
/data/
secrets/
*.map_key
node_modules/
.next/
next-env.d.ts
*.tsbuildinfo
.ipynb_checkpoints/
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ All notable changes to this project are documented here. The format follows

## [Unreleased]

### Added

- The page of a Comune, with Docker (`compose.yaml`, `Dockerfile`,
`demo/api.py`, `demo/web`): the fires of the last days (with a MAP_KEY) and
of each archive year in an area, the largest of them and what land burnt,
how far to trust a detection at a point, «Non è un'allerta» always in view.
Bootstrap Italia and Leaflet from npm, Italian and English, configured from
`.env` and the URL, embeddable in an iframe. Without a key it shows the
archives only, and says so. Ports on 127.0.0.1; the MAP_KEY only from
`.env` or a mounted file, never in an image, a log or an answer (tested).
- Containers for the notebooks (JupyterLab, profile `notebook`), the MCP
server over HTTP (profile `mcp`) and the command line (profile `tools`).
- `firms-mcp --http PORT --host HOST`: the MCP server over streamable HTTP.
- Four notebooks (#21): a national fire history, one season against the
others, how much to trust a detection, per-cell features for a model.
`make notebooks`; `make notebooks-offline` runs the two that need no
network, in CI.
- CI builds and starts the Docker stack and checks the API and the page.
- `docs/riuso.md`: installing the page for a Comune.

## [0.8.0] - 2026-11-26

### Added
Expand Down
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# firms-client with every extra: the library, the command line, the MCP server,
# the API behind the page of a Comune (demo/api.py) and the notebooks. The page
# itself is another image, built from demo/web.
#
# docker compose up the page, on http://127.0.0.1:3005
# docker build -t firms-client:local .
#
# The MAP_KEY is never copied in (.dockerignore): it comes at run time, as a
# variable from .env or as a file mounted read-only (see compose.yaml).
FROM python:3.13-slim

COPY --from=ghcr.io/astral-sh/uv:0.13 /uv /bin/uv

# rasterio brings its own GDAL, which needs libexpat from the system.
RUN apt-get update && apt-get install -y --no-install-recommends libexpat1 \
&& rm -rf /var/lib/apt/lists/*

ENV UV_PROJECT_ENVIRONMENT=/opt/venv \
UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
UV_NO_CACHE=1 \
PATH=/opt/venv/bin:$PATH \
PYTHONUNBUFFERED=1 \
MPLBACKEND=Agg \
DATA_ROOT=/data

WORKDIR /app

# The dependencies first: this layer is rebuilt only when they change.
COPY pyproject.toml uv.lock ./
RUN uv sync --locked --no-dev --all-extras --group demo --group examples --no-install-project

COPY README.md LICENSE ./
COPY src src
RUN uv sync --locked --no-dev --all-extras --group demo --group examples --no-editable

# Fail the build, not the first run, if a system library is missing.
RUN python -c "import rasterio, mcp, starlette, uvicorn, matplotlib, firms_client"

COPY demo/api.py demo/notebook.sh demo/
COPY benchmarks benchmarks
COPY notebooks notebooks

RUN useradd --create-home firms && mkdir -p /data \
&& chown firms /data /app/notebooks
USER firms
VOLUME /data

EXPOSE 8010
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8010/api/health', timeout=4)"]

CMD ["uvicorn", "--app-dir", "demo", "api:app", "--host", "0.0.0.0", "--port", "8010"]
39 changes: 36 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DEFAULT_GOAL := help
.PHONY: help check lint format typecheck test test-core live build docs bench faithful bench-data clean
.PHONY: help check lint format typecheck test test-core live build docs bench faithful bench-data notebooks notebooks-offline up demo down logs clean

help: ## List the targets
@awk 'BEGIN {FS = ":.*## "} /^[a-z-]+:.*## / {printf " \033[36m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
Expand All @@ -15,10 +15,10 @@ format: ## Format the code and fix what ruff can
uv run ruff check --fix

typecheck: ## mypy --strict
uv run --all-extras mypy
uv run --all-extras --group demo mypy

test: ## pytest with coverage, every extra installed (no key, no network)
uv run --all-extras pytest --cov --cov-report=term-missing
uv run --all-extras --group demo pytest --cov --cov-report=term-missing

# What most users install. Its own environment, so .venv keeps the extras:
# the tests of the extras skip, the rest must pass.
Expand Down Expand Up @@ -56,6 +56,39 @@ docs: ## The site with the API reference, in _site/
cp -r docs/images _site/images
uv run --all-extras --group docs pdoc firms_client -o _site/api

# The notebooks are stored with their outputs. 01 and 02 download the yearly
# archives (no key, about 2 MB a year, once); 03 and 04 run on the crops of
# the benchmark, offline, as CI does.
notebooks: ## Run every notebook again, outputs saved in place
cd notebooks && for nb in *.ipynb; do \
echo "== $$nb"; \
uv run --all-extras --group examples jupyter nbconvert --to notebook --execute --inplace "$$nb" || exit 1; \
done

OFFLINE = 03_how_much_to_trust.ipynb 04_cells_for_a_model.ipynb
notebooks-offline: ## Run the notebooks that need no network (03, 04), as CI does
cd notebooks && for nb in $(OFFLINE); do \
echo "== $$nb"; \
uv run --all-extras --group examples jupyter nbconvert --to notebook --execute --stdout "$$nb" > /dev/null || exit 1; \
done

# Docker: every setting is in .env (cp .env.example .env). Ports on 127.0.0.1 only.
up: ## The page, the notebooks and the MCP server in Docker (http://127.0.0.1:3005)
docker compose --profile notebook --profile mcp up -d --build
@echo "page: http://127.0.0.1:$${FIRMS_UI_PORT:-3005}"
@echo "notebooks: http://127.0.0.1:$${FIRMS_NOTEBOOK_PORT:-8890} (token: make logs)"
@echo "MCP: http://127.0.0.1:$${FIRMS_MCP_PORT:-8769}/mcp"

demo: ## Only the page (the API and the UI): no key needed
docker compose up -d --build api ui
@echo "page: http://127.0.0.1:$${FIRMS_UI_PORT:-3005}"

down: ## Stop the containers (the downloaded archives stay in the volume)
docker compose --profile notebook --profile mcp --profile tools down

logs: ## Follow the logs of the containers
docker compose --profile notebook --profile mcp logs -f --tail=50

clean: ## Remove what the targets build (downloaded archives stay in the data directory)
rm -rf dist _site .venv-core htmlcov .coverage .pytest_cache .mypy_cache .ruff_cache
find . -name __pycache__ -type d -prune -exec rm -rf {} +
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ One dependency: `httpx`. Website: **[agentengineering.it/firms-client](https://a
- [What this library does](#what-this-library-does)
- [Who is it for](#who-is-it-for)
- [Quick start](#quick-start)
- [The page of a Comune, with Docker](#the-page-of-a-comune-with-docker)
- [Where downloads are saved](#where-downloads-are-saved)
- [Not every hotspot is a fire](#not-every-hotspot-is-a-fire)
- [How it works](#how-it-works)
Expand Down Expand Up @@ -260,6 +261,56 @@ Sicily compare with 2025?"*: it calls `fire_history` with Sicily's bbox.

---

## The page of a Comune, with Docker

For a Comune that wants the fires on its own site, with nothing to install
but Docker:

```bash
cp .env.example .env # the Comune: FIRMS_UI_LAT, FIRMS_UI_LON, FIRMS_UI_PLACE
docker compose up -d # the page: http://127.0.0.1:3005
```

The page reads one area around the Comune (`FIRMS_UI_RADIUS_KM`, 10 km by
default) and shows, in Italian or English, with Bootstrap Italia:

- the fires of the **last 7 days**, from the area API, when `.env` has a
`FIRMS_MAP_KEY`. Without a key the page shows the yearly archives only,
and says so: the last days are *not measured*, not zero;
- the fires of **each archive year** in the area, the largest of them and
what land burnt (ESA WorldCover), and a bar per year;
- **how far to trust** a detection at any point clicked (`facts.place`);
- «Non è un'allerta» / «Not an alert» always in view, the presence-only
note, and the NASA and ESA credits.

At startup the API downloads the archives of the country (Italy: about 2 MB a
year) and the WorldCover tiles of the area (about 60 MB each), once, into the
volume `firms-data`. The ports listen on 127.0.0.1 only: publish the page
through a proxy with HTTPS. `FIRMS_UI_FRAME_ANCESTORS` lets the Comune's site
show it in an iframe (`?embed=1` drops the header). Every answer of the API
(`demo/api.py`, `/api/health`, `/api/season`, `/api/recent`, `/api/history`,
`/api/trust`) is a `facts` answer.

The MAP_KEY comes from `.env` or from a file in `./secrets` mounted
read-only (`FIRMS_MAP_KEY_NAME`); it is never copied into an image, and
never written to a log or an answer (a test checks both).

Two more containers, on request:

```bash
docker compose --profile notebook up -d # JupyterLab on the notebooks: http://127.0.0.1:8890
docker compose --profile mcp up -d # the MCP server over HTTP: http://127.0.0.1:8769/mcp
docker compose run --rm cli --help # the command line
```

`make up`, `make demo`, `make down` and `make logs` do the same. The
notebooks are in [`notebooks/`](notebooks/README.md): a national fire
history, one season against the others, how much to trust a detection, and
per-cell features for a model. Installing it for a Comune, licences
included: [`docs/riuso.md`](docs/riuso.md).

---

## Where downloads are saved

Everything the library downloads goes to **one data directory**, and is
Expand Down
Loading
Loading