Skip to content

Commit 6eab70c

Browse files
authored
Initial commit
0 parents  commit 6eab70c

File tree

16 files changed

+5099
-0
lines changed

16 files changed

+5099
-0
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
renv/library/
2+
renv/local/
3+
renv/lock/
4+
renv/python/
5+
renv/staging/
6+
7+
test
8+
tests
9+
workflow_tests

.github/workflows/ci.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: CI Workflow
2+
3+
on:
4+
push:
5+
branches: ['main']
6+
7+
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
12+
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
13+
jobs:
14+
build-and-push-image:
15+
runs-on: ubuntu-latest
16+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
17+
permissions:
18+
contents: read
19+
packages: write
20+
attestations: write
21+
id-token: write
22+
#
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
27+
- name: Log in to the Container registry
28+
uses: docker/login-action@v3.3.0
29+
with:
30+
registry: ${{ env.REGISTRY }}
31+
username: ${{ github.actor }}
32+
password: ${{ secrets.GITHUB_TOKEN }}
33+
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
34+
- name: Extract metadata (tags, labels) for Docker
35+
id: meta
36+
uses: docker/metadata-action@v5.5.1
37+
with:
38+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
39+
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
40+
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
41+
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
42+
- name: Build and push Docker image
43+
id: push
44+
uses: docker/build-push-action@v6.7.0
45+
with:
46+
context: .
47+
push: true
48+
tags: ${{ steps.meta.outputs.tags }}
49+
labels: ${{ steps.meta.outputs.labels }}
50+
# A PAT is needed for this action, GITHUB_TOKEN cannot get relevant permission
51+
# secrets: |
52+
# github_pat=${{ secrets.GH_PAT }}

