# livebook-dev/kino architecture

> Architecture diagram of the GitHub repository livebook-dev/kino, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/livebook-dev/kino
- Repository: https://github.com/livebook-dev/kino

## Overview

##### Project Type and Purpose
- Kino is a library (written in Elixir) used by Livebook to generate rich, interactive outputs from Elixir code. Its role is to enable rendering interactive widgets, charts, and other visual elements (including integrations like databases, maps, charts, etc.) directly within Livebook notebooks.
- This isn’t a full-stack web application or a traditional end-user app—it’s a library that integrates with a host (Livebook) and provides both server-side (Elixir) and client-side (JavaScript/CSS) assets/components.

##### Analyzing the File Structure
- The top-level includes assets (JavaScript/CSS packs), lib (where the core Elixir code lives), and tests (for verifying Kino functionality). Notice the separation between:
- lib/kino: The Elixir modules that implement core functionality. This includes modules for various areas (audio, bridge, control, data_table, debug, remote_execution_cell, smart_cell, etc.) and subdirectories for related functionalities such as JS interoperability (lib/kino/js).
- You also have a standard mix.exs, mix.lock, and related configuration files that establish this as an Elixir project.

##### Key Architectural Components
a. Core Library (Elixir Modules):
- lib/kino and its submodules are responsible for the underlying logic, error handling, communication, and integration with Livebook.
- Modules like kino/bridge.ex, kino/js/live/server.ex, kino/smart_cell/server.ex, and kino/subscription_manager.ex build the backbone that enables interactive output rendering.

b. Assets and Frontend Components:
- These assets are built into bundles (as seen in the build folders) and are used to render rich visuals in the browser.

c. Integration Points:
- Kino is designed to be installed in Livebook via Mix.install/2. This integration is essential as Kino communicates with Livebook’s runtime and renders its content.
- Components such as JS live components (e.g., lib/kino/js/live) handle live reactivity and serve as the bridge between Elixir processes and the browser.

d. Testing and Quality Assurance:
- The test directory showing tests for nearly every core module indicates a robust CI/testing setup.
- Tests are organized to simulate both unit and integration scenarios.

##### Relationships and Interactions
- How Livebook (an external host) calls into Kino’s API, invoking modules in lib/kino.
- Invocation of functionalities from Livebook to Kino.
- Communication between core modules (e.g., kino/bridge.ex working with kino/js/live/server.ex for live updates).
- Rendering flow from build/bundled assets to the resultant interactive component in the browser.

