Registry / web-framework / streamlit-code-editor

streamlit-code-editor

JSON →
library0.1.22pypypi✓ verified 84d ago

Streamlit Code Editor is an active, community-driven component that integrates a React-Ace based code editor into Streamlit applications. It currently stands at version 0.1.22, offering features like custom themes, syntax highlighting for various languages, and customizable interface elements. The library maintains an active release cadence, with updates addressing performance, bug fixes, and new features.

pip install streamlit-code-editor
INSTALL
IMPORT
SIG · STREAMLIT-CODE-EDI
S
streamlit-code-editor
web-frameworkpythonv0.1.22
Install
15.6s avg
Import
1340ms
Disk
450MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.22 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 1.725s · 461.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 15.6s · import 0.955s · 431MB
450MB installed
● package 450MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

code_editor
from code_editor import code_editor

This quickstart demonstrates how to embed a basic Python code editor in a Streamlit app. It uses `st.title` and `st.subheader` for layout and `code_editor` to render the interactive editor. The `key` argument is important for maintaining state across reruns, and the `response_dict` captures the editor's output, allowing submission handling.

import streamlit as st from code_editor import code_editor def main(): st.set_page_config(layout="wide") st.title("My Streamlit Code Editor App") initial_code = """import pandas as pd def greet(name): return f"Hello, {name}!" print(greet("Streamlit")) """ st.subheader("Edit your Python code:") response_dict = code_editor(initial_code, lang="python", key="my_editor") if response_dict and response_dict["type"] == "submit": st.write("You submitted the following code:") st.code(response_dict["text"], language="python") if __name__ == "__main__": main()
Debug
Known issues
gotchaEditor content may not update when input code changes if `key` is static and `allow_reset` is `False`.
fix
To allow programmatic updates to the editor's content while keeping a consistent `key`, set `allow_reset=True` in the `code_editor` function call. `code_editor(initial_code, key='my_editor', allow_reset=True)`.
affects: All versions
gotchaThe default `response_mode` only returns content on explicit user commands (e.g., Ctrl+Enter).
fix
If real-time updates (e.g., on text change, blur, or selection) are desired, explicitly set the `response_mode` argument, e.g., `response_mode="debounce"` or `response_mode=["blur", "select"]`.
affects: v0.1.4+
gotchaStreamlit's rerun model can lead to unexpected widget behavior or lost input if `st.session_state` is not handled correctly with interactive components.
fix
Always initialize `st.session_state` keys (e.g., `if 'key' not in st.session_state: st.session_state.key = initial_value`). When processing editor output, check `response_dict['id']` to ensure a new response has been sent before updating state or taking action.
affects: All versions
Errors
Common errors & fixes
Editor content does not reflect changes to the initial code string on subsequent reruns.
The `code_editor` component, when given a static `key`, defaults `allow_reset` to `False`, preventing programmatic updates to its displayed content from the input `code` argument.
fix
Pass `allow_reset=True` to the `code_editor` function: `code_editor(initial_code, key='my_editor', allow_reset=True)`.
The first edit made to the code editor is not registered or reverts to the original value.
This often occurs due to Streamlit's rerun model and how `st.session_state` is updated from widget outputs, similar to the 'double submit' problem in Streamlit's `st.data_editor`.
fix
Ensure `st.session_state` is correctly initialized for any variable managing the editor's content. When reacting to the editor's output, use `response_dict['id']` to confirm a *new* response before processing and updating state, preventing reprocessing of stale values.
Buttons or callbacks intended to modify the editor's content do not work on the first click or behave inconsistently.
Incorrect management of `st.session_state` when trying to programmatically change the `code_editor`'s value via a button click or callback.
fix
Ensure `st.session_state` variables are properly initialized and updated within callback functions or directly within the main script logic based on user interaction, then passed to the `code_editor` with `allow_reset=True`.
Upgrade
Version history
0.1.22latest on PyPI · released Dec 29, 2024
Audit
Dependencies
streamlitrequiredThis is a custom component for Streamlit and requires Streamlit to function.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
streamlit-code-editor — pip install streamlit-code-editor · libregistry