.github/workflows/release.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Release Workflow
2+
3+
on:
4+
push:
5+
tags:
6+
- '[0-9]+.[0-9]+.[0-9]+' # Push events to any matching semantic tag. For example, 1.10.1 or 2.0.0.
7+
8+
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
9+
env:
10+
REGISTRY: ghcr.io
11+
IMAGE_NAME: ${{ github.repository }}
12+
13+
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
14+
jobs:
15+
build-and-push-image:
16+
runs-on: ubuntu-latest
17+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
18+
permissions:
19+
contents: write
20+
packages: write
21+
attestations: write
22+
id-token: write
23+
#
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
- name: Update container in operator JSON
28+
run: |
29+
jq --arg variable "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_REF##*/}" '.container = $variable' operator.json
30+
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
31+
- name: Log in to the Container registry
32+
uses: docker/login-action@v3.3.0
33+
with:
34+
registry: ${{ env.REGISTRY }}
35+
username: ${{ github.actor }}
36+
password: ${{ secrets.GITHUB_TOKEN }}
37+
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
38+
- name: Extract metadata (tags, labels) for Docker
39+
id: meta
40+
uses: docker/metadata-action@v5.5.1
41+
with:
42+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
43+
tags: |
44+
# minimal
45+
type=pep440,pattern={{version}}
46+
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
47+
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
48+
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
49+
- name: Build and push Docker image
50+
id: push
51+
uses: docker/build-push-action@v6.7.0
52+
with:
53+
context: .
54+
push: true
55+
tags: ${{ steps.meta.outputs.tags }}
56+
labels: ${{ steps.meta.outputs.labels }}
57+
# A PAT is needed for this action, GITHUB_TOKEN cannot get relevant permission
58+
# secrets: |
59+
# github_pat=${{ secrets.GH_PAT }}
60+
- name: Create required package.json
61+
run: echo '{}' > package.json
62+
- name: Build changelog
63+
id: Changelog
64+
uses: tercen/generate-changelog-action@master
65+
- name: Create release
66+
id: create_release
67+
uses: actions/create-release@latest
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
70+
with:
71+
tag_name: ${{ github.ref }}
72+
release_name: Release ${{ github.ref }}
73+
body: ${{steps.Changelog.outputs.changelog}}
74+
draft: false
75+
prerelease: false

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
.idea
6+
venv/*

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["ms-python.python", "ms-toolsai.jupyter"]
3+
}

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Python: Current File",
6+
"type": "python",
7+
"python": "/config/.pyenv/versions/3.9.0/bin/python",
8+
"request": "launch",
9+
"program": "${file}",
10+
"console": "integratedTerminal",
11+
"justMyCode": true,
12+
"env": { "PYTHONPATH": "${workspaceRoot}"}
13+
14+
}
15+
]
16+
}

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM tercen/runtime-python39:0.1.0
2+
3+
COPY . /operator
4+
WORKDIR /operator
5+
6+
ENV PYTHONPATH "${PYTHONPATH}:~/.pyenv/versions/3.9.0/bin/python3"
7+
RUN python3 -m pip install -r ./requirements.txt
8+
9+
ENV TERCEN_SERVICE_URI https://tercen.com
10+
11+
ENTRYPOINT ["python3", "main.py"]
12+
CMD ["--taskId", "someid", "--serviceUri", "https://tercen.com", "--token", "sometoken"]

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
# Template Python Operator
3+
4+
Cell-wise mean calculated implemented in Python.
5+
6+
## Python operator - Development workflow
7+
8+
* Set up [the Tercen Studio development environment](https://github.com/tercen/tercen_studio)
9+
* Create a new git repository based on the [template Python operator](https://github.com/tercen/template-python-operator)
10+
* Open VS Code Server by going to: http://127.0.0.1:8443
11+
* Clone this repository into VS Code (using the 'Clone from GitHub' command from the Command Palette for example)
12+
* Load the environment and install core requirements by running the following commands in the terminal:
13+
14+
```bash
15+
source /config/.pyenv/versions/3.9.0/bin/activate
16+
pip install -r requirements.txt
17+
```
18+
19+
* Develop your operator. Note that you can interact with an existing data step by specifying arguments to the `TercenContext` function:
20+
21+
```python
22+
tercenCtx = ctx.TercenContext()
23+
```
24+
25+
```python
26+
tercenCtx = ctx.TercenContext(
27+
workflowId="YOUR_WORKFLOW_ID",
28+
stepId="YOUR_STEP_ID",
29+
username="admin", # if using the local Tercen instance
30+
password="admin", # if using the local Tercen instance
31+
serviceUri = "http://tercen:5400/" # if using the local Tercen instance
32+
)
33+
```
34+
35+
* Generate requirements
36+
37+
```bash
38+
python3 -m tercen.util.requirements . > requirements.txt
39+
```
40+
41+
* Push your changes to GitHub: triggers CI GH workflow
42+
* Tag the repository: triggers Release GH workflow
43+
* Go to tercen and install your operator
44+
45+
46+
## Helpful Commands
47+
48+
### Install Tercen Python Client
49+
50+
```bash
51+
python3 -m pip install --force git+https://github.com/tercen/tercen_python_client@0.7.1
52+
```
53+
54+
### Wheel
55+
56+
Though not strictly mandatory, many packages require it.
57+
58+
```bash
59+
python3 -m pip install wheel
60+
```
61+

main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from tercen.client import context as ctx
2+
import numpy as np
3+
4+
tercenCtx = ctx.TercenContext()
5+
6+
df = (
7+
tercenCtx
8+
.select(['.y', '.ci', '.ri'], df_lib="pandas")
9+
.groupby(['.ci','.ri'], as_index=False)
10+
.mean()
11+
.rename(columns={".y":"mean"})
12+
.astype({".ci": np.int32, ".ri": np.int32})
13+
)
14+
15+
df = tercenCtx.add_namespace(df)
16+
tercenCtx.save(df)

operator.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "Template Python operator",
3+
"description": "Mean operator implemented in Python",
4+
"tags": ["template"],
5+
"authors": ["tercen"],
6+
"urls": ["https://github.com/tercen/template-python-operator"],
7+
"container":"ghcr.io/tercen/template-python-operator:latest",
8+
"properties": [
9+
{
10+
"kind": "DoubleProperty",
11+
"name": "Property1",
12+
"defaultValue": 42.0,
13+
"description": "Example Double Property"
14+
},
15+
{
16+
"kind": "StringProperty",
17+
"name": "Property2",
18+
"defaultValue": "42",
19+
"description": "Example String Property"
20+
},
21+
{
22+
"kind": "BooleanProperty",
23+
"name": "Property3",
24+
"defaultValue": true,
25+
"description": "Example Boolean Property"
26+
},
27+
{
28+
"kind": "EnumeratedProperty",
29+
"isSingleSelection":true,
30+
"name": "Property4",
31+
"defaultValue": "element2",
32+
"values": ["element1", "element2", "element3"],
33+
"description": "Example Enumerated Property"
34+
}
35+
]
36+
}

0 commit comments

Comments
 (0)