Package Inputs
Package Inputs & Multiple Functions
Section titled “Package Inputs & Multiple Functions”Most real packages need to accept input and expose multiple functions. This tutorial covers both.
How Inputs Work
Section titled “How Inputs Work”When Brane calls a function in your package, it passes input values via environment variables. The variable names match the input parameter names directly.
For example, if your function declares an input called text, Brane sets the environment variable text to the provided value.
Example: Base64 Encoder/Decoder
Section titled “Example: Base64 Encoder/Decoder”Let’s build a package with two functions: encode and decode.
The Code
Section titled “The Code”Create base64_tool.py:
#!/usr/bin/env python3import base64import osimport sysimport yaml
def encode(): text = os.environ.get("text", "") encoded = base64.b64encode(text.encode()).decode() return {"encoded": encoded}
def decode(): text = os.environ.get("text", "") decoded = base64.b64decode(text.encode()).decode() return {"decoded": decoded}
if __name__ == "__main__": # Brane passes the function name as the first argument command = sys.argv[1] if len(sys.argv) > 1 else ""
if command == "encode": result = encode() elif command == "decode": result = decode() else: print(f"Unknown command: {command}", file=sys.stderr) sys.exit(1)
print(yaml.dump(result, default_flow_style=True).strip())The key pattern:
- Read
sys.argv[1]to determine which function was called - Read inputs from environment variables
- Print the result as YAML to stdout
The Package Definition
Section titled “The Package Definition”Create container.yml:
name: base64_toolversion: 1.0.0kind: ecu
dependencies: - python3 - python3-yaml
files: - base64_tool.py
entrypoint: kind: task exec: base64_tool.py
actions: 'encode': command: args: - encode input: - name: text type: string output: - name: encoded type: string
'decode': command: args: - decode input: - name: text type: string output: - name: decoded type: stringKey differences from the hello-world package:
- Two actions (
encodeanddecode) instead of one command.argspasses the function name as a command-line argumentinputdeclares the expected parameters with their types- Each action has its own
outputwith a corresponding YAML key
Build and Test
Section titled “Build and Test”brane package build ./container.ymlbrane package test base64_toolSelect the encode function, enter some text, and verify the output.
Use in a Workflow
Section titled “Use in a Workflow”import base64_tool;
let encoded := encode("Hello, Brane!");println(encoded);
let decoded := decode(encoded);println(decoded);Supported Input Types
Section titled “Supported Input Types”| Type | BraneScript | Description |
|---|---|---|
string | "text" | Text values |
integer | 42 | Whole numbers |
real | 3.14 | Floating-point numbers |
boolean | true / false | Boolean values |
Array[T] | [1, 2, 3] | Arrays of a single type |
- Always handle missing or empty environment variables gracefully
- Use YAML for output — Brane expects it
- Test each function locally before pushing
- Keep packages focused: one package per related group of functions
Next Steps
Section titled “Next Steps”- Working with datasets — Access and produce data in workflows