Create a Nix expression running a Python function
Usage
rxp_py(
name,
py_expr,
additional_files = "",
nix_env = "default.nix",
serialize_function = NULL,
unserialize_function = NULL,
env_var = NULL
)
Arguments
- name
Symbol, name of the derivation.
- py_expr
Character, Python code to generate the expression.
- additional_files
Character vector, additional files to include. Custom functions must go into a script called "functions.py", and additional files that need to be accessible during the build process can be named anything.
- nix_env
Character, path to the Nix environment file, default is "default.nix".
- serialize_function
Character, defaults to NULL. The name of the Python function used to serialize the object. It must accept two arguments: the object to serialize (first), and the target file path (second). If NULL, the default behavior uses
pickle.dump
. Define this function infunctions.py
.- unserialize_function
Character, defaults to NULL. The name of the Python function used to unserialize the object. It must accept one argument: the file path.
- env_var
Character vector, defaults to NULL. A named vector of environment variables before running the Python script, e.g., c(PYTHONPATH = "/path/to/modules"). Each entry will be added as an export statement in the build phase.
Details
At a basic level,
rxp_py(mtcars_am, "mtcars.filter(polars.col('am') == 1).to_pandas()")
is equivalent to
mtcars_am = mtcars.filter(polars.col('am') == 1).to_pandas()
. rxp_py()
generates the required Nix boilerplate to output a so-called "derivation"
in Nix jargon. A Nix derivation is a recipe that defines how to create an
output (in this case mtcars_am
) including its dependencies, build steps,
and output paths.
See also
Other derivations:
rxp_jl()
,
rxp_py_file()
,
rxp_qmd()
,
rxp_r()
,
rxp_r_file()
,
rxp_rmd()
Examples
if (FALSE) { # \dontrun{
rxp_py(
mtcars_pl_am,
py_expr = "mtcars_pl.filter(polars.col('am') == 1).to_pandas()"
)
# Custom serialization
rxp_py(
mtcars_pl_am,
py_expr = "mtcars_pl.filter(polars.col('am') == 1).to_pandas()",
serialize_function = "serialize_model",
additional_files = "functions.py")
} # }