> ## Documentation Index
> Fetch the complete documentation index at: https://docs.datazone.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Context

> Each pipeline in Datazone has a context object that provides access to resources and configuration settings.

```python theme={null}
@transform
def my_transform(context):
    context.state.write(key="my_key", value="my_value")
```

## State

The context object provides a `state` attribute that allows you to read and write key-value pairs.
The state is stored in database and can be accessed in any transform function in the pipeline.
Also you can access the state in the next executions of the pipeline.

```python theme={null}
from datazone import transform

@transform
def my_transform(context):
    context.state.write(key="my_key", value="my_value")


@transform(depends=[my_transform])
def my_transform(context):
    value = context.state.read(key="my_key")
    print(value)
```

### State Callbacks

You can write your states by various conditions like `success`, `failure`, `now`.

```python theme={null}
from datazone import transform

@transform
def my_transform(context):
    context.state.write(key="my_key", value="my_value", on="success")
    raise Exception("Error")
```

In this example, the state will not be written because the transform function raises an exception.

## Resources

### PySpark Session

You can access the PySpark session using the `pyspark` attribute of the `resources` object.

```python theme={null}
from datazone import transform

@transform
def my_transform(context):
    spark = context.resources["pyspark"].spark

    # Use the PySpark session
    df = spark.createDataFrame({
        "name": ["Alice", "Bob"],
        "age": [25, 30]
    })

    return df
```
