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
6 changes: 4 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"customizations": {
"vscode": {
"extensions": [
"esbenp.prettier-vscode"
"esbenp.prettier-vscode",
"tamasfe.even-better-toml"
]
}
},
Expand All @@ -36,6 +37,7 @@
},
"postStartCommand": {
"temporal-server": "nohup bash -c 'temporal server start-dev &'",
"npm-install": "cd typescript && npm ci"
"npm-install": "cd typescript && npm ci",
"python-install": "cd python && uv sync"
}
}
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ insert_final_newline = true
indent_style = tab
indent_size = 4

[*.py]
indent_size = 4

[Makefile]
indent_style = tab
indent_size = 4
38 changes: 38 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Python
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: python
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version-file: python/pyproject.toml

- name: Install uv
uses: astral-sh/setup-uv@v6
with:
enable-cache: true
working-directory: python

- name: Ruff check
uses: astral-sh/ruff-action@v3
with:
src: "./python"

- name: Ruff format
uses: astral-sh/ruff-action@v3
with:
src: "./python"
args: format --check
3 changes: 3 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vscode:
- ms-python.python
- ms-python.vscode-pylance
- ms-python.autopep8
- tamasfe.even-better-toml

tasks:
- name: Temporal server
Expand All @@ -21,8 +22,10 @@ tasks:
init: |
go install github.com/air-verse/air@latest
(cd typescript/ && npm ci)
(cd python/ && uv sync)
gp sync-done dependencies
command: exit
openMode: split-right

- name: Install pre-commit
init: |
Expand Down
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ repos:
rev: v0.18.1
hooks:
- id: markdownlint-cli2
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.12.11
hooks:
# Run the linter.
- id: ruff-check
args: [ --fix ]
# Run the formatter.
- id: ruff-format
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast"
]
],
"python.defaultInterpreterPath": "${workspaceFolder}/python/.venv/bin/python",
"python-envs.pythonProjects": []
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ dependencies:
##### Python

* [Python](https://www.python.org/downloads)
* [uv](https://docs.astral.sh/uv)

##### TypeScript

Expand Down
3 changes: 3 additions & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.venv
__pycache__
.ruff_cache
1 change: 1 addition & 0 deletions python/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
63 changes: 63 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Python Starter

Build your Temporal application in Python ([docs](https://docs.temporal.io/develop/python))

<!-- toc -->

* [Overview](#overview)
* [Quick start](#quick-start)
* [Workflow](#workflow)
* [Starter](#starter)

<!-- Regenerate with "pre-commit run -a markdown-toc" -->

<!-- tocstop -->

## Overview

> [!IMPORTANT]
> Make sure you have followed [the instructions to configure your development environment](../README.md#your-development-environment)
> before reading this section.

This repository is designed to be a quick-start for your Python Temporal application.
It is designed to be quick and easy to get started, rather than any example of
best practice.

This starter application consists of two parts:

* `src/worker.py`: this is where you define your workflow and contains the main business
logic for your Temporal application.
* `src/starter.py`: this is how your trigger your workflow with any input variables
and where you await the response.

## Quick start

Install dependencies using `uv`:

```sh
cd python
uv sync
source .venv/bin/activate
```

### Workflow

```shell
cd python
uv run poe dev
```

The workflow is a long-running application. For rapid development, the recommendation
is to use the file watcher.

You can also run this with `uv run src/worker.py` and restarting after every code
change.

### Starter

```shell
cd python
uv run src/starter.py
```

The starter will usually be something that runs to completion.
17 changes: 17 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[project]
name = "python"
version = "0.0.0"
description = "Python hackathon starter"
readme = "README.md"
requires-python = ">=3.12"
dependencies = ["temporalio>=1.16.0"]

[dependency-groups]
dev = [
"poethepoet>=0.37.0",
"ruff>=0.12.11",
"watchdog>=6.0.0",
]

[tool.poe.tasks]
dev = "watchmedo auto-restart -p '*.py' -R -- uv run ./src/worker.py"
9 changes: 9 additions & 0 deletions python/src/activities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from temporalio import activity


@activity.defn
async def sayName(name: str) -> str:
if name == "":
name = "anonymous human"

return f"Hello {name}!"
1 change: 1 addition & 0 deletions python/src/shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TASK_QUEUE_NAME = "hackathon"
24 changes: 24 additions & 0 deletions python/src/starter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import asyncio
import uuid
from temporalio.client import Client
from temporalio.envconfig import ClientConfigProfile
import shared
import workflows


async def main():
default_profile = ClientConfigProfile.load()
connect_config = default_profile.to_client_connect_config()

client = await Client.connect(**connect_config)
result = await client.execute_workflow(
workflows.HelloWorldWorkflow,
"Hacker",
id=f"hello-world-workflow-{uuid.uuid4()}",
task_queue=shared.TASK_QUEUE_NAME,
)
print("Workflow result:", result)


if __name__ == "__main__":
asyncio.run(main())
27 changes: 27 additions & 0 deletions python/src/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import asyncio
from temporalio.client import Client
from temporalio.worker import Worker
from temporalio.envconfig import ClientConfigProfile
from workflows import HelloWorldWorkflow
from activities import sayName
import shared


async def main():
default_profile = ClientConfigProfile.load()
connect_config = default_profile.to_client_connect_config()

client = await Client.connect(**connect_config)
worker = Worker(
client,
task_queue=shared.TASK_QUEUE_NAME,
workflows=[HelloWorldWorkflow],
activities=[sayName],
)

print("Worker started.")
await worker.run()


if __name__ == "__main__":
asyncio.run(main())
14 changes: 14 additions & 0 deletions python/src/workflows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from datetime import timedelta
from temporalio import workflow
from activities import sayName


@workflow.defn
class HelloWorldWorkflow:
@workflow.run
async def run(self, name: str) -> str:
return await workflow.execute_activity(
sayName,
name,
schedule_to_close_timeout=timedelta(seconds=10),
)
Loading
Loading