Skip to content

feat: Add table state to Code and SQL blocks. - #34

Merged
Artmann merged 6 commits into
mainfrom
chris/add-table-state
Oct 14, 2025
Merged

Artmann merged 6 commits into
mainfrom
chris/add-table-state

Conversation

@Artmann

@Artmann Artmann commented Oct 8, 2025 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Code and SQL blocks now persist and apply table view settings (sorting, filters, pagination, column order, hidden columns, display names, conditional formatting, wrapped text) before execution.
  • Bug Fixes
    • Improved escaping of special characters in generated code and table-state payloads to prevent formatting/execution issues.
  • Documentation
    • Added a comprehensive developer guide describing workflows, commands, and contribution standards.
  • Tests
    • Expanded tests covering table-state serialization, escaping, and emitted code behavior.

@coderabbitai

coderabbitai Bot commented Oct 8, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

  • Adds CLAUDE.md developer guide.
  • Introduces createDataFrameConfig to emit a dedented Python prelude that configures dataframe formatting via _dntk when available, else assigns to _deepnote_current_table_attrs.
  • Extends CodeBlockMetadata and SqlBlockMetadata with deepnote_table_state fields.
  • Modifies code and SQL block generators to prefix emitted code with the dataframe-config prelude.
  • Standardizes ts-dedent imports to named import across files and tests.
  • Adds extensive tests for dataframe configuration generation, JSON escaping, and integration with code and SQL block outputs.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as Dev
  participant TS as Block renderer (TS)
  participant DF as createDataFrameConfig
  participant PY as Python runtime
  participant GS as Global state

  Dev->>TS: Render CodeBlock
  TS->>DF: build prelude from deepnote_table_state
  DF-->>TS: dedented Python prelude
  rect rgba(200,220,255,0.25)
    note over PY,GS: Guarded prelude injected before user code
    TS->>PY: if '_dntk' in globals(): _dntk.dataframe_utils.configure_dataframe_formatter(JSON)
    TS->>GS: else: _deepnote_current_table_attrs = JSON
  end
  TS->>PY: Execute user code content
Loading
sequenceDiagram
  autonumber
  actor Dev as Dev
  participant TS as Block renderer (TS)
  participant DF as createDataFrameConfig
  participant PY as Python runtime
  participant GS as Global state

  Dev->>TS: Render SqlBlock
  TS->>DF: build prelude from deepnote_table_state
  DF-->>TS: dedented Python prelude
  rect rgba(200,220,255,0.25)
    note over PY,GS: Guarded prelude injected before SQL execution
    TS->>PY: if '_dntk' in globals(): configure_dataframe_formatter(JSON)
    TS->>GS: else: _deepnote_current_table_attrs = JSON
  end
  alt variable assigned
    TS->>PY: var = _dntk.execute_sql(...)
  else direct execution
    TS->>PY: _dntk.execute_sql(...)
  end
Loading

Possibly related PRs

Suggested reviewers

  • andyjakubowski
  • jamesbhobbs
  • saltenasl

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly summarizes the main feature in this PR, which adds table state support to Code and SQL blocks. It aligns with the new TableState interface, the deepnote_table_state metadata in code and SQL block implementations, and corresponding Python code generator updates. The phrasing is concise and specific enough to inform reviewers of the key change. It avoids unnecessary detail while remaining descriptive.

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Oct 8, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.80%. Comparing base (ef94700) to head (e064c04).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #34      +/-   ##
==========================================
+ Coverage   93.59%   93.80%   +0.20%     
==========================================
  Files          19       21       +2     
  Lines         593      613      +20     
  Branches      120      182      +62     
==========================================
+ Hits          555      575      +20     
  Misses         38       38              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 322e3a3 and f8336f7.

