CurrentRun.ctx()

CurrentRun.ctx() is used access context variables. These can be variables passed into the PrismProject's ctx, or it can be variables passed into the runtime_ctx at runtime.

Here is the full method definition:

CurrentRun.ctx(self, key: str, default_value: Optional[Any] = None) -> Any:
    """
    Get the value associated with context variable `key`. Context variables can be
    set in two places: when instantiated the PrismProject (with the `ctx` keyword
    argument) and when creating the run job (with the `runtime_ctx` keyword argument
    in the PrismProject's `create_run_job` method).

    args:
        key: variable to retrieve
        default_value: default value to return if `key` is not found.
            Default is `None`
    returns:
        value associated with context variable `key`
    """

Example

Here's an example project entrypoint:

# example_project/main.py
import os
from pathlib import Path

from prism.client import PrismProject
from prism.connectors import SnowflakeConnector


snowflake_conn = SnowflakeConnector(
    id="snowflake-connector",
    account=os.getenv("account"),
    user=os.getenv("user"),
    password=os.getenv("password"),
    database=os.getenv("database"),
    schema=os.getenv("schema"),
    warehouse=os.getenv("warehouse"),
    role=os.getenv("role"),
)

project = PrismProject(
    tasks_dir=Path.cwd() / "tasks",
    connectors=[snowflake_conn]
    ctx={
        "OUTPUT": Path.cwd() / "output"
    }
)

if __name__ == "__main__":
    project.run()

Tasks within this project can call the OUTPUT context variable as follows:

# example_project/tasks/extract_from_snowflake.py

from prism.decorators import task, target
from prism.runtime import CurrentRun
import prism.target

@task(
     targets=[
         target(type=prism.target.PandasCsv, loc=CurrentRun.ctx("OUTPUT") / 'snowflake_df.csv')   
     ]
)
def extract_from_snowflake():
    conn = CurrentRun.conn(connector_id="snowflake-connector")
    df = conn.execute_sql(..., return_type="pandas")
    return df

Last updated