Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Poetry and docker compose #34

Merged
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
10 changes: 5 additions & 5 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r dev-requirements.txt
- name: Lint with ruff
run: |
make lint
make format
pip install poetry
poetry config installer.max-workers 10
poetry install --no-interaction --no-ansi
- name: Run pre-commit
run: poetry run pre-commit run --all-files
8 changes: 5 additions & 3 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ jobs:

- name: Install dependencies
run: |
mv .env.example .env
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r dev-requirements.txt
pip install poetry
poetry config installer.max-workers 10
poetry install --no-interaction --no-ansi

- name: Test with pytest
run: |
set -o pipefail
python -m pytest --junitxml=pytest.xml --cov-report=term-missing --cov=src tests/ | tee pytest-coverage.txt
poetry run python -m pytest --junitxml=pytest.xml --cov-report=term-missing --cov=src tests/ | tee pytest-coverage.txt

- name: Pytest coverage comment
continue-on-error: true
Expand Down
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.4.6
hooks:
# Run the linter.
- id: ruff
args: [ ., --fix, --diff ]
# Run the formatter.
- id: ruff-format
args: [ . ]
56 changes: 36 additions & 20 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,42 @@ To find issues you can help with, go though the list of [good first issues](http
Once you find an interesting issue let us know that you want to work on it by commenting on the issue.

## Development
### Install our development environment
1. Please set up your development environment by referring to the `Setup` section in the `README.md`.

2. Install the `pre-commit`:
```
pre-commit install
```

### Code Style and Quality
- The [PEP 8](https://realpython.com/python-pep8/) styling convention is used.
- This is achieved using the `ruff` Linter and Formatter.
- The Linter and Formatter are automatically executed before committing via pre-commit.
- If you want to run the Linter and Formatter at any time, execute `pre-commit run --all-files`.

### Testing
> [!NOTE]
> This project uses `Makefile` as a task runner. You need to set up your environment to be able to run the `make` command.

Run the following command in the project's root directory:
```bash
# Run tests locally using Poetry
make test

# Run tests Docker container
make test_docker
```
You can generate a coverage report with the following command:
```bash
# Generate a coverage report locally using Poetry
make output_coverage

# Generate a coverage report in a Docker container
make output_coverage_docker
```
Additionally, when a PR is raised, pytest will be executed by the GitHub Actions CI.

**Code Style and Quality**

The [PEP 8](https://realpython.com/python-pep8/) styling convention is used.

To make sure you are following it, you can install [black](https://pypi.org/project/black/)

`pip install black`

To run black:

`black .`

**Testing**

To run tests, install pytest, `pip install pytest` and navigate to `/test`.

Run `pytest`

On commit, git will run `pytest` for you to catch any errors.

## Questions, suggestions or new ideas

Expand All @@ -46,4 +62,4 @@ Feel free to [create a new issue](https://github.com/ryansurf/cli-surf/issues/ne

Be sure to explain in details the context and the outcome that you are lookign for. If reporting bugs, provide basic information like your OS version, whether using Docker, etc.

Thanks! :ocean: :surfer:
Thanks! :ocean: :surfer:
23 changes: 16 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@

FROM python:3.12.3-slim

#this instruction specifies the "working directory"
# install apt packages (curl command)
RUN apt-get update && \
apt-get install -y curl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

#this instruction specifies the "working directory"
#or the path in the image where files will be copied and commands will be executed.
WORKDIR /app

# Install the application dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy in the source code
COPY . .

# Copy in the .env file
COPY .env.example .env

# Install the application dependencies
RUN pip install poetry
RUN poetry config installer.max-workers 10
RUN poetry install --no-interaction --no-ansi

# # Setup an app user so the container doesn't run as the root user
# RUN useradd app
# USER app

# Set the working directory for running the application
WORKDIR /app/src
EXPOSE 8000

# Command to run the Flask application
CMD ["python3", "server.py"]
CMD ["poetry", "run", "python", "src/server.py"]
106 changes: 69 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,49 +65,73 @@ Wave Period: 9.8
* `curl localhost:8000/help`


## Installation

**Python Dependencies**
* python>=3.8.1
* geopy
* openmeteo_requests
* pandas
* python-dotenv
* Requests
* requests_cache
* retry_requests

To run locally on your machine, I recommend either:

**Using a [Python Virtual Environment](https://docs.python.org/3/library/venv.html)**
1. `git clone https://github.com/ryansurf/cli-surf.git`
2. `cd cli-surf`
3. `cp .env.example .env`
4. `python3 -m venv venv`
5. `source venv/bin/activate`
6. `pip install -r requirements.txt`
7. `cd src`
8. `python3 server.py`

**Or running Docker (install [Docker](https://docs.docker.com/engine/install/))**
1. `git clone https://github.com/ryansurf/cli-surf.git`
2. `cd cli-surf`
3. `cp .env.example .env`
4. `docker build -t cli-surf .`
5. `docker run -d -p 8000:8000 cli-surf`
* Add `--restart unless-stopped` for automatic start on reboot

## Setup
### How to Start Locally with `Poetry`
To use cli-surf, clone the project locally and install the necessary dependencies via `poetry`.

1. Install [Poetry](https://python-poetry.org/docs/#installation).

2. Clone the repository.
```bash
git clone https://github.com/ryansurf/cli-surf.git
cd cli-surf
```

3. Install dependencies using Poetry.
```bash
poetry install
```

4. Activate the virtual environment.
```bash
poetry shell
```

5. Run the project. For example, if the entry point is `server.py`, use the following command.
```bash
python src/server.py

# Alternatively, you can run the project using `Makefile`
make run
```

### How to Start with `Docker`
If you do not have Poetry installed or do not want to pollute your local environment, you can also start the project using Docker Compose.

1. Install [Docker](https://docs.docker.com/engine/install/).
2. Install [Docker Compose](https://docs.docker.com/compose/install/).

**Variables**
3. Clone the repository.
```bash
git clone https://github.com/ryansurf/cli-surf.git
cd cli-surf
```

4. Docker compose up.
```bash
docker compose up -d

# Alternatively, you can run the project using `Makefile`
make run_docker
```


### Variables

When running locally with Poetry, create a `.env` file from the `.env.example` file.
```bash
cp .env.example .env
```

Note that when starting with Docker, the `.env` file will be automatically created from `.env.example` during the image build.

Change `.env.example` to `.env`

| Variable | Description|
| -------- | ------- |
| `PORT` | The port you want to open to run the application. Default = `8000` |
| `IP_ADDRESS` | The ip your server is running on. Default = `localhost` |
| `SMTP_SERVER` | The email server you are using. Default = smtp.gmail.com |
| `SMTP_PORT` | The email server port you are using. Default = `587` |
| `EMAIL` | The email you will send the report from. |
| `EMAIL_PW` | The sending email's password |
| `EMAIL_RECEIVER` | The email that will receive the report (your personal email) |
Expand All @@ -116,15 +140,23 @@ Change `.env.example` to `.env`
| `GPT_PROMPT` | Given the surf data (height, swell direction, etc.), you can tell the GPT what kind of report you would like. For example: `With this data, recommend what size board I should ride.` |


**Email Server**
### Email Server

Optional, sends a surf report to a specified email.

You will need to setup an email account that is able to utilize SMTP services. I used Gmail, following Method #1 outlined [here](https://www.cubebackup.com/blog/how-to-use-google-smtp-service-to-send-emails-for-free/). After doing this, change the variables in `.env`

Execute by running `python3 send_email.py`. Running with cron is a good use case
The Email Server can be executed using one of the following methods.
```bash
# Send Email locally using Poetry
make send_email

# Send Email in a Docker container
make send_email_docker
```
Note that the Flask server must be running in order to send emails.

**Frontend**
### Frontend

<p align="center">
<img src="images/website.gif" alt="cli-surf_website gif" style="width: 700px; height: auto;">
Expand Down
10 changes: 10 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
flask:
build: .
container_name: cli-surf
hostname: cli-surf
restart: always
ports:
- "8000:8000"
volumes:
- .:/app
3 changes: 0 additions & 3 deletions dev-requirements.txt

This file was deleted.

11 changes: 3 additions & 8 deletions docs/styling.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# Styling

## Code Style and Quality

The [PEP 8](https://realpython.com/python-pep8/) styling convention is used.

To make sure you are following it, you can install [black](https://pypi.org/project/black/)

`pip install black`

To run black:
This is achieved using the ruff Linter and Formatter.

`black .`
The Linter and Formatter are automatically executed before committing via pre-commit.

On a push/pull request, git will run `black .` for you!
If you want to run the Linter and Formatter at any time, execute `pre-commit run --all-files`.
28 changes: 20 additions & 8 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
.PHONY: lint format
.PHONY: run run_docker test test_docker post_test docker_post_test send_email send_email_docker

lint:
ruff check . && ruff check . --diff
run:
poetry run python src/server.py

format:
ruff check . --fix && ruff format .
run_docker:
docker compose up -d

test:
pytest -s -x --cov=src -vv
poetry run pytest -s -x --cov=src -vv

post_test:
coverage html
test_docker:
docker compose exec flask poetry run pytest -s -x --cov=src -vv

output_coverage:
poetry run coverage html

output_coverage_docker:
docker compose exec flask poetry run coverage html

send_email:
poetry run python src/send_email.py

send_email_docker:
docker compose exec flask poetry run python src/send_email.py
Loading
Loading