📒 Files selected for processing (7)
  • CLAUDE.md (1 hunks)
  • packages/blocks/src/blocks.ts (1 hunks)
  • packages/blocks/src/blocks/code-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/data-frame.test.ts (1 hunks)
  • packages/blocks/src/blocks/data-frame.ts (1 hunks)
  • packages/blocks/src/blocks/sql-blocks.ts (2 hunks)
  • packages/blocks/src/python-code.test.ts (7 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
packages/blocks/src/blocks/code-blocks.ts (1)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (6-16)
packages/blocks/src/blocks/data-frame.test.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (8-12)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (6-16)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/python-code.test.ts (4)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (8-12)
packages/blocks/src/index.ts (1)
  • createPythonCode (4-4)
packages/blocks/src/python-code.ts (1)
  • createPythonCode (27-81)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/data-frame.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (8-12)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/python-utils.ts (1)
  • escapePythonString (1-7)
🔇 Additional comments (9)
packages/blocks/src/blocks.ts (1)

39-40: LGTM!

The index signature enables flexible metadata storage for table state and other dynamic properties. Comment appropriately documents the rationale.

packages/blocks/src/blocks/data-frame.ts (1)

6-16: LGTM!

The function correctly extracts table state, serializes to JSON, and emits conditional Python boilerplate. The use of escapePythonString ensures safe embedding of JSON in Python strings.

CLAUDE.md (1)

1-184: LGTM!

Comprehensive development guide that aligns well with the PR's changes. Clear workflow documentation and package-specific guidance.

packages/blocks/src/blocks/code-blocks.ts (1)

14-22: LGTM!

The function now correctly prepends the dataframe configuration before block content. The dedent usage ensures clean formatting.

packages/blocks/src/blocks/sql-blocks.ts (1)

50-62: LGTM!

The dataframe configuration is correctly injected in both the undefined-variable and defined-variable paths, ensuring consistent behavior.

packages/blocks/src/blocks/data-frame.test.ts (1)

8-214: LGTM!

Comprehensive test coverage including edge cases (empty state, missing metadata, special characters, complex state). Tests verify both code and SQL block scenarios.

packages/blocks/src/python-code.test.ts (3)

169-282: LGTM!

Tests correctly validate the augmented output structure with dataframe configuration preface. Good coverage of various table state scenarios (empty, with state, custom page size, hidden columns).


590-735: LGTM!

SQL block tests appropriately updated to include dataframe configuration. Tests cover all execution paths (with/without variable, different return types, special characters).


737-823: LGTM!

New tests for SQL blocks with table state provide comprehensive validation of the configuration injection and JSON serialization.

@Artmann
Artmann force-pushed the chris/add-table-state branch from f8336f7 to 624d676 Compare October 8, 2025 17:27

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f8336f7 and 624d676.

📒 Files selected for processing (7)
  • CLAUDE.md (1 hunks)
  • packages/blocks/src/blocks.ts (1 hunks)
  • packages/blocks/src/blocks/code-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/data-frame.test.ts (1 hunks)
  • packages/blocks/src/blocks/data-frame.ts (1 hunks)
  • packages/blocks/src/blocks/sql-blocks.ts (2 hunks)
  • packages/blocks/src/python-code.test.ts (7 hunks)
🧰 Additional context used
🧬 Code graph analysis (5)
packages/blocks/src/python-code.test.ts (4)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (8-12)
packages/blocks/src/python-code.ts (1)
  • createPythonCode (27-81)
packages/blocks/src/index.ts (1)
  • createPythonCode (4-4)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/sql-blocks.ts (1)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (6-16)
packages/blocks/src/blocks/data-frame.test.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (8-12)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (6-16)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/code-blocks.ts (1)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (6-16)
packages/blocks/src/blocks/data-frame.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (8-12)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/python-utils.ts (1)
  • escapePythonString (1-7)
🔇 Additional comments (10)
packages/blocks/src/blocks/code-blocks.ts (1)

15-21: LGTM: DF config prelude before user code

The dedent wrapper + injected dataFrameConfig is clean and consistent with SQL blocks.

packages/blocks/src/blocks/sql-blocks.ts (3)

41-49: Good fix: wrap execute_sql call with dedent

This resolves unintended indentation/whitespace. Injection of dataFrameConfig above it is correct.


52-56: Undefined-variable path handled correctly

Returning a dedented block with dataFrameConfig then the call is consistent with the assignment path.


60-64: Assignment path looks right

dataFrameConfig precedes assignment and expression echo; formatting is consistent.

CLAUDE.md (1)

173-173: Build Tool “tsdown” is correct
Referenced in package.json and used in build scripts for blocks and hello-world; no doc change needed.

packages/blocks/src/blocks/data-frame.test.ts (1)

1-215: Excellent test coverage.

Comprehensive suite covering empty state, populated state, SQL blocks, missing metadata, special character escaping, and complex scenarios. The escaping test (lines 144-168) is particularly thorough, correctly validating that escapePythonString handles both single quotes (\') and backslashes in JSON-encoded strings.

packages/blocks/src/python-code.test.ts (4)

169-176: Consistent boilerplate addition.

Correctly updated to expect the dataframe config boilerplate before the user code. Defaults to '{}' when deepnote_table_state is absent from metadata.


179-225: Good coverage of table state scenarios.

Validates full JSON serialization of table state fields (pageSize, pageIndex, columnOrder, etc.) and confirms the boilerplate is properly prepended to user code.


590-604: Consistent SQL block updates.

All SQL block tests properly updated to include the dataframe config boilerplate before _dntk.execute_sql calls. Good consistency across assignment and non-assignment paths.

Also applies to: 622-635, 655-669, 687-701, 720-734


737-792: Comprehensive SQL block with table state.

Validates the full flow: table state JSON serialization → boilerplate → execute_sql call. Expected output correctly includes pageSize/pageIndex and column configuration.

Comment thread packages/blocks/src/blocks.ts Resolved
@Artmann
Artmann force-pushed the chris/add-table-state branch from 624d676 to 2caee5f Compare October 13, 2025 11:13
@Artmann
Artmann marked this pull request as ready for review October 13, 2025 11:15

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (5)
packages/blocks/src/blocks/code-blocks.ts (1)

1-2: LGTM!

Named import consistent with project-wide standardization.

packages/blocks/src/blocks.ts (1)

39-40: LGTM!

Index signature with unknown enables arbitrary metadata (e.g., deepnote_table_state) while preserving type safety.

packages/blocks/src/blocks/data-frame.ts (1)

1-1: LGTM!

Named import aligns with project standards.

packages/blocks/src/blocks/sql-blocks.ts (2)

1-1: LGTM!

Import standardization consistent with other files.


41-49: LGTM!

dedent properly formats the SQL execution call.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 624d676 and 18d38bc.

📒 Files selected for processing (10)
  • CLAUDE.md (1 hunks)
  • packages/blocks/src/blocks.ts (1 hunks)
  • packages/blocks/src/blocks/code-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/data-frame.test.ts (1 hunks)
  • packages/blocks/src/blocks/data-frame.ts (1 hunks)
  • packages/blocks/src/blocks/input-blocks.test.ts (1 hunks)
  • packages/blocks/src/blocks/input-blocks.ts (1 hunks)
  • packages/blocks/src/blocks/sql-blocks.ts (2 hunks)
  • packages/blocks/src/python-code.test.ts (8 hunks)
  • packages/blocks/src/python-snippets.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
packages/blocks/src/python-code.test.ts (4)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (9-13)
packages/blocks/src/index.ts (1)
  • createPythonCode (4-4)
packages/blocks/src/python-code.ts (1)
  • createPythonCode (27-81)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/data-frame.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (9-13)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/python-utils.ts (1)
  • escapePythonString (1-7)
packages/blocks/src/blocks/sql-blocks.ts (1)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-18)
packages/blocks/src/blocks/code-blocks.ts (1)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-18)
🔇 Additional comments (27)
packages/blocks/src/blocks/input-blocks.ts (1)

