Passing App Panel's Parameters to the Script
Was this helpful?
Was this helpful?
When running a Capsule in Code Ocean, the system looks and executes the Run file. The Run file is a bash script that can be auto-generated by the system and modified manually afterward.
For example, the screenshot below shows the Run script of a sample file provided by Code Ocean.
The Run script is auto-generated and it calls the main.R
on line 8. with this parameter "$@". This means passing all the parameters to the script that it calls.
The way to access the parameters as command-line arguments in the main script can vary, depending on the programming language and the method of the parameter.
Ordered Parameters are selected by default. In this method, the arguments will be passed in order. All the sample scripts accommodate this method and can be easily modified to fit your script.
From the above example, the command in R for accessing the command line arguments is in line 5. It reads all the arguments and saves it in the args
variables to access later in line 9 to line 11.
In the App Builder section, the number of the order of the parameter is indicated within the [brackets].
Alternatively, arguments can be accessed by name if the Named Parameters is selected. The way to access Named Parameters is different from the Order Parameters and varies between languages.
Similar to the order parameters, the name of the parameter is indicated in the [brackets].
Instead of calling and handling the input in the main script as in both coding examples above, you can use a config file for a Capsule to handle the input. Since the run script is a bash language, the config file is often a bash script for a Capsule, but it can also be another programming language.
Let's take Code Ocean Apps Dose response as an example Capsule. It uses an R config file to manage the input. Here is the App Panel for this Capsule. The Order Parameter method was selected and it takes one input file.
This is the config.R
script
To explain the script, line 5 takes all the inputs as we saw in the earlier example. Then it checks for the input in line 8 with a if
statement. It checks if there is no input from the App panel, then the system will execute line 9, using default data in the /data
folder. If there is an input provided, then it will access the input with arg[1]
. Finally, it prints out the file that will be used in line 13 and loads the file in line 16.
Here is a code snip of the main script (/code/plot_dose_response.R).
Line 3-7 loads all the packages, and line 9 calls config.R file to access the parameters from the App Panel.
Below is the Named Parameter version of the sample script for Python with comments for the purpose of each step. See Python's for more details.
The config file doesn't come with the sample file that is auto-generated by the system. However, most of the Capsule in the are structured this way. Once the config file is set up, don't forget to call the file before the main process.