##### Architectural Patterns and Design Principles
- There is a clear separation of concerns:
- Business/logic layer (Elixir modules in lib/kino)
- Presentation layer (client-side JS/CSS in assets/ and lib/assets/)
- Integration/testing (test directory)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Host
    LB["Livebook (External Host)"]:::external

    %% Backend Modules (Elixir)
    subgraph "Backend Modules (Elixir)"
        KL["Core Library"]:::backend
        BM["Bridge Module"]:::backend
        JLS["JS Live Server"]:::backend
        SCM["Smart Cell Manager"]:::backend
        DT_BE["Data Table (Elixir)"]:::backend
        DBG_BE["Debug (Elixir)"]:::backend
        MRMD_BE["Mermaid (Elixir)"]:::backend
        REC_BE["Remote Exec Cell (Elixir)"]:::backend
        TREE_BE["Tree Visualization (Elixir)"]:::backend
    end

    %% Built Assets (from Elixir side)
    subgraph "Built Assets"
        DT_BA["Data Table (Built Assets)"]:::backend
        MRMD_BA["Mermaid (Built Assets)"]:::backend
        REC_BA["Remote Exec Cell (Built Assets)"]:::backend
        TREE_BA["Tree (Built Assets)"]:::backend
    end

    %% Frontend Assets (JS/CSS)
    subgraph "Frontend Assets (JS/CSS)"
        DT_FE["Data Table (JS/CSS)"]:::frontend
        DBG_DEF_FE["Debug Default (JS/CSS)"]:::frontend
        DBG_PIPE_FE["Debug Pipeline (JS/CSS)"]:::frontend
        MRMD_FE["Mermaid (JS/CSS)"]:::frontend
        REC_FE["Remote Exec Cell (JS/CSS)"]:::frontend
        TREE_FE["Tree Visualization (JS/CSS)"]:::frontend
    end

    %% Testing & CI
    subgraph "Testing & CI"
        TS["Test Suite"]:::testing
        CI["CI Workflows"]:::testing
    end

    %% Connections between layers
    LB -->|"calls"| KL

    KL -->|"includes"| BM
    KL -->|"includes"| JLS
    KL -->|"includes"| SCM
    KL -->|"renders"| DT_BE
    KL -->|"renders"| DBG_BE
    KL -->|"renders"| MRMD_BE
    KL -->|"renders"| REC_BE
    KL -->|"renders"| TREE_BE

    %% Module flow from Backend to Built Assets to Frontend
    DT_BE -->|"builds"| DT_BA
    DT_BA -->|"serves"| DT_FE

    DBG_BE -->|"updates"| DBG_DEF_FE
    DBG_BE -->|"updates"| DBG_PIPE_FE

    MRMD_BE -->|"builds"| MRMD_BA
    MRMD_BA -->|"serves"| MRMD_FE

    REC_BE -->|"builds"| REC_BA
    REC_BA -->|"serves"| REC_FE

    TREE_BE -->|"builds"| TREE_BA
    TREE_BA -->|"serves"| TREE_FE

    %% JS Live Server facilitates live reactivity to frontend
    JLS -->|"pushes updates to"| DT_FE
    JLS -->|"pushes updates to"| MRMD_FE
    JLS -->|"pushes updates to"| REC_FE
    JLS -->|"pushes updates to"| TREE_FE
    JLS -->|"pushes updates to"| DBG_DEF_FE
    JLS -->|"pushes updates to"| DBG_PIPE_FE

    %% Testing and CI integrate with Core Library
    TS ---|"verifies"| KL
    CI ---|"triggers on changes in"| KL

    %% Click Events for Backend Modules
    click KL "https://github.com/livebook-dev/kino/tree/main/lib/kino"
    click BM "https://github.com/livebook-dev/kino/blob/main/lib/kino/bridge.ex"
    click JLS "https://github.com/livebook-dev/kino/blob/main/lib/kino/js/live/server.ex"
    click SCM "https://github.com/livebook-dev/kino/blob/main/lib/kino/smart_cell/server.ex"
    click DT_BE "https://github.com/livebook-dev/kino/blob/main/lib/kino/data_table.ex"
    click DBG_BE "https://github.com/livebook-dev/kino/blob/main/lib/kino/debug.ex"
    click MRMD_BE "https://github.com/livebook-dev/kino/blob/main/lib/kino/mermaid.ex"
    click REC_BE "https://github.com/livebook-dev/kino/blob/main/lib/kino/remote_execution_cell.ex"
    click TREE_BE "https://github.com/livebook-dev/kino/blob/main/lib/kino/tree.ex"

    %% Click Events for Built Assets
    click DT_BA "https://github.com/livebook-dev/kino/tree/main/lib/assets/data_table/build"
    click MRMD_BA "https://github.com/livebook-dev/kino/tree/main/lib/assets/mermaid/build"
    click REC_BA "https://github.com/livebook-dev/kino/tree/main/lib/assets/remote_execution_cell/build"
    click TREE_BA "https://github.com/livebook-dev/kino/tree/main/lib/assets/tree/build"

    %% Click Events for Frontend Assets
    click DT_FE "https://github.com/livebook-dev/kino/tree/main/assets/packs/data_table"
    click DBG_DEF_FE "https://github.com/livebook-dev/kino/tree/main/assets/packs/debug_default"
    click DBG_PIPE_FE "https://github.com/livebook-dev/kino/tree/main/assets/packs/debug_pipeline"
    click MRMD_FE "https://github.com/livebook-dev/kino/tree/main/assets/packs/mermaid"
    click REC_FE "https://github.com/livebook-dev/kino/tree/main/assets/packs/remote_execution_cell"
    click TREE_FE "https://github.com/livebook-dev/kino/tree/main/assets/packs/tree"

    %% Click Events for Testing and CI
    click TS "https://github.com/livebook-dev/kino/tree/main/test"
    click CI "https://github.com/livebook-dev/kino/tree/main/.github/workflows"

    %% Styles
    classDef backend fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef frontend fill:#d4edda,stroke:#333,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef testing fill:#fff3cd,stroke:#333,stroke-width:2px;
```