1-1: LGTM!

Named import aligns with ts-dedent package structure.

Based on learnings

packages/blocks/src/blocks/input-blocks.test.ts (1)

1-1: LGTM!

Import style consistent with source file changes.

CLAUDE.md (1)

1-184: LGTM!

Comprehensive guide covering repo structure, workflow, and quality standards. Content aligns with modern TypeScript monorepo practices.

packages/blocks/src/python-snippets.ts (1)

1-1: LGTM!

Import change consistent across codebase.

packages/blocks/src/blocks/code-blocks.ts (1)

15-23: LGTM!

DataFrame config correctly prepended before user code. The blank line separation ensures clean output.

packages/blocks/src/blocks/sql-blocks.ts (3)

39-39: LGTM!

DataFrame config generation correctly placed before execution paths.


52-56: LGTM!

Config correctly prepended in undefined-variable path.


59-64: LGTM!

Config correctly prepended in defined-variable path. Both execution paths now include DataFrame configuration.

packages/blocks/src/blocks/data-frame.test.ts (7)

1-6: LGTM!

Named import from ts-dedent aligns with best practices.


9-29: LGTM!

Validates guard structure and empty state serialization.


31-75: LGTM!

Comprehensive validation of full table state serialization.


77-122: LGTM!

Validates SQL block support.


