Overview

State management in Davia provides a clean way to work with persistent data using Python’s type annotations. By leveraging the Annotated type and State class, you can easily access and modify state between different tasks.

Working with State

Initializing State

Initialize state during app creation:

from davia import Davia, State
import pandas as pd

products = pd.DataFrame({
    "name": ["Laptop", "Phone", "Headphones"],
    "price": [999.99, 699.99, 199.99],
    "quantity": [1, 1, 2],
})

app = Davia(
    title="Product Manager",
    description="Manage your shopping list with ease",
    state={"products": products},
)

Accessing State

Davia provides two ways to access state:

Method 1: Using Annotated Type

from typing import Annotated

@app.task
def add_product(
    products: Annotated[pd.DataFrame, State("products")],
    name: str,
    price: float,
    quantity: int = 1,
):
    new_product = pd.DataFrame({"name": [name], "price": [price], "quantity": [quantity]})
    products.loc[len(products)] = new_product.iloc[0]

Method 2: Accessing Complete State

@app.task
def get_products(state: State) -> dict:
    return state["products"].to_dict()

Modifying State

State modifications are applied directly to the parameter:

@app.task
def update_price(
    products: Annotated[pd.DataFrame, State("products")],
    product_name: str,
    new_price: float,
):
    products.loc[products["name"] == product_name, "price"] = new_price

Supported State Types

Davia supports:

  • Pandas DataFrames
  • Dictionaries
  • Lists
  • Custom objects
  • Primitive types (within a container)