Skip to content

Commit d4816b2

Browse files
authored
Expose more REPL options via query string parameters (#1573)
* Expose more REPL options via query string parameters * fix clearCodeContentOnExecute * Document the new REPL options * improvements
1 parent 233718d commit d4816b2

File tree

2 files changed

+111
-17
lines changed

2 files changed

+111
-17
lines changed

docs/quickstart/embed-repl.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,68 @@ pip install jupyterlab-gt-coar-theme
9898

9999
See the [how-to guides](../howto/index.md) for more details on how to customize the
100100
environment and add more themes and extensions.
101+
102+
### Clear cells on execute
103+
104+
To automatically clear the previously executed cells on execute, set the
105+
`clearCellsOnExecute` parameter to `1`:
106+
107+
```html
108+
<iframe
109+
src="https://jupyterlite.github.io/demo/repl/index.html?clearCellsOnExecute=1"
110+
width="100%"
111+
height="100%"
112+
></iframe>
113+
```
114+
115+
### Clear code content on execute
116+
117+
To automatically clear the code content in the prompt cell on execute, set the
118+
`clearCodeContentOnExecute` parameter to `1`:
119+
120+
```html
121+
<iframe
122+
src="https://jupyterlite.github.io/demo/repl/index.html?clearCodeContentOnExecute=1"
123+
width="100%"
124+
height="100%"
125+
></iframe>
126+
```
127+
128+
### Hide code input
129+
130+
To hide the input cell of the executed cells, set the `hideCodeInput` parameter to `1`:
131+
132+
```html
133+
<iframe
134+
src="https://jupyterlite.github.io/demo/repl/index.html?hideCodeInput=1"
135+
width="100%"
136+
height="100%"
137+
></iframe>
138+
```
139+
140+
### Prompt cell position
141+
142+
To change where the prompt cell is placed, set the `promptCellPosition` parameter to
143+
`top`, `bottom`, `left`, or `right`:
144+
145+
```html
146+
<iframe
147+
src="https://jupyterlite.github.io/demo/repl/index.html?promptCellPosition=left"
148+
width="100%"
149+
height="100%"
150+
></iframe>
151+
```
152+
153+
### Show or hide the kernel banner
154+
155+
By default the REPL shows the kernel banner.
156+
157+
To hide the banner, set the `showBanner` parameter to `0`:
158+
159+
```html
160+
<iframe
161+
src="https://jupyterlite.github.io/demo/repl/index.html?showBanner=0"
162+
width="100%"
163+
height="100%"
164+
></iframe>
165+
```

packages/repl-extension/src/index.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
import { IThemeManager, IToolbarWidgetRegistry } from '@jupyterlab/apputils';
1313

14-
import { ConsolePanel, IConsoleTracker } from '@jupyterlab/console';
14+
import { CodeConsole, ConsolePanel, IConsoleTracker } from '@jupyterlab/console';
1515

1616
import { ITranslator } from '@jupyterlab/translation';
1717

@@ -81,7 +81,7 @@ const consolePlugin: JupyterFrontEndPlugin<void> = {
8181
if (!tracker) {
8282
return;
8383
}
84-
const { commands, serviceManager, started } = app;
84+
const { commands, started } = app;
8585

8686
const search = window.location.search;
8787
const urlParams = new URLSearchParams(search);
@@ -91,33 +91,62 @@ const consolePlugin: JupyterFrontEndPlugin<void> = {
9191
const theme = urlParams.get('theme')?.trim();
9292
const toolbar = urlParams.get('toolbar');
9393

94-
Promise.all([started, serviceManager.ready]).then(async () => {
95-
commands.execute('console:create', { kernelPreference: { name: kernel } });
94+
// normalize config options
95+
const clearCellsOnExecute =
96+
urlParams.get('clearCellsOnExecute') === '1' || undefined;
97+
const clearCodeContentOnExecute =
98+
urlParams.get('clearCodeContentOnExecute') === '0' ? false : undefined;
99+
const hideCodeInput = urlParams.get('hideCodeInput') === '1' || undefined;
100+
const showBanner = urlParams.get('showBanner') === '0' ? false : undefined;
101+
102+
const position = urlParams.get('promptCellPosition') ?? '';
103+
const validPositions = ['top', 'bottom', 'left', 'right'];
104+
const promptCellPosition = validPositions.includes(position)
105+
? (position as CodeConsole.PromptCellPosition)
106+
: undefined;
107+
108+
started.then(async () => {
109+
// create a new console at application startup
110+
void commands.execute('console:create', {
111+
kernelPreference: { name: kernel },
112+
});
96113
});
97114

98-
if (theme && themeManager) {
99-
const themeName = decodeURIComponent(theme);
100-
themeManager.setTheme(themeName);
101-
}
102-
103-
tracker.widgetAdded.connect(async (_, widget) => {
104-
const { console } = widget;
105-
115+
tracker.widgetAdded.connect(async (_, panel) => {
106116
if (!toolbar) {
107117
// hide the toolbar by default if not specified
108-
widget.toolbar.dispose();
118+
panel.toolbar.dispose();
109119
}
110120

111-
if (code) {
112-
await console.sessionContext.ready;
121+
const { console: widget } = panel;
122+
const { sessionContext } = widget;
123+
124+
await sessionContext.ready;
125+
if (code.length > 0) {
113126
if (execute === '0') {
114127
const codeContent = code.join('\n');
115-
console.replaceSelection(codeContent);
128+
widget.replaceSelection(codeContent);
116129
} else {
117-
code.forEach((line) => console.inject(line));
130+
code.forEach((line) => widget.inject(line));
118131
}
119132
}
133+
134+
widget.setConfig({
135+
clearCellsOnExecute,
136+
clearCodeContentOnExecute,
137+
hideCodeInput,
138+
promptCellPosition,
139+
// TODO: handling of the showBanner may not work as expected for now
140+
// due to the assumption there should be a banner upstream:
141+
// https://github.com/jupyterlab/jupyterlab/pull/17322
142+
showBanner,
143+
});
120144
});
145+
146+
if (theme && themeManager) {
147+
const themeName = decodeURIComponent(theme);
148+
themeManager.setTheme(themeName);
149+
}
121150
},
122151
};
123152

0 commit comments

Comments
 (0)