Path: blob/main/examples/reference/widgets/CodeEditor.ipynb
2011 views
The CodeEditor
widget allows embedding a code editor based on Ace.
Only a small subset of Ace functionality is currently enabled:
syntax highlighting for several languages
themes
basic completion support via
ctrl+space
(using only static analysis of the code)annotations
readonly mode
Parameters:
For details on other options for customizing the component see the layout and styling how-to guides.
annotations
(list): list of annotations. An annotation is a dict with the following keys:'row'
: row in the editor of the annotation'column'
: column of the annotation'text'
: text displayed when hovering over the annotation'type'
: type of annotation and the icon displayed {warning
|error
}
filename
(str): If filename is provided the file extension will be used to determine the languageindent
(int): Number of spaces to use for default indentation.language
(str): A string declaring which language to use for code syntax highlighting (default: 'text')on_keyup
(bool): Whether to update the value on every key press or only upon loss of focus / hotkeys.print_margin
(boolean): Whether to show a print margin in the editorsoft_tabs
(boolean): Whether to use spaces instead of tabs for indentation.theme
(str): If no value is provided, it defaults to the current theme set by pn.config.theme, as specified in the CodeEditor.THEME_CONFIGURATION dictionary. If not defined there, it falls back to the default parameter value ('chrome').readonly
(boolean): Whether the editor should be opened in read-only modevalue
(str): State of the current code in the editor ifon_keyup
. Otherwise, only upon loss of focus, i.e. clicking outside the editor, or pressing <Ctrl+Enter> or <Cmd+Enter>.value_input
(str): State of the current code updated on every key press. Identical tovalue
ifon_keyup
.
To construct an Ace
widget we must define it explicitly using pn.widgets.Ace
. We can add some text as initial code. Code inserted in the editor is automatically reflected in the value_input
and value
.
we can add some code in it
By default, the code editor will update the value
on every key press, but you can set on_keyup=False
to only update the value
when the editor loses focus or pressing <Ctrl+Enter>
/ <Cmd+Enter>
. Here, the code is printed when value
is changed.
We can change language and theme of the editor
We can add some annotations to the editor
If we want just to display editor content but not edit it we can set the readonly
property to True
If instead of setting the language explicitly we want to set the filename and automatically detect the language based on the file extension we can do that too:
Including the CodeEditor editor in a parameterized class
You can view the Ace widget as part of a param.Parameterized
class:
Controls
The CodeEditor
widget exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively:
Try changing the filename including a file extension and watch the language automatically update.