Package: pytask-julia
pytask-juliaRun Julia scripts with pytask.Installationpytask-julia is available onPyPIandAnaconda.org. Install it with$pipinstallpytask-julia#or$condainstall-cconda-forgepytask-juliaYou also need to have Julia installed andjuliaon your command line. Test it by
typing the following on the command line$julia-hIf an error is shown instead of a help page, you can install Julia on Unix systems with$condainstall-cconda-forgejuliaor choose one of the installers on thispage.UsageTo create a task which runs a Julia script, define a task function with the@pytask.mark.juliadecorator. Thescriptkeyword provides a path relative to the
task module to the Julia script.importpytaskfrompathlibimportPathfrompytaskimporttask@task(kwargs={"path":Path("out.csv")})@pytask.mark.julia(script="script.jl")deftask_run_jl_script():passIf you are wondering why the function body is empty, know that pytask-julia replaces the
body with a predefined internal function. See the section on implementation details for
more information.Dependencies and ProductsDependencies and products can be added as usual. Read thistutorial.For example, with the@pytask.taskdecorator as shown before. (The choice of the kwarg
name, herepath, is arbitrary.)Accessing dependencies and products in the scriptTo access the paths of dependencies and products in the script, pytask-julia stores the
information by default in a.jsonfile. The path to this file is passed as a
positional argument to the script. Inside the script, you can read the information.importJSONpath_to_json=ARGS[1]# Contains the path to the .json file.config=JSON.parse(read(path_to_json,String))# A dictionary.config["path"]# Is the path to the output file "../out.csv".The.jsonfile is stored in the same folder as the task in a.pytaskdirectory.To parse the JSON file, you need to installJSON.jl.You can also pass any other information to your script by using the@taskdecorator.@task(kwargs={"path":Path("out.csv"),"number":1})@pytask.mark.julia(script="script.jl")deftask_run_jl_script():passand inside the script useconfig["number"]# Is 1.DebuggingIn case a task throws an error, you might want to execute the script independently from
pytask. After a failed execution, you see the command which executed the Julia script in
the report of the task. It looks roughly like this$julia<options>--script.jl<path-to>/.pytask/task_py_task_example.jsonManaging Julia environmentsJulia has support for environments to execute your tasks viaPkg.jlwhich is explainedhere.pytask-julia allows you define a default environment via yourpytask configuration file.Use thejulia_projectkey to define a path to your configuration file to point to your
environment.Probably your environment filesManifest.tomlandProject.tomlreside at the root of
your project folder as well as your pytask configuration file. Then, the content will
look like this.[tool.pytask.ini_options]julia_project="."You can also define environments for each task which will overwrite any other default
with theprojectkeyword argument. Pass a path to the task module.@task(kwargs={"path":Path("out.csv")})@pytask.mark.julia(script="script.jl",project=".")deftask_run_jl_script():passCommand Line OptionsCommand line options can be pass via theoptionskeyword argument.@task(kwargs={"path":Path("out.csv")})@pytask.mark.julia(script="script.jl",options=["--threads","2"])deftask_run_jl_script():passThis example will execute the script using to threads.Repeating tasks with different scripts or inputsYou can also repeat the execution of tasks, meaning executing multiple Julia scripts or
passing different command line arguments to the same Julia script.The following task executes two Julia scripts,script_1.jlandscript_2.jl, which
produce different outputs.foriinrange(2):@task(kwargs={"path":Path(f"out_{i}.csv")})@pytask.mark.julia(script=f"script_{i}.jl")deftask_execute_julia_script():passIf you want to pass different inputs to the same Julia script, pass these arguments with
thekwargskeyword of the@taskdecorator.foriinrange(2):@task(kwargs={"path":Path(f"out_{i}.csv"),"i":i})@pytask.mark.julia(script="script.jl")deftask_execute_julia_script():passand inside the task access the argumentiwithimportJSONpath_to_json=ARGS[1]# Contains the path to the .json file.config=JSON.parse(read(path_to_json,String))# A dictionary.config["path"]# Is the path to the output file "../output_{i}.csv".config["i"]# Is the number.SerializersYou can also serialize your data with any other tool you like. By default, pytask-julia
also supports YAML (if PyYaml is installed).Use theserializerkeyword arguments of the@pytask.mark.juliadecorator with@pytask.mark.julia(script="script.jl",serializer="yaml")deftask_example():...And in your Julia script useimportYAMLconfig=YAML.load_file(ARGS[1])Note that theYAMLpackage needs to be installed.If you need a custom serializer, you can also provide any callable toserializerwhich
transforms data to a string. Usesuffixto set the correct file ending.Here is a replication of the JSON example.importjson@pytask.mark.julia(script="script.jl",serializer=json.dumps,suffix=".json")deftask_example():...ConfigurationYou can influence the default behavior of pytask-julia with some configuration values.
Place them into apyproject.tomlfile.julia_serializerUse this option to change the default serializer.[tool.pytask.ini_options]julia_serializer="json"julia_suffixUse this option to set the default suffix of the file which contains serialized paths to
dependencies and products and more.[tool.pytask.ini_options]julia_suffix=".json"julia_optionsUse this option to set default options for each task which are separated by whitespace.[tool.pytask.ini_options]julia_options=["--threads",2]julia_projectUse this option to set a default environment for each task. If your environment withManifest.tomlandProject.tomlis defined in the same directory as the configuration
filepyproject.toml, just use a dot.[tool.pytask.ini_options]julia_project="."If the environment files were in a folder next to the configuration file calledenvironmentuse[tool.pytask.ini_options]julia_project="environment"ChangesConsult therelease notesto find out about what is new.DevelopmentAfter setting up the environment, enter$julia$]$pkg>activate.$pkg>instantiateto set up the Julia environment.