echo 'Here are the contents of my_file.txt' > my_file.txtAdd resources
Accessing additional resources inside a Viash component.
If a script needs access to an external file, it needs to be added as a resource in the config.
First, create a file called my_file.txt.
echo 'Here are the contents of my_file.txt' > my_file.txtecho 'Here are the contents of my_file.txt' > my_file.txtecho 'Here are the contents of my_file.txt' > my_file.txtecho 'Here are the contents of my_file.txt' > my_file.txtecho 'Here are the contents of my_file.txt' > my_file.txtNext, the file needs to be added to the config as a resource. This will let Viash know to copy the new file inside a component’s resource directory.
name: example_bash
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: bash_script
path: script.sh
- path: my_file.txt
engines:
- type: docker
image: bash:4.0
- type: native
runners:
- type: executable
- type: nextflowname: example_csharp
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: csharp_script
path: script.csx
- path: my_file.txt
engines:
- type: docker
image: ghcr.io/data-intuitive/dotnet-script:1.3.1
- type: native
runners:
- type: executable
- type: nextflowname: example_js
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: javascript_script
path: script.js
- path: my_file.txt
engines:
- type: docker
image: node:19-bullseye-slim
- type: native
runners:
- type: executable
- type: nextflowname: example_python
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: python_script
path: script.py
- path: my_file.txt
engines:
- type: docker
image: python:3.10-slim
- type: native
runners:
- type: executable
- type: nextflowname: example_r
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: r_script
path: script.R
- path: my_file.txt
engines:
- type: docker
image: eddelbuettel/r2u:22.04
- type: native
runners:
- type: executable
- type: nextflowname: example_scala
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: scala_script
path: script.scala
- path: my_file.txt
engines:
- type: docker
image: sbtscala/scala-sbt:eclipse-temurin-19_36_1.7.2_2.13.10
- type: native
runners:
- type: executable
- type: nextflowLastly, to access a resource from within the script, use the resources_dir meta-variable:
#!/bin/bash
## VIASH START
par_input=path/to/file.txt
par_output=output.txt
## VIASH END
# view resource file
cat "$meta_resources_dir/my_file.txt"
# copy file
echo "Copying '$par_input' to '$par_output'."
cp -r "$par_input" "$par_output"using System.IO;
// VIASH START
var par = new {
input = "path/to/file.txt",
output = "output.txt"
};
// VIASH END
// view resource file
string myFile = $"{meta.resources_dir}/my_file.txt";
string text = File.ReadAllText(myFile);
Console.WriteLine(text);
// copy file
Console.WriteLine($"Copying '{par.input}' to '{par.output}'.");
File.Copy(par.input, par.output, true);const fs = require('fs');
// VIASH START
let par = {
'input': 'path/to/file.txt',
'output': 'output.txt'
};
// VIASH END
// view resource file
const my_file = `${meta['resources_dir']}/my_file.txt`
fs.readFile(my_file, 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
// copy file
console.log(`Copying '${par['input']}' to '${par['output']}'`)
fs.copyFile(par['input'], par['output'], (err) => {
if (err) throw err;
});import shutil
## VIASH START
par = {
'input': 'file.txt',
'output': 'output.txt'
}
## VIASH END
# view resource file
my_file = f"{meta['resources_dir']}/my_file.txt"
with open(my_file, "r") as f:
print(f.read())
# copy file
print(f"Copying '{par['input']}' to '{par['output']}'.")
shutil.copyfile(par['input'], par['output'])## VIASH START
par <- list(
"input" = 'file.txt',
"output" = 'output.txt'
)
## VIASH END
# view resource file
lines <- readLines(paste0(meta$resources_dir, "/my_file.txt"))
cat(lines, sep = "\n")
# copy file
cat("Copying '", par$input, "' to '", par$output, "'.\n", sep = "")
file.copy(par$input, par$output)import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.Files
import java.nio.file.Paths
// VIASH START
case class ViashPar(input: String, output: String)
val par = ViashPar(
"path/to/file.txt",
"output.txt"
)
// VIASH END
// view resource file
val myFile = s"${meta.resources_dir}/my_file.txt"
val src = scala.io.Source.fromFile(myFile)
src.getLines.foreach(println)
// copy file
println(s"Copying '${par.input}' to '${par.output}'.")
val fileIn = Paths.get(par.input)
val fileOut = Paths.get(par.output)
Files.copy(fileIn, fileOut, REPLACE_EXISTING)Now we can run the component as follows:
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt[notice] Checking if Docker image is available at 'example_bash:latest'
[warning] Could not pull from 'example_bash:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_bash:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/bash/config.vsh.yaml' to '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/bash/foo.txt'.
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt[notice] Checking if Docker image is available at 'example_csharp:latest'
[warning] Could not pull from 'example_csharp:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_csharp:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/csharp/config.vsh.yaml' to '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/csharp/foo.txt'.
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt[notice] Checking if Docker image is available at 'example_js:latest'
[warning] Could not pull from 'example_js:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_js:latest' with Dockerfile
Copying '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/js/config.vsh.yaml' to '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/js/foo.txt'
Here are the contents of my_file.txt
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt[notice] Checking if Docker image is available at 'example_python:latest'
[warning] Could not pull from 'example_python:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_python:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/python/config.vsh.yaml' to '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/python/foo.txt'.
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt[notice] Checking if Docker image is available at 'example_r:latest'
[warning] Could not pull from 'example_r:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_r:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/r/config.vsh.yaml' to '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/r/foo.txt'.
[1] TRUE
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt[notice] Checking if Docker image is available at 'example_scala:latest'
[warning] Could not pull from 'example_scala:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_scala:latest' with Dockerfile
warning: 1 deprecation
warning: 1 deprecation (since 2.13.3)
warning: 2 deprecations in total; re-run with -deprecation for details
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/scala/config.vsh.yaml' to '/viash_automount/tmp/RtmpjGzUow/add-dependencies632e6641fc99/scala/foo.txt'.