This commit is contained in:
Jonathon Broughton
2024-05-06 04:28:40 +00:00
parent 404c50cc26
commit 25f4e3f275
3 changed files with 832 additions and 352 deletions
+118 -42
View File
@@ -1,71 +1,129 @@
# Speckle Automate function template - Python
# Speckle Automate Workshop: Python Function Template
This is a template repository for a Speckle Automate functions written in python
using the [specklepy](https://pypi.org/project/specklepy/) SDK to interact with Speckle data.
This template contains the full scaffolding required to publish a function to the automate environment.
Also has some sane defaults for a development environment setups.
This repository serves as a starting point for creating Speckle Automate
functions in Python for the Automate workshops.
It is based on
the [Speckle Automate Python Function Template](https://github.com/specklesystems/speckle_automate_python_example)
which
is the preferred starting point for creating new
Speckle Automate functions in Python.
## Getting started
1. Use this template repository to create a new repository in your own / organization's profile.
This template will be available from the Speckle Automate New Function wizard.
Select the Workshop icon to create a new repository from this template.
Register the function
By default, the wizard will create a new repository in your GitHub account,
and you will be able to start editing the code in the `main.py` file.
### Add new dependencies
To create a new version of your Function, create a new GitHub release in your
repository.
This will trigger GitHub Action that builds, tests and deploys your function to
Speckle Automate.
To add new python package dependencies to the project, use:
`$ poetry add pandas`
### Managing Dependencies with Poetry
### Change launch variables
Poetry simplifies dependency management for Python projects, ensuring consistent
environments and hassle-free dependency
resolution. Here's why we use Poetry:
describe how the launch.json should be edited
Dependency Resolution: Poetry ensures compatible library versions, preventing
conflicts.
Virtual Environments: It manages project dependencies in isolated virtual
environments.
Lockfile: Generates a lockfile (poetry.lock) for reproducible builds.
Simplified Installation: Adding dependencies is as easy as poetry add <
package-name>.
Dependency Isolation: Ensures project dependencies are self-contained and
portable.
Project Metadata: Manages project configuration in a single `pyproject.toml`
file.
### Github Codespaces
#### Adding Dependencies
Create a new repo from this template, and use the create new code.
To add new dependencies, use:
### Using this Speckle Function
`$ poetry add <package-name>`
Replace <package-name> with the desired package. Poetry handles the rest,
updating project files automatically.
Note: It's while it is a good practice to combine the use of Poetry with virtual
environments to ensure a clean and
isolated development environment for your Python projects, other tools like
pipenv or venv can also be used. It is not
mandatory to use a virtual environment for Speckle Automate functions.
### Configuring Launch Variables (Visual Studio Code)
To edit launch variables in Visual Studio Code, follow these steps:
Open the project in Visual Studio Code.
Navigate to the .vscode directory.
Open the launch.json file.
Edit the configurations as needed.
Save the file.
These configurations specify how your Python script will be run and debugged
within Visual Studio Code.
### GitHub Codespaces
Once you have created a clone of this template repo with the Automate wizard,
you can use GitHub Codespaces to
develop your function in the cloud. In the Codespaces environment, you can edit
code, run tests, and debug your
function.
To open your repository in a Codespace, click the "Code" button in the GitHub UI
and select "Open with Codespaces".
## Using this Speckle Function
1. [Create](https://automate.speckle.dev/) a new Speckle Automation.
1. Select your Speckle Project and Speckle Model.
1. Select the existing Speckle Function named [`Random comment on IFC beam`](https://automate.speckle.dev/functions/e110be8fad).
1. Enter a phrase to use in the comment.
1. Click `Create Automation`.
## Getting Started with creating your own Speckle Function
1. [Register](https://automate.speckle.dev/) your Function with [Speckle Automate](https://automate.speckle.dev/) and select the Python template.
1. A new repository will be created in your GitHub account.
1. Make changes to your Function in `main.py`. See below for the Developer Requirements, and instructions on how to test.
1. To create a new version of your Function, create a new [GitHub release](https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository) in your repository.
2. Select your Speckle Project and Speckle Model.
3. Select the Speckle Function you created from this template.
4. Enter the requested inputs. For first run this will be a phrase to use in a
comment.
5. Click `Create Automation`.
## Developer Requirements
1. Install the following:
- [Python 3](https://www.python.org/downloads/)
- [Python 3](https://www.python.org/downloads/) (>= 3.10)
- [Poetry](https://python-poetry.org/docs/#installing-with-the-official-installer)
1. Run `poetry shell && poetry install` to install the required Python packages.
2. Run `poetry shell && poetry install` to install the required Python packages.
## Building and Testing
The code can be tested locally by running `poetry run pytest`.
The code can be tested locally by running `poetry run pytest`. The tests are
located in the `tests` directory.
The tests also allow for testing the function locally by mocking the Speckle
Automate environment or using the specklepy
authentication token to connect to a real Speckle Server and use real data.
### Building and running the Docker Container Image
Running and testing your code on your own machine is a great way to develop your Function; the following instructions are a bit more in-depth and only required if you are having issues with your Function in GitHub Actions or on Speckle Automate.
Running and testing your code on your own machine is a great way to develop your
Function; the following instructions
are a bit more in-depth and only required if you are having issues with your
Function in GitHub Actions or on Speckle
Automate.
#### Building the Docker Container Image
Your code is packaged by the GitHub Action into the format required by Speckle Automate. This is done by building a Docker Image, which is then run by Speckle Automate. You can attempt to build the Docker Image yourself to test the building process locally.
Your code is packaged by the GitHub Action into the format required by Speckle
Automate. This is done by building a
Docker Image, which is then run by Speckle Automate. You can attempt to build
the Docker Image yourself to test the
building process locally.
To build the Docker Container Image, you will need to have [Docker](https://docs.docker.com/get-docker/) installed.
To build the Docker Container Image, you will need to
have [Docker](https://docs.docker.com/get-docker/) installed.
Once you have Docker running on your local machine:
1. Open a terminal
1. Navigate to the directory in which you cloned this repository
1. Run the following command:
2. Navigate to the directory in which you cloned this repository
3. 3.Run the following command:
```bash
docker build -f ./Dockerfile -t speckle_automate_python_example .
@@ -73,7 +131,11 @@ Once you have Docker running on your local machine:
#### Running the Docker Container Image
Once the image has been built by the GitHub Action, it is sent to Speckle Automate. When Speckle Automate runs your Function as part of an Automation, it will run the Docker Container Image. You can test that your Docker Container Image runs correctly by running it locally.
Once the image has been built by the GitHub Action, it is sent to Speckle
Automate. When Speckle Automate runs your
Function as part of an Automation, it will run the Docker Container Image. You
can test that your Docker Container Image
runs correctly by running it locally.
1. To then run the Docker Container Image, run the following command:
@@ -87,14 +149,28 @@ Once the image has been built by the GitHub Action, it is sent to Speckle Automa
Let's explain this in more detail:
`docker run --rm speckle_automate_python_example` tells Docker to run the Docker Container Image that we built earlier. `speckle_automate_python_example` is the name of the Docker Container Image that we built earlier. The `--rm` flag tells docker to remove the container after it has finished running, this frees up space on your machine.
`docker run --rm speckle_automate_python_example` tells Docker to run the Docker
Container Image that we built
earlier. `speckle_automate_python_example` is the name of the Docker Container
Image that we built earlier. The `--rm`
flag tells docker to remove the container after it has finished running, this
frees up space on your machine.
The line `python -u main.py run` is the command that is run inside the Docker Container Image. The rest of the command is the arguments that are passed to the command. The arguments are:
The line `python -u main.py run` is the command that is run inside the Docker
Container Image. The rest of the command
is the arguments that are passed to the command. The arguments are:
- `'{"projectId": "1234", "modelId": "1234", "branchName": "myBranch", "versionId": "1234", "speckleServerUrl": "https://speckle.xyz", "automationId": "1234", "automationRevisionId": "1234", "automationRunId": "1234", "functionId": "1234", "functionName": "my function", "functionLogo": "base64EncodedPng"}'` - the metadata that describes the automation and the function.
- `{}` - the input parameters for the function that the Automation creator is able to set. Here they are blank, but you can add your own parameters to test your function.
- `yourSpeckleServerAuthenticationToken` - the authentication token for the Speckle Server that the Automation can connect to. This is required to be able to interact with the Speckle Server, for example to get data from the Model.
- `'{"projectId": "1234", "modelId": "1234", "branchName": "myBranch", "versionId": "1234", "speckleServerUrl": "https://speckle.xyz", "automationId": "1234", "automationRevisionId": "1234", "automationRunId": "1234", "functionId": "1234", "functionName": "my function", "functionLogo": "base64EncodedPng"}'` -
the metadata that describes the automation and the function.
- `{}` - the input parameters for the function that the Automation creator is
able to set. Here they are blank, but you
can add your own parameters to test your function.
- `yourSpeckleServerAuthenticationToken` - the authentication token for the
Speckle Server that the Automation can
connect to. This is required to be able to interact with the Speckle Server,
for example to get data from the Model.
## Resources
- [Learn](https://speckle.guide/dev/python.html) more about SpecklePy, and interacting with Speckle from Python.
- [Learn](https://speckle.guide/dev/python.html) more about `specklepy`, and
interacting with Speckle from Python.
Generated
+703 -304
View File
File diff suppressed because it is too large Load Diff
+11 -6
View File
@@ -1,19 +1,24 @@
[tool.poetry]
name = "speckle-automate-py"
version = "0.1.0"
description = "Example function for Speckle Automate using specklepy"
authors = ["Gergő Jedlicska <gergo@jedlicska.com>"]
description = "Template function for BILT Riga Coding workshop"
authors = ["Jonathon Broughton <jonathon@speckle.systems>"]
readme = "README.md"
package-mode = false
[tool.poetry.dependencies]
python = "^3.11"
specklepy = "2.17.17"
specklepy = "2.18.3"
python-levenshtein = "^0.25.1"
more-itertools = "^10.2.0"
pandas = "^2.2.2"
python-dotenv = "^1.0.1"
[tool.poetry.group.dev.dependencies]
black = "^23.3.0"
black = "^24.0.0"
mypy = "^1.3.0"
ruff = "^0.0.271"
pytest = "^7.4.2"
ruff = "^0.4.0"
pytest = "^8.0.0"
# specklepy = { path = "../specklepy", develop = true }
[build-system]