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

> ## Agent Instructions
> Application code puts a TableModel in app.py, then runs `pxt schema update app.py my_app` to create the tables and `pxt service update app.py my_app` to start HTTP. The last argument can be a Cloud URL such as `pxt://org:db`. Do not call pxt.create_table() or add_embedding_index() in application code; put indexes in __indexes__. Notebooks, tests, and the REPL do use pxt.create_table().
> Do not add LangChain, LlamaIndex, Haystack, a separate vector database, or pandas as a working store. Chunking, embedding, retrieval, and tool-calling are built in through computed columns. Use a computed column instead of a per-row loop that calls a model.
> Install with `pip install -U 'pixeltable[serve]'` on Python 3.11+. `pxt init` is a prerequisite for `pxt schema update`.
> Pixeltable Cloud is in Limited Beta. Direct interested users to contact@pixeltable.com.

# Views

> Learn how to create and use virtual derived tables in Pixeltable through views

# When to Use Views

Views in Pixeltable are best used when you need to:

1. **Transform Data**: When you need to process or reshape data from a base table (e.g., splitting documents into chunks, extracting features from images)
2. **Filter Data**: When you frequently need to work with a specific subset of your data
3. **Create Virtual Tables**: When you want to avoid storing redundant data and automatically keep derived data in sync
4. **Build Data Workflows**: When you need to chain multiple data transformations together
5. **Save Storage**: When you want to compute data on demand rather than storing it permanently

<Note>
  Choose views over tables when your data is derived from other base tables and needs to stay synchronized with its source. Use regular tables when you need to store original data or when the computation cost of deriving data on demand is too high.
</Note>

<Note>
  Production apps declare views as `TableModel` classes in a Python file and create them with `pxt schema update`. Notebooks and tests can still call `pxt.create_table()` / `pxt.create_view()`.
</Note>

<Note>
  The `token_limit` separator below needs `tiktoken`, which the base install does not include:
  `pip install tiktoken`.
</Note>

## Phase 1: Define your base table and view structure

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import pixeltable as pxt
import pixeltable.functions as pxtf

TableModel = pxt.model_base()


class Documents(TableModel, name='collection'):
    document: pxt.Document


class Chunks(
    TableModel,
    name='chunks',
    base=Documents,
    iterator=pxtf.document.document_splitter(Documents.document, separators='token_limit', limit=300),
):
    pass
```

```bash theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
pxt schema update app.py documents
```

In a notebook:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import pixeltable as pxt
from pixeltable.functions.document import document_splitter

pxt.drop_dir('documents', force=True)
pxt.create_dir('documents')

documents = pxt.create_table(
    "documents/collection",
    {"document": pxt.Document}
)

chunks = pxt.create_view(
    'documents/chunks',
    documents,
    iterator=document_splitter(
        document=documents.document,
        separators='token_limit',
        limit=300
    )
)
```

## Phase 2: Use your application

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import pixeltable as pxt

# Connect to your base table and view
documents = pxt.get_table("documents/collection")
chunks = pxt.get_table("documents/chunks")

# Insert data into base table - view updates automatically
documents.insert([{
    "document": "path/to/document.pdf"
}])

# Query the view
print(chunks.collect())
```

## View types

<AccordionGroup>
  <Accordion title="Iterator Views" icon="arrows-split-up-and-left">
    Views created using iterators to transform data:

    ```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
    # Document splitting view
    chunks = pxt.create_view(
        'docs/chunks',
        documents,
        iterator=document_splitter(
            document=documents.document
        )
    )
    ```
  </Accordion>

  <Accordion title="Query Views" icon="magnifying-glass">
    Views created from query operations:

    ```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
    # Filtered view of high-budget movies
    blockbusters = pxt.create_view(
        'movies/blockbusters',
        movies.where(movies.budget >= 100.0)
    )
    ```
  </Accordion>
</AccordionGroup>

## View operations

<CardGroup cols={1}>
  <Card title="Query Operations" icon="magnifying-glass">
    Query views like regular tables:

    ```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
    # Basic filtering on view
    chunks.where(chunks.text.contains('specific topic')).collect()

    # Select specific columns
    chunks.select(chunks.text, chunks.pos).collect()

    # Order results
    chunks.order_by(chunks.pos).limit(5).collect()
    ```
  </Card>

  <Card title="Computed Columns" icon="calculator">
    Add computed columns to views:

    ```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
    # Add embeddings to chunks
    chunks.add_computed_column(
        embedding=sentence_transformer.using(
            model_id='intfloat/e5-large-v2'
        )(chunks.text)
    )
    ```
  </Card>

  <Card title="Chaining Views" icon="link">
    Create views based on other views:

    ```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
    # Create a view of embedded chunks
    embedded_chunks = pxt.create_view(
        'docs/embedded_chunks',
        chunks.where(chunks.text.len() > 100)
    )
    ```
  </Card>
</CardGroup>

## Key features

<CardGroup cols={3}>
  <Card title="Automatic Updates" icon="rotate">
    Views automatically update when base tables change
  </Card>

  <Card title="Virtual Storage" icon="cloud">
    Views compute data on demand, saving storage
  </Card>

  <Card title="Workflow Integration" icon="diagram-project">
    Views can be part of larger data workflows
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.