Workbook API lifecycle
The Workbook API is the server boundary for the workbook resource in Insights. It lets an allowed user find a workbook, load its related analysis items, change its structure, share access, and move a complete workbook between storage and template sources. The main API groups are in insights/…/workbooks.py; workbook document behavior is in insights/…/insights_workbook.py.
Resource model
Workbook resource
An Insights Workbook is a named container for queries, charts, dashboards, and folders. Queries and charts can belong to folders, charts can use queries, and dashboards can place charts and filters into a view. The server returns these relationships with the workbook so the client can present one coherent resource rather than a set of unrelated documents. The document also keeps internal markers for template origin and imported state so template-based workbooks can be identified and compared. See Workbooks, Workbook interface, and Workbook templates for the related product views.
References: insights/…/insights_workbook.py• L183-236 insights/…/insights_workbook.json• L15-47 frontend/…/workbook.types.ts• L23-64
Resource ownership
The workbook is the main access boundary for its child resources. A user who can edit a workbook can organize its folders and create or change linked analysis items; a user with view access receives the resource as read-only. The permission system also checks workbook access when a new child resource is linked to a workbook. See Authorization and users for the wider access model.
References: insights/permissions.py• L91-123 insights/permissions.py• L195-219 insights/…/insights_workbook.json• L72-96
API surface
Workbook listing
insights.api.workbooks.get_workbooks returns workbooks from sources that the current user can read. The caller can search by title, limit the result, select owned or shared workbooks, and apply filters based on workbook content. Each list item also carries its owner, change times, sharing summary, data sources, and last-opened time so a list screen does not need separate requests for those facts.
| Input | Meaning |
|---|---|
search_term | Matches workbook titles. |
limit | Caps the number of returned workbooks. |
sources | created selects the current user's workbooks. shared selects workbooks given to the user by someone else. others selects workbooks outside those groups and is available to administrators. The default selects created and shared workbooks. |
filters | Filters by workbook, query, chart, dashboard, data source, or table name. |
References: insights/…/workbooks.py• L40-105 frontend/…/workbooks.ts• L10-30
Workbook load and save
The standard Frappe document API creates, loads, saves, and deletes the workbook record. A new workbook receives a server name and a default title when the caller does not provide one. A loaded workbook includes compact lists of its folders, queries, charts, and dashboards. The client uses the workbook document methods for normal changes and uses the named server methods for actions that affect several related resources. The open flow can call track_view to record a view, with a short time gap between repeated view records for the same user.
When a workbook is deleted, the server tries to keep a portable backup before it deletes the folders, queries, charts, and dashboards. If backup creation fails, deletion still proceeds after the error is logged. If a workbook record is later inserted with a backup, the server restores the child resources and reconnects their links. This keeps the visible workbook tree from retaining child records that no longer have a parent while allowing the workbook contents to be recovered.
References: insights/…/insights_workbook.py• L39-181 insights/…/insights_workbook.py• L183-236 frontend/…/workbook.ts• L412-447
Workbook content actions
The workbook document exposes actions for copying, importing individual analysis items, exporting the full workbook, and viewing query lineage. These actions preserve the workbook as the unit of work while allowing a user to move content into or out of it.
| Action | Result |
|---|---|
duplicate | Creates a new workbook with copied folders, queries, charts, dashboards, and remapped links. |
export | Produces a portable workbook package with the workbook definition and its active related content. |
import_workbook | Creates a new workbook from a portable package. |
import_query / import_chart | Adds one portable analysis item to an existing workbook. |
get_lineage_graph | Returns the query relationships and their upstream table or query sources for an administrator. |
The import and duplicate paths create new resource identities. They insert referenced queries before the queries that use them, then update links so charts still point to the new queries and dashboards still point to the new charts. A reference to a query outside the package remains a reference to that existing query, subject to the importing user's access. This prevents copied workbooks from changing or displaying content from the source workbook while preserving deliberate links to existing data.
References: insights/…/insights_workbook.py• L261-452 insights/…/insights_workbook.py• L455-511
Workbook structure lifecycle
Folder organization
Folders provide separate query and chart groups inside a workbook. The folder API can create, rename, delete, expand, and move items, while the sort-order API updates the order of folders and items together. Each move or order change checks that every named folder or item belongs to the target workbook.
Deleting a folder normally moves its queries and charts to the workbook root. This keeps the items available while removing only the grouping choice. A caller that needs a different result must set move_items_to_root explicitly.
Sort updates tolerate items that were deleted after the client read the workbook. A deleted folder is treated as the root during an order update, while an item or folder held by another workbook is refused.
References: insights/…/workbooks.py• L228-392 insights/…/insights_folder.json• L21-53 frontend/…/workbook.ts• L309-364
Sharing access
Sharing uses document shares on the workbook. get_share_permissions returns direct user permissions and the workbook owner's identity. update_share_permissions replaces the direct user list and can set organization-wide access to view or edit.
Only Insights users can be named in the direct user list, with Administrator allowed for template-created workbooks. Organization access is separate from direct access: view access allows people in the organization to read the workbook, while edit access also allows changes. The same workbook permission then controls access to its related analysis resources. The update operation replaces removed direct shares and records newly granted user or organization access.
The sharing lifecycle is:
sequenceDiagram
participant Owner
participant API
participant Shares
participant Reader
Owner->>API: get_share_permissions(workbook)
API->>Shares: read direct and organization shares
Shares-->>API: current access
API-->>Owner: permission model
Owner->>API: update_share_permissions(...)
API->>Shares: replace direct shares and organization share
Reader->>API: get_workbooks or load workbook
API-->>Reader: workbook data with read_only state
References: insights/…/workbooks.py• L20-37 insights/…/workbooks.py• L116-225 insights/permissions.py• L195-219 frontend/…/workbook.ts• L193-230
View tracking
track_view records a workbook view for the current user only when a recent matching view does not already exist. The list API uses these records to return a view count. A template-origin workbook also emits a daily usage signal when it is opened.
References: insights/…/insights_workbook.py• L238-260 insights/…/workbooks.py• L65-89
Template lifecycle
Template discovery
Workbook templates are packaged by installed apps and exposed through the insights_workbooks hook. Each template has a qualified ID in the form app/folder, a manifest, and a workbook definition. The template API discovers these entries at run time, hides entries whose required apps are not installed, and reports whether source data exists for the template's source doctypes.
The template list can include a description, notes, module, version, preview image, imported workbook, and update state. See Workbook templates for the template authoring and library view.
References: insights/hooks.py• L30-36 insights/…/templates.py• L14-20 insights/…/templates.py• L64-153 insights/…/templates.py• L173-218
Template import
insights.api.templates.create_workbook_from_template imports a template into one shared workbook for the site. The operation is restricted to Insights Admin users and requires all apps named by the template. The new workbook is marked with its template origin, owned by Administrator, and shared with the organization for read access.
The API returns the workbook name and its first dashboard so the caller can open a useful landing view. A repeated import request returns the existing shared workbook instead of creating another copy.
flowchart LR
A[Template library] --> B{Required apps present}
B -- No --> C[Template unavailable]
B -- Yes --> D[Admin imports template]
D --> E[Create workbook and child resources]
E --> F[Record origin and version]
F --> G[Administrator-owned organization view copy]
G --> H[Return workbook and first dashboard]
References: insights/…/templates.py• L268-320 insights/…/templates.py• L323-350 insights/…/test_workbook_templates.py• L119-197
Template update
The template version and an imported content fingerprint describe the state of a template-created workbook. When a newer shipped version exists, the library marks an update as available. A migration updates a pristine copy without a user action; a customized copy is left unchanged and is marked for a manual update.
update_workbook_from_template applies the shipped definition to the existing workbook name. This keeps its URL and shares while replacing its related content, so an administrator must treat the action as a content replacement. The update path also restores the shared ownership and access model after rebuilding the content.
References: insights/…/templates.py• L323-350 insights/…/templates.py• L353-404 insights/…/templates.py• L407-437 insights/…/test_workbook_templates.py• L258-346
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to the workbook summary must stay aligned with the serialized child lists and the
read_onlyflag consumed by the workbook client. - Changes to export or import content must preserve the remapping of query, chart, dashboard, and filter links. Keep the order rule for in-package query references and the access behavior for references outside the package.
- Changes to workbook sharing must remain consistent with permission checks for child queries, charts, and dashboards. Keep the special
Administratorcase for template-created workbooks aligned with share validation. - Changes to workbook list sources or filters must keep source selection, content matching, data-source summaries, and last-opened data aligned with the list client.
- Changes to folder types or sort data must stay compatible with item placement, same-workbook checks, deleted-item handling, and bulk sort updates.
- Changes to deletion or restore behavior must preserve the backup boundary and the child-resource cleanup or recreation sequence.
- Changes to template version or content fields must preserve the distinction between a pristine copy and a locally edited copy.
- Changes to template discovery must keep qualified IDs, required-app filtering, and safe template resolution aligned.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A caller names a user who is not an Insights user or Administrator in a direct share | The share request is rejected. | Validate the complete user list before saving shares. |
| A reader attempts a folder or sort change on a view-shared workbook | The request fails with a permission error. | Check workbook write access before structure changes and expose read_only to clients. |
| A folder from another workbook is used for an item | The move request is rejected and the item is not moved. | Validate that the folder and item have the same workbook. |
| A sort request names an item from another workbook | The update is rejected instead of changing the other workbook. | Validate the workbook owner of every named item before writing its order. |
| An imported workbook contains a circular query reference | The import is rejected because the referenced queries cannot be created in a safe order. | Reject the package when no remaining query has all of its in-package references placed. |
| An imported item points to a query outside the package that the importer cannot read | The import may retain a link that cannot be used by that user. | Treat outside-package references as existing resources and apply normal read permission checks. |
| A template needs an app that is not installed | The template is hidden or an import request fails. | Check required_apps during listing and before import or update. |
| A template ID is unknown or uses a path traversal value | No template file is resolved. | Resolve only IDs found in the discovered template registry. |
| A template update reaches a customized imported workbook | Automatic migration leaves the workbook unchanged; a manual update can replace its contents. | Compare the stored import fingerprint with current exported content and warn before a manual replacement. |
| A workbook is deleted | Its backup is created when possible; if export fails, deletion still proceeds and its folders, queries, charts, and dashboards are deleted. | Treat workbook deletion as deletion of the complete workbook tree and use the backup only through the restore path. |
Key files
| File | Why to read it |
|---|---|
| insights/…/workbooks.py | Defines workbook listing, import, sharing, folder, and ordering API groups. |
| insights/…/insights_workbook.py | Defines the workbook document lifecycle, portable content actions, view tracking, restoration, and relationship remapping. |
| insights/…/templates.py | Defines template discovery, site-wide import, version checks, and update behavior. |
| insights/permissions.py | Explains how workbook ownership and shares control access to the workbook tree. |
| frontend/…/workbook.ts | Shows how the client calls the workbook document actions and server APIs. |
| insights/…/insights_workbook.json | Defines the workbook identity, title, template markers, restore backup, and role-level document permissions. |
| insights/…/test_workbook.py | Demonstrates the supported workbook, organization, copy, import, and sharing lifecycles. |
| insights/…/test_workbook_templates.py | Demonstrates template availability, shared import, version update, and customization behavior. |