124-142: LGTM!

Validates fallback for missing table state.


144-168: LGTM!

Correctly validates double-escaping for JSON in Python strings. The comment explaining the escaping logic is helpful.


170-214: LGTM!

Comprehensive coverage of complex table state scenarios.

packages/blocks/src/python-code.test.ts (12)

1-1: LGTM!

Import change aligns with ts-dedent best practices.


169-177: LGTM!

Correctly validates boilerplate injection before user code.


179-225: LGTM!

Validates full table state serialization in code block output.


227-254: LGTM!

Validates custom pagination parameters in serialized state.


256-282: LGTM!

Validates hidden column IDs in serialized state.


590-605: LGTM!

Correctly validates boilerplate before execute_sql with variable assignment.


622-636: LGTM!

Validates boilerplate before execute_sql without variable assignment.


655-670: LGTM!

Validates boilerplate with query_preview return type.


687-702: LGTM!

Validates boilerplate with fallback integration ID.


720-735: LGTM!

Validates boilerplate before execute_sql with special characters in SQL.


737-792: LGTM!

Comprehensive validation of SQL block with full table state serialization.


794-823: LGTM!

Validates custom pagination in SQL block output.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 624d676 and 18d38bc.

📒 Files selected for processing (10)
  • CLAUDE.md (1 hunks)
  • packages/blocks/src/blocks.ts (1 hunks)
  • packages/blocks/src/blocks/code-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/data-frame.test.ts (1 hunks)
  • packages/blocks/src/blocks/data-frame.ts (1 hunks)
  • packages/blocks/src/blocks/input-blocks.test.ts (1 hunks)
  • packages/blocks/src/blocks/input-blocks.ts (1 hunks)
  • packages/blocks/src/blocks/sql-blocks.ts (2 hunks)
  • packages/blocks/src/python-code.test.ts (8 hunks)
  • packages/blocks/src/python-snippets.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (5)
packages/blocks/src/blocks/code-blocks.ts (1)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-18)
packages/blocks/src/blocks/data-frame.test.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (9-13)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-18)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/data-frame.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (9-13)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/python-utils.ts (1)
  • escapePythonString (1-7)
packages/blocks/src/python-code.test.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (9-13)
packages/blocks/src/python-code.ts (1)
  • createPythonCode (27-81)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (19-23)
packages/blocks/src/blocks/sql-blocks.ts (1)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-18)
🔇 Additional comments (21)
packages/blocks/src/blocks/input-blocks.ts (1)

1-1: LGTM!

Named import aligns with ts-dedent 2.2.0+ and project standards.

packages/blocks/src/blocks/input-blocks.test.ts (1)

1-1: LGTM!

Matches project-wide import standardization.

packages/blocks/src/python-snippets.ts (1)

1-1: LGTM!

Consistent import style across the codebase.

packages/blocks/src/blocks.ts (1)

39-40: LGTM!

Index signature with unknown allows dynamic metadata while preserving type safety. Supports the table state feature properly.

CLAUDE.md (1)

1-184: LGTM!

Comprehensive developer guide with accurate tooling references and clear guidelines. Well-structured for both AI assistants and human developers.

packages/blocks/src/blocks/code-blocks.ts (3)

1-1: LGTM!

Named import aligns with project standards.


5-5: LGTM!

Clean integration with the data-frame module.


15-23: LGTM!

Cleanly injects table state config before user code. The dedent template handles indentation properly.

packages/blocks/src/blocks/data-frame.ts (1)

1-1: LGTM!

Named import matches project standards.

packages/blocks/src/blocks/sql-blocks.ts (5)

1-1: LGTM!

Matches project-wide import standardization.


5-5: LGTM!

Clean integration with data-frame module.


