Skip to content

Conversation

@gadenbuie
Copy link
Collaborator

Summary

Adds input_code_editor(), a lightweight code editor input with syntax highlighting, ported from rstudio/bslib#1274.

Features

  • Syntax highlighting for 20+ programming languages (Python, R, SQL, JavaScript, etc.)
  • Automatic light/dark theme switching based on Bootstrap theme
  • Line numbers (optional, defaults based on language)
  • Word wrap (optional)
  • Customizable themes (14 built-in options)
  • Keyboard shortcuts including Ctrl/Cmd+Enter to submit
  • Value updates on blur or submit (not every keystroke)

API

from shiny import ui

# Basic usage
ui.input_code_editor(
    "code",
    value="print('Hello')",
    language="python",
)

# Update from server
ui.update_code_editor("code", value="new code", language="javascript")

gadenbuie and others added 9 commits December 31, 2025 09:36
Ports the `input_code_editor()` component from bslib PR #1274 to py-shiny.

## Overview
`input_code_editor()` is a lightweight code editor input with syntax
highlighting powered by prism-code-editor. It supports 20+ languages,
multiple themes, and automatic light/dark mode switching.

## Added
- `input_code_editor()` - Create a code editor input
- `update_code_editor()` - Update editor from server
- `code_editor_themes()` - List available themes

## Key decisions for human review

1. **Dependency structure**: The code editor uses two HTML dependencies:
   - `prism-code-editor` (vendored library for syntax highlighting)
   - `bslib-code-editor-js` (the Shiny input binding)

2. **Themes list**: Manually hardcoded the theme list to match the
   vendored CSS files. The R version discovers these dynamically, but
   Python doesn't have easy access to the vendored directory at runtime.

3. **Language validation**: Using a hardcoded tuple of supported languages
   instead of dynamic discovery, matching the R implementation's approach.

4. **Manual vendoring**: Due to issues with the automated vendoring script
   (ionRangeSlider CSS step fails), assets were manually copied from the
   bslib feat/input-code branch. Updated htmlDependencies.R to include
   the prism-code-editor copy for future runs once bslib PR is merged.

5. **Value handling**: The `value` parameter accepts either a string or
   a sequence of strings (lines), matching Python idioms.

6. **Fill behavior**: Added `html-fill-container` and `html-fill-item`
   classes for fillable layout support, similar to other fill components.

Related: rstudio/bslib#1274

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Phase 2 (continued):
- Create API examples for input_code_editor and update_code_editor
- Add InputCodeEditor Playwright controller with methods for:
  - set(), submit() for setting values and submitting via Ctrl/Cmd+Enter
  - expect_value(), expect_language(), expect_theme_light(), expect_theme_dark()
  - expect_read_only(), expect_line_numbers(), expect_word_wrap()
  - expect_tab_size(), expect_height()

Phase 3:
- Port unit tests from bslib (35 pytest tests covering validation, HTML structure,
  parameters, values, indentation, labels, languages, and warnings)
- Create Playwright end-to-end tests (11 tests covering initial state, updates,
  read-only mode, language switching, and server-side updates)
- Update CHANGELOG with new feature description
- Run quality checks (format, type-check)
- Update API reference configuration (core, express, and testing docs)

Technical decisions:
- Validation in update_code_editor happens BEFORE requiring active session to
  enable testing without session
- InputCodeEditor controller uses bslib-code-editor as loc_container (not
  div.shiny-input-container) since the custom element is its own container
- Platform detection for Ctrl/Cmd key uses navigator.platform evaluation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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