Skip to content

feat: editable SQL query editor in $app()#265

Merged
gadenbuie merged 6 commits into
mainfrom
feat/editable-query
Jun 26, 2026
Merged

feat: editable SQL query editor in $app()#265
gadenbuie merged 6 commits into
mainfrom
feat/editable-query

Conversation

@gadenbuie

Copy link
Copy Markdown
Contributor

Closes #70

Summary

Replaces the read-only SQL display in $app() with an interactive bslib::input_code_editor() (R) and its Python equivalent, allowing users to directly edit and run SQL queries from the UI.

Key design decisions:

  • Submit semantics: Relies on the editor's built-in trigger (blur or Ctrl/Cmd+Enter) — no explicit "Run Query" button needed.
  • Sync without re-render: A dedicated sync_sql_editor observer calls update_code_editor() on every sql_val() change (LLM update, reset, table switch) to keep the editor current without re-rendering the widget.
  • Reset feedback-loop fix: on_sql_editor compares incoming SQL to SELECT * FROM <table> and writes NULL on a match, so the Reset button correctly disappears after a reset even though update_code_editor() triggers the observer.
  • No new dependencies: bslib::input_code_editor() / update_code_editor() were already available from the viz feature.
  • sql_editor is excluded from Shiny bookmarking.

Both R (pkg-r/R/QueryChat.R) and Python (pkg-py/src/querychat/_shiny.py) updated in parallel.

Verification

querychat_app(penguins)
  1. Ask the LLM a question — the SQL panel should now be an editable code editor showing the generated query.
  2. Manually edit the SQL and press Ctrl/Cmd+Enter (or click away) — the table updates immediately.
  3. Click Reset — the editor reverts to SELECT * FROM penguins and the Reset button disappears.
  4. For multi-table setups, switch the active table — the editor should update to show the correct query.

Replace the read-only SQL markdown display with a `bslib::input_code_editor()`
that lets users edit the SQL directly. The editor submits on blur or Ctrl/Cmd+Enter.

- `sync_sql_editor` observer keeps the editor in sync via `update_code_editor()`
  when the LLM updates SQL, the query is reset, or the active table switches
- `on_sql_editor` observer applies user edits to `sql_val()`; treats the default
  `SELECT * FROM <name>` as NULL to prevent the Reset button from reappearing
  after a reset (update_code_editor triggers input$sql_editor on the server)
- Editor excluded from bookmarking in both R and Python

Closes #70
@gadenbuie gadenbuie marked this pull request as ready for review June 26, 2026 16:34
@gadenbuie gadenbuie requested a review from cpsievert June 26, 2026 16:34
`pre code` no longer exists; SQL is now rendered in `input_code_editor`
which uses a `.cm-content` element.
bslib's input_code_editor renders as a <bslib-code-editor> custom element
using prism-code-editor (not CodeMirror). The editable value lives in the
textarea inside the element; switch from to_contain_text() to to_have_value()
accordingly.
@gadenbuie gadenbuie merged commit f68e6ae into main Jun 26, 2026
16 of 17 checks passed
@gadenbuie gadenbuie deleted the feat/editable-query branch June 26, 2026 17:20
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.

Add editable-code box component to be able to edit the SQL query

1 participant