39-49: LGTM!

Table config setup and dedented SQL call template are clean.


51-57: LGTM!

Properly injects table config for undefined-variable path.


59-65: LGTM!

Properly injects table config for defined-variable path. Pattern is consistent across both code and SQL blocks.

packages/blocks/src/blocks/data-frame.test.ts (4)

1-1: Named import aligns with best practice.

Based on learnings, the named export form is preferred for TypeScript/ESM projects.


9-75: LGTM!

Tests validate both empty and populated table state scenarios correctly, including the conditional _dntk check and fallback assignment.


144-168: LGTM!

Escaping test correctly validates multi-layer escaping (JSON → Python string). The nested quotes are handled properly by the combination of JSON.stringify and escapePythonString.


170-214: LGTM!

Comprehensive test validates complex table state with sorting, filtering, pagination, hidden columns, and formatting rules.

packages/blocks/src/python-code.test.ts (3)

1-1: Named import aligns with best practice.

Consistent with ts-dedent conventions and other test files in this PR.


156-282: LGTM!

Code block tests correctly validate that the dataframe config boilerplate is prepended in all scenarios (empty state, full state, custom pagination, hidden columns). The boilerplate defaulting to {} for blocks without table state is intentional.


571-823: LGTM!

SQL block tests comprehensively validate the integration of dataframe config across various scenarios: with/without variable names, different return types, missing integration IDs, special character escaping, and table state configurations.

@Artmann
Artmann requested a review from jankuca October 13, 2025 11:19
coderabbitai[bot]
coderabbitai Bot previously approved these changes Oct 13, 2025

@saltenasl saltenasl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only blocking thing is to test that the SQL query code is escaped.

The fact that the escape utility doesn't have tests can be addressed in a separate PR

