Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(notebooks): add visible cell insertion buttons (#116292)

Marius Andra90ba0010c4ef
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 67e20f037f0c..421dd4b192f9 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -12891,9 +12891,9 @@ snapshots:
scenes-app-notebooks-generated-widgets--before-generation--light:
hash: v1.k794b7964.f8eabbbe270e16f6db6253fac4ccc1298d6486f4fcc54d086f39b494e32276bb.byEVF2WvcVULWZ_b-qJMNkCK-hCMqS-EcQNFNyVUVd4
scenes-app-notebooks-jupyter-mode--jupyter-mode--dark:
- hash: v1.k794b7964.999bb1c59ceaa9a76b1bdc28b2b24f67f345d22e986e2ae7aad57cffaaf25621.2A56iTWWLKXdmpS1g7TooVeM3DY-y1Mbmq2bjtpt5vE
+ hash: v1.k794b7964.0348331cebb2cd718ba27d081121a42d855b62c191f86af19b66bc7d5d812ede.Uco9KJMqE3EAiMZg1x3rnZA6nGEQ5Z6iWPhr1s_9LJk
scenes-app-notebooks-jupyter-mode--jupyter-mode--light:
- hash: v1.k794b7964.b9f5f2ae8ff0b6921c71d8bc102e0aae6d4029589a4aa9cbea567d9d09644f73.TV6wY-T8RH14L5AInke72aCjROD3aHhqExNWHwBdDy4
+ hash: v1.k794b7964.b2e5a41eeab0da940c38f236a768eb67f35000260b98b1df0fdbacf5e5f307b8.ogybvKdLXRRiMBqxzGenSF58ax8ATMn1drzEhmss2Pk
scenes-app-notebooks-jupyter-mode--jupyter-mode-off--dark:
hash: v1.k794b7964.8a933f9d3d913d27c9306f54490eff93629b2ae2c5a42c3a08c2165a7db72b68.ghNnjSunZ43NsgdxzN6tQlPHLUl6OFRy-vLszghAd7s
scenes-app-notebooks-jupyter-mode--jupyter-mode-off--light:
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx
index aaab7379fd6d..65789be220ae 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx
@@ -1,4 +1,4 @@
-import { act, fireEvent, render } from '@testing-library/react'
+import { act, fireEvent, render, waitFor, within } from '@testing-library/react'
import { createElement, useCallback } from 'react'
import { usePublishNotebookComponentRunHandler } from './componentRunHandlers'
@@ -37,14 +37,24 @@ const registry = createMarkdownNotebookRegistry([
label: 'Python',
category: 'Code',
defaultProps: { code: '' },
+ insertCommand: {},
ViewComponent: () => createElement('div', null, 'output'),
EditComponent: CodeEditorStandIn,
ToolbarComponent: RunPublisher,
},
+ {
+ tagName: 'Sql',
+ label: 'SQL',
+ category: 'Code',
+ defaultProps: { code: '' },
+ insertCommand: {},
+ ViewComponent: () => createElement('div', null, 'output'),
+ EditComponent: CodeEditorStandIn,
+ },
])
const jupyterMode: MarkdownNotebookJupyterModeConfig = {
- cellTagNames: ['Py'],
+ cellTagNames: ['Py', 'Sql'],
newCellTagName: 'Py',
getCellSource: (node) => String(node.props.code ?? ''),
withCellSource: (node, source) => ({ ...node, props: { ...node.props, code: source } }),
@@ -111,6 +121,35 @@ describe('MarkdownNotebook in Jupyter mode', () => {
expect(notebook.latest()).toEqual('# Title\n\nA useful paragraph.\n\nAnother paragraph.')
})
+ it.each([
+ ['Add Python', 'Py'],
+ ['Add SQL', 'Sql'],
+ ])('%s appends the requested code cell and focuses its editor', async (label, tagName) => {
+ const notebook = renderJupyterNotebook(TWO_CELLS)
+ fireEvent.click(within(notebook.container).getByText(label))
+ await waitFor(() => expect(document.activeElement).toBe(notebook.editor(2)))
+ expect(notebook.latest()).toEqual(`${TWO_CELLS}\n\n<${tagName} code="" />`)
+ })
+
+ it('Add text starts a separate markdown cell ready for typing', async () => {
+ const notebook = renderJupyterNotebook('# Title\n\nExisting text')
+ fireEvent.click(within(notebook.container).getByText('Add text'))
+ const newParagraph = notebook.container.querySelectorAll<HTMLElement>('.MarkdownNotebook__text-block')[2]
+ await waitFor(() => expect(document.activeElement).toBe(newParagraph))
+ expect(newParagraph.textContent).toBe('')
+ newParagraph.textContent = 'More text'
+ fireEvent.input(newParagraph)
+ expect(notebook.latest()).toEqual('# Title\n\nExisting text\n\n\nMore text')
+ })
+
+ it('the footer slash button opens the insert menu at the end', async () => {
+ const notebook = renderJupyterNotebook(TWO_CELLS)
+ fireEvent.click(within(notebook.container).getByLabelText('Add a cell'))
+ const menu = await within(notebook.container).findByRole('listbox')
+ fireEvent.click(within(menu).getByText('Python'))
+ expect(notebook.latest()).toEqual(`${TWO_CELLS}\n\n\n<Py code="" />`)
+ })
+
it.each([
[
'A inserts a cell above',
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss
index 0d7b719c9b0f..e017134f4180 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss
@@ -2662,23 +2662,11 @@ button.MarkdownNotebook__component-title:focus-visible {
}
.MarkdownNotebook__jupyter-add-cell {
- display: block;
+ display: flex;
+ flex-wrap: wrap;
+ gap: 0.5rem;
width: calc(100% - var(--jupyter-prompt-width));
- padding: 0.625rem 0.75rem;
margin: 0.5rem 0 0 var(--jupyter-prompt-width);
- font-size: 0.75rem;
- color: var(--color-text-secondary);
- text-align: left;
- cursor: pointer;
- user-select: none;
- background: none;
- border: 1px solid transparent;
- border-radius: 2px;
-
- &:hover {
- background: var(--jupyter-editor-background);
- border-color: var(--jupyter-editor-border);
- }
}
// Holds focus while a markdown cell is in command mode. Text never takes focus itself (the
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
index 446baa0a531a..a92e34c38207 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
@@ -7292,15 +7292,55 @@ function MarkdownNotebookEditor({
)
})}
{jupyterCommands && mode === 'edit' ? (
- <button
- type="button"
+ <div
className="MarkdownNotebook__jupyter-add-cell"
contentEditable={false}
data-attr="notebook-jupyter-add-cell"
- onClick={() => jupyterCommands.insertCell(null, 'below', { edit: true })}
>
- Click to add a cell.
- </button>
+ <LemonButton
+ size="small"
+ type="secondary"
+ aria-label="Add a cell"
+ tooltip="Add a cell"
+ data-attr="notebook-jupyter-add-cell-menu"
+ onClick={() => openInsertMenuAtBoundary(getJupyterAppendIndex())}
+ >
+ /
+ </LemonButton>
+ <LemonButton
+ size="small"
+ type="secondary"
+ data-attr="notebook-jupyter-add-text"
+ onClick={() =>
+ insertJupyterNodesAt(
+ getJupyterAppendIndex(),
+ [makeEmptyParagraph('text')],
+ 'edit'
+ )
+ }
+ >
+ Add text
+ </LemonButton>
+ {jupyterMode?.cellTagNames.map((tagName) => {
+ const definition = getMarkdownNotebookComponentDefinition(mergedRegistry, tagName)
+ return definition?.insertCommand ? (
+ <LemonButton
+ key={tagName}
+ size="small"
+ type="secondary"
+ data-attr="notebook-jupyter-add-code"
+ onClick={() => {
+ const node = createJupyterCellNode(tagName)
+ if (node) {
+ insertJupyterNodesAt(getJupyterAppendIndex(), [node], 'edit')
+ }
+ }}
+ >
+ {`Add ${definition.label}`}
+ </LemonButton>
+ ) : null
+ })}
+ </div>
) : null}
</div>
{adjustedRemoteCarets?.length ? (