The goal of this tutorial is to build a simple ByteNite app that outputs the string "Hello, World!" to a file. Use this tutorial if you're new to ByteNite and want to have a quick sense of the workflow.
Duration
Difficulty
Prerequisites
~15m
Very Easy
1
Download sample app
Run the app new command in your terminal to create an app directory locally named "hello-world":
bytenite app new hello-world
Check that a new directory named "hello-world" was indeed created at your base path:
ls
2
Write the run function
Locate your pre-generated Python runner file (./hello-world/app/runner.py) and add the code to your run function.
We'll make the app perform a few simple steps:
Read a string from a text file.
Convert the string to a case matching the input parameter 'case'.
Write the result to a new text file.
runner.py
import os
OUTPUT_PATH = 'result'
def run(basepath, params, metadata):
print('Run called')
# 1. Reading Inputs
# Expect a text file to be passed by the partitioner
with open(os.path.join(basepath, 'data.bin'), 'rb') as f:
data = f.read()
my_string = data.decode('utf-8')
# 2. Handling Parameters
# Expect the job parameters to have a key named 'case', and the options to be "upper", "lower", and "title"
case = params['case']
# 3. Developing the Core Functionality
# Convert the string to the specified case
if case == "upper":
my_string = my_string.upper()
elif case == "lower":
my_string = my_string.lower()
elif case == "title":
my_string = my_string.title()
# 4. Saving Outputs
# Save the string directly into a text file to the default
output_path = os.path.join(basepath, OUTPUT_PATH, "hello_world.txt")
with open(output_path, 'w') as f:
f.write(my_string)
3
Submit and activate your app
Upload the content of your app:
bytenite app push hello-world
Activate your app to make it accept jobs:
bytenite app activate hello-world
Now, check your app's details and status by running the command:
bytenite app get hello-world
Finally, ensure that your app has a corresponding template, which you will need for running jobs. This template is created automatically during the app initialization process. You've already uploaded it with the command above, unless you've removed or edited it:
bytenite template get hello-world-template
4
Launch a job with 'hello-world-template'
Once your app is uploaded and activated, you can run it through the Jobs API.
Send a POST request to the endpoint to create a new job, and then run it. All requests must be authenticated with an access token.
Tip
If you previously authenticated through the CLI, you don't need an API key to generate a token as you already have a fresh one stored in your application library:
Token path on Mac: /Users/[user]/Library/Application Support/bytenite-cli/auth.json
Token path on Linux: /$HOME/.config/bytenite-cli/auth.json
Below is a Python script that sends job create and run requests. You can send API requests in any programming language, or using API testing tools like Postman:
import requests
import json
# Extract your token from CLI credentials. Alternatively, get a token with an API key.
token_path = "/Users/[user]/Library/Application Support/bytenite-cli/auth.json"
with open(token_path, 'r') as file:
data = json.load(file)
access_token = data["access_token"]
# Create a new job with 'hello-world-template'
job_body = {
"templateId": "hello-world",
"name": "Job with hello-world",
"dataSource": {
"dataSourceDescriptor": "url",
"params": {
"@type": "type.googleapis.com/bytenite.data_source.HttpDataSource",
"url": "https://storage.googleapis.com/video-test-public/hello-world-I.txt"
}
},
"dataDestination": {
"dataSourceDescriptor": "bucket"
},
"params": {
"app": {
"case": "upper"
}
}
}
response = requests.post('https://api.bytenite.com/v1/customer/jobs',
json=job_body,
headers={'Authorization': access_token})
job_id = response.json()['job']['id']
print(f'Job ID: {job_id}')
# Run the job
response = requests.post(f'https://api.bytenite.com/v1/customer/jobs/{job_id}/run',
headers={'Authorization': access_token})
response.raise_for_status()
5
Check the results
Call the 'results' endpoint to access the job's output. Hello, ByteNite!