Comment on lines 15 to 23
export function createPythonCodeForCodeBlock(block: CodeBlock): string {
return block.content
const dataFrameConfig = createDataFrameConfig(block)

return dedent`
${dataFrameConfig}

${block.content}
`
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - i can't do a code suggestion but along the lines of:

export const createPythonCodeForCodeBlock = (block: CodeBlock): string => dedent`
  ${createDataFrameConfig(block)}
  ${block.content}
`

`)
})

it('escapes special characters in JSON string', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be tested in the utility function instead, otherwise, if we end up removing the data-frame.ts file we will remove the tests for the utility.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've thought about this further and think we need the test in both places:

  1. The utility function should be tested inclusive of all edge cases
  2. This function needs to be tested that it is indeed using the utility to escape (because removal of it should break tests as it will break user code).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the test to the python-code tests as well


expect(result).toContain('"hiddenColumnIds":["secret"]')
})
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets please add a test that the query code is escaped

Comment thread packages/blocks/src/blocks.ts Outdated
| MarkdownCellMetadata

export interface ExecutableBlockMetadata {
[key: string]: unknown

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the purpose of this adding the data table related fields? If so, can we list them explicitly? Allowing any key here may be unnecessarily permissive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the real types.

import type { SqlBlock } from './sql-blocks'

export function createDataFrameConfig(block: CodeBlock | SqlBlock): string {
const rawTableState = 'deepnote_table_state' in block.metadata ? block.metadata.deepnote_table_state : {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the key presence check needed? Wouldn't it be enough to do block.metadata.deepnote_table_state ?? {}?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not needed now that we have the explicit types. Updated!

export function createDataFrameConfig(block: CodeBlock | SqlBlock): string {
const rawTableState = 'deepnote_table_state' in block.metadata ? block.metadata.deepnote_table_state : {}
const tableState = rawTableState ?? {}
const tableStateAsJson = JSON.stringify(tableState) ?? '{}'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this fallback needed? Shouldn't this always be at least {} before the stringifying?

(may seem like a nitpick but any fallback like this makes the reader wonder whether there is some random edge case that this is covering)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's right!

jankuca
jankuca previously approved these changes Oct 14, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 18d38bc and d0cd532.

📒 Files selected for processing (4)
  • packages/blocks/src/blocks/code-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/data-frame.ts (1 hunks)
  • packages/blocks/src/blocks/sql-blocks.ts (2 hunks)
  • packages/blocks/src/python-code.test.ts (8 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-09-03T15:02:01.006Z
Learnt from: jankuca
PR: deepnote/deepnote#17463
File: apps/webapp-client/src/features/drag-n-drop/CellDropTarget.tsx:63-67
Timestamp: 2025-09-03T15:02:01.006Z
Learning: In the Deepnote codebase, follow Google casing rules for compound words in camelCase - prefer "Sql" over "SQL", so useInsertEmptyDataframeSqlBlock is preferred over useInsertEmptyDataframeSQLBlock.

Applied to files:

  • packages/blocks/src/blocks/sql-blocks.ts
🧬 Code graph analysis (2)
packages/blocks/src/blocks/data-frame.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (16-20)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (25-29)
packages/blocks/src/blocks/python-utils.ts (1)
  • escapePythonString (1-7)
packages/blocks/src/blocks/code-blocks.ts (2)
packages/blocks/src/blocks.ts (1)
  • ExecutableBlockMetadata (38-48)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-17)
🪛 GitHub Check: Typecheck
packages/blocks/src/python-code.test.ts

[failure] 237-237:
Object literal may only specify known properties, and 'filters' does not exist in type '{ columnOrder: string[]; pageIndex: number; pageSize: number; sortBy: { id: string; type: string; }[]; }'.


[failure] 189-189:
Object literal may only specify known properties, and 'filters' does not exist in type '{ columnOrder: string[]; pageIndex: number; pageSize: number; sortBy: { id: string; type: string; }[]; }'.

🔇 Additional comments (4)
packages/blocks/src/blocks/data-frame.ts (1)

7-17: LGTM!

Clean implementation. Nullish coalescing handles missing table state, and the conditional Python logic routes to _dntk or fallback variable as needed.

packages/blocks/src/blocks/sql-blocks.ts (1)

45-70: Clean integration of dataFrameConfig.

The dedented template properly prefixes SQL execution with table state configuration. Logic correctly handles both variable assignment and direct return paths.

packages/blocks/src/blocks/code-blocks.ts (1)

22-30: LGTM!

Straightforward integration—dataFrameConfig prepended to block content via dedent.

packages/blocks/src/python-code.test.ts (1)

169-177: Test coverage looks solid.

Guarded boilerplate generation, empty vs. populated table states, special character escaping, and various SQL scenarios are all covered.

Also applies to: 217-225, 618-633, 650-664, 683-698, 715-730, 748-763, 805-819

Comment on lines +13 to +18
deepnote_table_state: {
columnOrder: string[]
pageIndex: number
pageSize: number
sortBy: { id: string; type: string }[]
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Type definition incomplete—compilation will fail.

The type defines 4 properties but tests use at least 10: filters, hiddenColumnIds, columnDisplayNames, conditionalFilters, cellFormattingRules, wrappedTextColumnIds are missing.

Apply this diff to align with test expectations:

 export interface SqlBlockMetadata extends ExecutableBlockMetadata {
   deepnote_return_variable_type?: SqlBlockVariableType
   deepnote_table_state: {
     columnOrder: string[]
+    columnDisplayNames: string[]
+    hiddenColumnIds: string[]
+    wrappedTextColumnIds: string[]
     pageIndex: number
     pageSize: number
     sortBy: { id: string; type: string }[]
+    filters: unknown[]
+    conditionalFilters: unknown[]
+    cellFormattingRules: unknown[]
   }
   deepnote_variable_name?: string
   function_export_name?: string
   is_compiled_sql_query_visible?: boolean
   sql_integration_id?: string
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
deepnote_table_state: {
columnOrder: string[]
pageIndex: number
pageSize: number
sortBy: { id: string; type: string }[]
}
deepnote_table_state: {
columnOrder: string[]
columnDisplayNames: string[]
hiddenColumnIds: string[]
wrappedTextColumnIds: string[]
pageIndex: number
pageSize: number
sortBy: { id: string; type: string }[]
filters: unknown[]
conditionalFilters: unknown[]
cellFormattingRules: unknown[]
}
🤖 Prompt for AI Agents
In packages/blocks/src/blocks/sql-blocks.ts around lines 13 to 18, the
deepnote_table_state type only lists columnOrder, pageIndex, pageSize and sortBy
but tests expect additional properties causing compilation failures; update the
type to include the missing properties used by tests — add filters (array),
hiddenColumnIds (string[]), columnDisplayNames (Record<string,string>),
conditionalFilters (array), cellFormattingRules (array), and
wrappedTextColumnIds (string[]), using appropriate specific types if known (or
unknown/any for complex shapes) and make them required to match test usage.

Comment thread packages/blocks/src/python-code.test.ts Resolved
saltenasl
saltenasl previously approved these changes Oct 14, 2025

@saltenasl saltenasl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Artmann
Artmann dismissed stale reviews from saltenasl and jankuca via ab629a4 October 14, 2025 13:30

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (2)
packages/blocks/src/python-code.test.ts (1)

749-753: SQL string escaping test — addresses prior feedback

Covers single quotes and newlines; matches expected escaping.

packages/blocks/src/blocks/code-blocks.ts (1)

21-25: Minor style nit: inline as one-liner arrow (optional)

Could be simplified per prior suggestion to a one-liner template. No behavior change.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d0cd532 and e064c04.

📒 Files selected for processing (6)
  • packages/blocks/src/blocks/code-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/data-frame.test.ts (1 hunks)
  • packages/blocks/src/blocks/sql-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/table-state.ts (1 hunks)
  • packages/blocks/src/index.ts (1 hunks)
  • packages/blocks/src/python-code.test.ts (8 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
packages/blocks/src/blocks/data-frame.test.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (12-16)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-17)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (21-25)
packages/blocks/src/blocks/code-blocks.ts (3)
packages/blocks/src/blocks.ts (1)
  • ExecutableBlockMetadata (38-48)
packages/blocks/src/blocks/table-state.ts (1)
  • TableState (1-12)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-17)
packages/blocks/src/blocks/sql-blocks.ts (3)
packages/blocks/src/blocks.ts (1)
  • ExecutableBlockMetadata (38-48)
packages/blocks/src/blocks/table-state.ts (1)
  • TableState (1-12)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-17)
🔇 Additional comments (21)
packages/blocks/src/blocks/table-state.ts (1)

1-12: TableState shape matches usages/tests

Covers all fields referenced in tests and other modules. Looks good.

packages/blocks/src/python-code.test.ts (12)

1-1: Consistent dedent import

Named import adopted. Good move.


169-177: Code block prelude (no table state) — OK

Asserts the guarded dataframe config with empty JSON.


179-225: Code block prelude with table state — OK

Validates proper JSON injection and placement before content.


227-254: Custom page size/index in code block — OK

Assertions are precise and robust.


256-283: Hidden columns in code block — OK

Coverage for hiddenColumnIds present.


284-310: Escaping within table state JSON — OK

Backslashes and quotes correctly escaped end-to-end.


619-623: SQL prelude (no table state) — OK

Correctly emits guarded formatter config.


651-655: SQL without variable name — OK

Prelude present; execution call formatted as expected.


684-688: SQL query_preview path — OK

Prelude included; return type handled.


716-720: SQL without integration id — OK

Falls back to SQL_ALCHEMY_JSON_ENV_VAR correctly, with prelude.


765-820: SQL block with table state — OK

Validates JSON passthrough and prelude placement before execute_sql.


822-851: Custom page size/index in SQL block — OK

Assertions confirm correct serialization.

packages/blocks/src/index.ts (1)

1-1: Publicly exporting TableState — OK

Keeps API surface coherent with new metadata usage.

packages/blocks/src/blocks/sql-blocks.ts (2)

14-15: SqlBlockMetadata extended with deepnote_table_state — OK

Type aligned with TableState and tests.


41-51: Always prepend dataframe config — OK

dataFrameConfig is injected in both branches; execute_sql call stays clean and dedented. Escaped query usage is correct.

Also applies to: 54-58, 62-63

packages/blocks/src/blocks/code-blocks.ts (1)

8-10: CodeBlockMetadata extended with deepnote_table_state — OK

Type addition matches consumers.

packages/blocks/src/blocks/data-frame.test.ts (4)

9-29: LGTM!

Correctly validates empty table state produces '{}' in Python output.


124-142: LGTM!

Correctly validates the ?? {} fallback when deepnote_table_state is missing.


144-168: LGTM!

Escaping test correctly validates:

  • Single quotes: ' → \' (Python string)
  • Double quotes: " → \" (JSON) → \\" (Python string)

The multi-layer escaping in the string literal is accurate.


1-1: ts-dedent import is correct All imports use named dedent consistently across the codebase; no further action needed.

coderabbitai[bot]
coderabbitai Bot previously requested changes Oct 14, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (2)
packages/blocks/src/blocks/code-blocks.ts (1)

19-25: Inline and simplify template

Minor nit: avoid the temporary and inline for readability.

-export function createPythonCodeForCodeBlock(block: CodeBlock): string {
-  const dataFrameConfig = createDataFrameConfig(block)
-
-  return dedent`
-    ${dataFrameConfig}
-
-    ${block.content}
-  `
-}
+export function createPythonCodeForCodeBlock(block: CodeBlock): string {
+  return dedent`
+    ${createDataFrameConfig(block)}
+
+    ${block.content}
+  `
+}
packages/blocks/src/python-code.test.ts (1)

732-763: SQL query escaping test added—thanks.

Correctly escapes quotes and newlines; resolves earlier feedback.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d0cd532 and e064c04.

📒 Files selected for processing (6)
  • packages/blocks/src/blocks/code-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/data-frame.test.ts (1 hunks)
  • packages/blocks/src/blocks/sql-blocks.ts (2 hunks)
  • packages/blocks/src/blocks/table-state.ts (1 hunks)
  • packages/blocks/src/index.ts (1 hunks)
  • packages/blocks/src/python-code.test.ts (8 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
packages/blocks/src/blocks/code-blocks.ts (3)
packages/blocks/src/blocks.ts (1)
  • ExecutableBlockMetadata (38-48)
packages/blocks/src/blocks/table-state.ts (1)
  • TableState (1-12)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-17)
packages/blocks/src/blocks/data-frame.test.ts (3)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (12-16)
packages/blocks/src/blocks/data-frame.ts (1)
  • createDataFrameConfig (7-17)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (21-25)
packages/blocks/src/python-code.test.ts (4)
packages/blocks/src/blocks/code-blocks.ts (1)
  • CodeBlock (12-16)
packages/blocks/src/index.ts (1)
  • createPythonCode (5-5)
packages/blocks/src/python-code.ts (1)
  • createPythonCode (27-81)
packages/blocks/src/blocks/sql-blocks.ts (1)
  • SqlBlock (21-25)
🔇 Additional comments (10)
packages/blocks/src/index.ts (1)

1-1: Public type export LGTM

Clean, type-only re-export; no runtime impact.

packages/blocks/src/blocks/sql-blocks.ts (1)

41-66: DataFrame config injection looks correct

Good use of dedent and consistent injection in both return paths; assignment with multiline call is valid Python.

packages/blocks/src/blocks/data-frame.test.ts (1)

144-168: Great coverage for escaping edge cases

The explicit assertion for mixed quotes and backslashes is spot-on and guards regressions in escapePythonString.

packages/blocks/src/python-code.test.ts (7)

1-1: Named import for ts-dedent—good.

Consistent with best practices and current package API. Based on learnings


227-254: Custom pagination for Code blocks—ok.

Asserts pageSize/pageIndex appear in payload.


256-282: Hidden columns propagation—ok.

Presence of hiddenColumnIds verified.


284-310: JSON and Python string escaping—good coverage.

Backslashes and quotes handled correctly.


765-820: SQL table state JSON injection—ok.

End-to-end expectation (including ID normalization) looks right.


822-851: Custom pagination for SQL blocks—ok.

pageSize/pageIndex correctly present.


187-198: TableState covers all required deepnote_table_state fields
TableState defines sortBy, filters, pageSize, pageIndex, columnOrder, hiddenColumnIds, columnDisplayNames (with columnName/displayName), conditionalFilters, cellFormattingRules, and wrappedTextColumnIds; both CodeBlockMetadata and SqlBlockMetadata reference deepnote_table_state?: TableState.

Comment thread packages/blocks/src/python-code.test.ts Resolved
Comment thread packages/blocks/src/python-code.test.ts Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants