: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(notebooks): Show SQL and Python cell results when they exist by default (#79707)
Sinan Kucukkoseler6c6bd4897035
diff --git a/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsx b/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsxindex a7f1ce5e18d8..3b8b87bd2189 100644--- a/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsx+++ b/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsx@@ -14,7 +14,7 @@ import { NotebookDataframeTable } from './components/NotebookDataframeTable'import { NotebookRunDownstreamBanner } from './components/NotebookRunDownstreamBanner'import { NotebookStaleCellBanner } from './components/NotebookStaleCellBanner'import { notebookNodeLogic } from './notebookNodeLogic'-import { countTextLines, outputHeightForShape } from './notebookNodeOutputHeight'+import { countTextLines, initialSizedRunId, outputHeightForShape } from './notebookNodeOutputHeight'import type { NotebookNodeSQLV2Result } from './NotebookNodeSQLV2'import { SQL_V2_DEFAULT_PAGE_SIZE, collectSqlV2Refs, notebookNodeSQLV2Logic } from './notebookNodeSQLV2Logic'import { NotebookDataframeResult } from './pythonExecution'@@ -49,7 +49,7 @@ const Component = ({updateAttributes,}: NotebookNodeProps<NotebookNodePythonV2Attributes>): JSX.Element | null => {const nodeLogic = useMountedLogic(notebookNodeLogic)- const { nodeId, notebookLogic, expanded } = useValues(nodeLogic)+ const { nodeId, notebookLogic, expanded, isEditable } = useValues(nodeLogic)const notebookShortId = notebookLogic.props.shortIdconst dataLogic = notebookNodeSQLV2Logic({@@ -95,11 +95,15 @@ const Component = ({// Grow a still-too-short node to fit the output each run lands, so it's readable without a// manual resize. Sized to what came back — a printed value stays compact, a table or figure// grows up to a cap. Only grows, and only for a run we haven't sized yet, so a deliberate- // resize (or a reload of an already-sized cell) is left untouched.- const sizedRunIdRef = useRef<string | null | undefined>(result ? (attributes.runId ?? null) : undefined)+ // resize is left untouched.+ const sizedRunIdRef = useRef<string | null | undefined>(+ initialSizedRunId({ hasResult: !!result, height: attributes.height, runId: attributes.runId ?? null })+ )useEffect(() => {const runId = attributes.runId ?? null- if (!result || runId === sizedRunIdRef.current) {+ // A read-only notebook lays the node out from its content, so there is no fixed height to+ // outgrow — and no editor to persist one into.+ if (!result || !isEditable || runId === sizedRunIdRef.current) {return}sizedRunIdRef.current = runId@@ -112,7 +116,7 @@ const Component = ({updateAttributes({ height: target })}// oxlint-disable-next-line exhaustive-deps- }, [result, attributes.runId])+ }, [result, attributes.runId, isEditable])if (!expanded) {return nulldiff --git a/frontend/src/scenes/notebooks/Nodes/NotebookNodeSQLV2.tsx b/frontend/src/scenes/notebooks/Nodes/NotebookNodeSQLV2.tsxindex 2bf5dfe31ace..285d0f9d8a84 100644--- a/frontend/src/scenes/notebooks/Nodes/NotebookNodeSQLV2.tsx+++ b/frontend/src/scenes/notebooks/Nodes/NotebookNodeSQLV2.tsx@@ -19,7 +19,7 @@ import { NotebookRunDownstreamBanner } from './components/NotebookRunDownstreamBimport { NotebookCodeSQLEditorSettings } from './components/NotebookSQLEditor'import { NotebookStaleCellBanner } from './components/NotebookStaleCellBanner'import { notebookNodeLogic } from './notebookNodeLogic'-import { outputHeightForShape } from './notebookNodeOutputHeight'+import { initialSizedRunId, outputHeightForShape } from './notebookNodeOutputHeight'import { SQL_V2_DEFAULT_PAGE_SIZE, collectSqlV2Refs, notebookNodeSQLV2Logic } from './notebookNodeSQLV2Logic'import { NotebookDataframeResult } from './pythonExecution'@@ -110,7 +110,7 @@ const Component = ({updateAttributes,}: NotebookNodeProps<NotebookNodeSQLV2Attributes>): JSX.Element | null => {const nodeLogic = useMountedLogic(notebookNodeLogic)- const { nodeId, notebookLogic, expanded, sqlV2ReturnVariableUsage } = useValues(nodeLogic)+ const { nodeId, notebookLogic, expanded, sqlV2ReturnVariableUsage, isEditable } = useValues(nodeLogic)const { navigateToNode } = useActions(nodeLogic)const notebookShortId = notebookLogic.props.shortId@@ -179,11 +179,15 @@ const Component = ({// Grow a still-too-short node to fit the result each run lands, so output is readable without// a manual resize. Sized to the rows that came back — a scalar stays compact, a wide result// grows up to a cap. Only grows, and only for a run we haven't sized yet, so a deliberate- // resize (or a reload of an already-sized cell) is left untouched.- const sizedRunIdRef = useRef<string | null | undefined>(result ? (attributes.runId ?? null) : undefined)+ // resize is left untouched.+ const sizedRunIdRef = useRef<string | null | undefined>(+ initialSizedRunId({ hasResult: !!result, height: attributes.height, runId: attributes.runId ?? null })+ )useEffect(() => {const runId = attributes.runId ?? null- if (!result || runId === sizedRunIdRef.current) {+ // A read-only notebook lays the node out from its content, so there is no fixed height to+ // outgrow — and no editor to persist one into.+ if (!result || !isEditable || runId === sizedRunIdRef.current) {return}sizedRunIdRef.current = runId@@ -195,7 +199,7 @@ const Component = ({updateAttributes({ height: target })}// oxlint-disable-next-line exhaustive-deps- }, [result, attributes.runId])+ }, [result, attributes.runId, isEditable])if (!expanded) {return nulldiff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.test.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.test.tsindex e43cbcd1b1ab..196d31e2d61e 100644--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.test.ts+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.test.ts@@ -1,4 +1,4 @@-import { countTextLines, outputHeightForShape } from './notebookNodeOutputHeight'+import { countTextLines, initialSizedRunId, outputHeightForShape } from './notebookNodeOutputHeight'describe('notebookNodeOutputHeight', () => {it('returns null when there is nothing to show', () => {@@ -27,6 +27,19 @@ describe('notebookNodeOutputHeight', () => {expect(outputHeightForShape({ hasMedia: true })!).toBeGreaterThan(outputHeightForShape({ textLines: 1 })!)})+ it.each([+ // An MCP run writes the envelope into the markdown but no height, so the cell arrives+ // with a result it has never been sized for.+ { when: 'a result that arrived without a height', hasResult: true, height: undefined, runId: 'run-1' },+ { when: 'no result yet', hasResult: false, height: undefined, runId: null },+ ])('treats $when as needing sizing', ({ hasResult, height, runId }) => {+ expect(initialSizedRunId({ hasResult, height, runId })).toBeUndefined()+ })++ it('leaves a cell the editor already sized alone', () => {+ expect(initialSizedRunId({ hasResult: true, height: 300, runId: 'run-1' })).toEqual('run-1')+ })+it('counts text lines across streams, ignoring a trailing newline', () => {expect(countTextLines('a\nb\n', 'c')).toEqual(3)expect(countTextLines(null, undefined, '')).toEqual(0)diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.tsindex 116bc43ca43f..90a183b8916a 100644--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.ts+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeOutputHeight.ts@@ -50,3 +50,19 @@ export const outputHeightForShape = ({}return Math.min(MAX_OUTPUT_HEIGHT, Math.max(MIN_OUTPUT_HEIGHT, NODE_CHROME_HEIGHT + content))}++/**+ * The run a mounting cell is already sized for. A cell auto-sizes a given run only once, and this+ * seeds that guard: a cell carrying a height was sized by the editor that ran it, while one+ * carrying a result but no height ran outside the editor — an MCP run writes the envelope straight+ * into the markdown — so it still needs sizing on its first render.+ */+export const initialSizedRunId = ({+ hasResult,+ height,+ runId,+}: {+ hasResult: boolean+ height: string | number | undefined+ runId: string | null+}): string | null | undefined => (hasResult && typeof height === 'number' ? runId : undefined)