Gear Building Tutorial Part 4: The Run Script
Introduction
In this part, we will cover how to create a basic run file for our example gear. The run file is the file that will contain our run script. This is essentially the "main" section of our code. This file can be used to call other files and processes, but it must contain all commands necessary to complete execution of the gear.
Instruction Steps
- Step 1. Identify the Purpose of the Gear
- Step 2. Create your Run File
- Step 3. Running the script locally
Step 1. Identify the Purpose of the Gear
The first step in building any gear is to identify exactly what the gear is going to do, what input(s) it requires, and what output file(s) it should generate. For this example gear, we are going to create a simple test script.
A typical test program is one that simply says "Hello, World!". Only, "Hello, World!" is a little too simple. To give us some experience with inputs and configuration settings, we'll have it say "Hello,
Finally, to add one more level of complexity, we will create a custom message in a text file and have the gear print that message after all the Hellos. Let's consider these inputs and outputs in the context of a gear:
Input values passed to the function
- Your name
- The number of times to say hello
- A message.txt file
As a bit of a preview of the next part in this tutorial series, there are two ways we can pass these inputs to our gear: as gear config parameters or as gear inputs. Since "your name" and the "number of times to say hello" are simple key-value pairs (e.g., "your name" is the value we want to pass to a variable inside our function), we will consider these as gear config parameters. Our message file is an actual file, however, so it is considered a gear input in Flywheel.
Output generated by function
- Text print of Hellos and contents of message file
- A hello.txt file
Let's get started by creating a run file in our Gear-Building-Tutorial directory.
Step 2. Create Your Run File
We'll start with a simple python script that takes a name and echos "Hello hello.txt
:
Hello section
That takes care of the "Hello" message portion of our run script.
Now we need to add the following lines to read in and print our custom message. First, write the following code to read in a file and print the message:
Print message section
Save this file as run.py in your Gear-Building-Tutorial directory.
run.py script
There is a complete version of the run.py
script at the end of this step.
Next, let's create the custom message our script will print out:
- Create a file called message.txt
- Write any simple text message in it
- Save this file to your Gear-Building-Tutorial directory
If you want to follow along exactly with this tutorial, the custom message we are using is included below.
Complete message.txt file
Complete run.py file
Step 3. Running the script locally
Now we should be able to run this script. In a terminal window, navigate to the gear directory, and enter: python run.py
We should see the following output:
We should also see a new text file, hello.txt
, saved in our gear directory.
Make sure your run file runs successfully first
When you run a gear in Flywheel, the system environment is a little different from the environment you might use on your computer. Sometimes, this can cause problems and errors that you'll need to debug when it comes time to do a test run in Flywheel. It's important to make sure that the algorithm in your script runs successfully on your machine before anything else. That way, if you encounter any errors later on in the development process, you'll know it's not your code, and is likely from environment differences (or input/output issues). The important thing is to ensure that your code itself is not the culprit.
Wrapping up with creating our run file
At this point, we have a working run script. However, if we make this our gear, we'll have no way of changing our name, the number of times to say hello, or the custom message file. To make this program more flexible and run for various inputs and settings, we need to make some modifications. We want to change this script so that the user can input ANY name, set the script to repeat ANY number of times, and have it read ANY .txt
file as the custom message. Making these modifications will require some knowledge about the manifest file.
We'll work these modifications in as we go through Part 5 The Manifest.
Navigation
Previous: Part 3 The Flywheel Environment
Next: Part 5 The Manifest