Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

examples: add Devenv usage examples #338

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions examples/python-web-app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.geonix/*
.result
.venv/*
src/python_app/__pycache__/*
13 changes: 13 additions & 0 deletions examples/python-web-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# devenv
.devenv*
devenv.local.nix

# direnv
.direnv

# pre-commit
.pre-commit-config.yaml

# python app
src/python_app/__pycache__/*
tests/__pycache__/
25 changes: 25 additions & 0 deletions examples/python-web-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Get python-app-base image:
# nix build .#container
# docker load < ./result

# Build image:
# docker build -t python-app:latest

FROM python-app-base

RUN mkdir -p /opt/python-app
WORKDIR /opt/python-app

COPY src ./src
COPY pyproject.toml poetry.* README.md ./

RUN poetry install --without=dev

RUN printf '#!/usr/bin/env bash \n\
set -euo pipefail \n\
source .venv/bin/activate \n\
flask --app src/python_app run --host 0.0.0.0 --port 8000 \n\
' > entrypoint.bash

ENTRYPOINT [ "bash", "/opt/python-app/entrypoint.bash" ]
EXPOSE 8000
146 changes: 146 additions & 0 deletions examples/python-web-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Example Python web application

This is a example web application demonstrating development of Python
application using dependencies provided by multiple sources.

Application data can be generated by local Python server or by
PostgreSQL/PostGIS database backend.

Application is packaged in container image using traditional `Dockerfile`.

## Dependencies

### Provided by Geonix

* shapely

### Provided by Nixpkgs

* matplotlib

### Provided by Poetry

* black
* flask
* isort
* pytest


## Template initialization

```
mkdir my-project
cd my-project

git init

nix flake init --accept-flake-config --template github:imincik/geonix#python-web-app

nix flake lock
git add *
```


## Usage

### Development (with local data backend)

* Enter development shell

```
nix develop
```

* Install Python virtual environment using Poetry

```
poetry env use $(which python)
poetry install
```

* Launch Python development server

```
poetry run flask --app src/python_app run --reload
```

* Exit development shell

```
exit
```

### Development (with database data backend)

* Launch PostgreSQL database

```
nix develop .#postgresql
```

* Enter development shell (run in new terminal window)

```
nix develop
```

* Launch Python development server

```
BACKEND=db poetry run flask --app src/python_app run --reload
```

* Connect to PostgreSQL DB container (optional)

```
psql
```

* Exit development shell

```
exit
```


### Geonix CLI

* Search for additional packages (run in development shell)

```
geonix search <PACKAGE>
```


## Deployment

* Build and load base container image

```
nix build .#container
docker load < ./result
```

* Build application container image

```
docker build -t python-app:latest .
```

* Run container

```
docker run --rm -p 8000:8000 python-app:latest
```


## Customized packages

See
[Customized packages](https://github.com/imincik/geonix/wiki/Customized-packages)
for instructions how to customize packages.


## More info

* [Zero to Nix](https://zero-to-nix.com/)
Loading