: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
feat(notebooks): stop suggesting the same dataframe name for every python cell (#73894)
Sinan Kucukkoseler407bf2742671
diff --git a/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsx b/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsxindex f9345d7160cc..c1d2f6b5ba6f 100644--- a/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsx+++ b/frontend/src/scenes/notebooks/Nodes/NotebookNodePythonV2.tsx@@ -202,9 +202,11 @@ const Component = ({<inputtype="text"// The dataframe name this cell's result is exposed as to later cells.+ // Optional: left empty, the cell binds nothing and later cells can't read it.className="rounded border border-border px-1.5 py-0.5 text-xs font-mono bg-bg-light text-default focus:outline-none focus:ring-1 focus:ring-primary"value={attributes.returnVariable ?? ''}onChange={(event) => updateAttributes({ returnVariable: event.target.value })}+ placeholder="Dataframe name (optional)"spellCheck={false}/></div>@@ -305,8 +307,10 @@ export const NotebookNodePythonV2 = createPostHogWidgetNode<NotebookNodePythonV2code: {default: '',},+ // Optional: empty means the cell binds no dataframe, so nothing downstream can read it.+ // A cell that predates the optional name carries its persisted name and keeps exporting it.returnVariable: {- default: 'df',+ default: '',},runId: {default: null,diff --git a/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsx b/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsxindex de90a3455bce..60d4d28d9588 100644--- a/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsx+++ b/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsx@@ -73,8 +73,8 @@ describe('buildDataframeTreeSection', () => {})it('collapses duplicate names, keeping the last', () => {- // Python names are deliberately not disambiguated and default to `df`, so two un-run- // Python cells is enough to emit `df` twice — duplicate ids in a virtualized tree.+ // Python names are deliberately not disambiguated (they are the kernel variables), so+ // two cells sharing a name emit it twice — duplicate ids in a virtualized tree.const section = buildDataframeTreeSection([pythonNode('df'), pythonNode('df')], [])expect(children(section)).toHaveLength(1)const ids = (section[0].children ?? []).map((c) => c.id)diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeContent.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeContent.tsindex e0fc94b2517c..f70363fcfd3d 100644--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeContent.ts+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeContent.ts@@ -498,10 +498,10 @@ export const collectNotebookFrameNodes = (content?: JSONContent | null): Notebooif (node.type === NotebookNodeType.SQLV2 || node.type === NotebookNodeType.PythonV2) {const attrs = node.attrs ?? {}const isSql = node.type === NotebookNodeType.SQLV2- // A missing SQL attribute predates the optional name (legacy 'sql_df' default);+ // A missing attribute predates the optional name (legacy 'sql_df'/'df' defaults);// an explicit blank or invalid one binds no dataframe — nothing to browse.const rawReturnVariable =- typeof attrs.returnVariable === 'string' ? attrs.returnVariable : isSql ? 'sql_df' : ''+ typeof attrs.returnVariable === 'string' ? attrs.returnVariable : isSql ? 'sql_df' : 'df'let name: string | nullif (isSql) {name = isReferenceableSqlV2FrameName(rawReturnVariable)@@ -511,7 +511,7 @@ export const collectNotebookFrameNodes = (content?: JSONContent | null): NoteboousedReturnVariables.add(normalizeSqlIdentifier(name))}} else {- name = rawReturnVariable.trim() || 'df'+ name = rawReturnVariable.trim()}if (name) {const result = attrs.result ?? null@@ -546,8 +546,10 @@ export type PythonKernelNodeSummary = {// Kernel-run Python cells (revamped notebooks): each binds its result to `returnVariable` in// the kernel namespace. Unlike the SQL collectors the names are NOT disambiguated — they are// exactly the kernel variables, so a duplicated returnVariable means last-run-wins, matching-// kernel semantics. Markdown-aware (unlike the legacy collectPythonNodes) because revamped-// markdown notebooks store their cells as `<PythonV2 …/>` component tags.+// kernel semantics. A blank name is a display-only cell that binds nothing (see the SQLV2+// collector); only a missing attribute takes the legacy 'df' default. Markdown-aware (unlike+// the legacy collectPythonNodes) because revamped markdown notebooks store their cells as+// `<PythonV2 …/>` component tags.export const collectPythonKernelNodes = (content?: JSONContent | null): PythonKernelNodeSummary[] => {if (!content || typeof content !== 'object') {return []@@ -561,10 +563,7 @@ export const collectPythonKernelNodes = (content?: JSONContent | null): PythonKe}if (node.type === NotebookNodeType.PythonV2) {const attrs = node.attrs ?? {}- const returnVariable =- typeof attrs.returnVariable === 'string' && attrs.returnVariable.trim()- ? attrs.returnVariable.trim()- : 'df'+ const returnVariable = typeof attrs.returnVariable === 'string' ? attrs.returnVariable.trim() : 'df'nodes.push({ nodeId: attrs.nodeId ?? '', returnVariable })}if (node.type === NotebookNodeType.MarkdownNotebook) {@@ -827,11 +826,8 @@ export const buildNotebookDependencyGraph = (content?: JSONContent | null): Noteconst attrs = node.attrs ?? {}pythonV2Index += 1// The returnVariable IS the kernel variable, never disambiguated — the same- // last-write-wins semantics as collectPythonKernelNodes.- const returnVariable =- typeof attrs.returnVariable === 'string' && attrs.returnVariable.trim()- ? attrs.returnVariable.trim()- : 'df'+ // last-write-wins semantics as collectPythonKernelNodes. Blank = exports nothing.+ const returnVariable = typeof attrs.returnVariable === 'string' ? attrs.returnVariable.trim() : 'df'const code = typeof attrs.code === 'string' ? attrs.code : ''nodes.push({nodeId: attrs.nodeId ?? '',diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.tsindex bb0a8e062994..00605f785f46 100644--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts@@ -91,11 +91,19 @@ describe('notebookNodeSQLV2Logic', () => {it('collects python cells as local refs under their kernel variable name', () => {// Journey 5: a SQL node referencing new_events must reroute to DuckDB, which only// happens if the python cell's returnVariable reaches the backend as a local ref.- const document = doc(sqlNode('a', 'df1'), pythonNode('py', 'new_events'), pythonNode('py2'))+ // A blank name binds nothing in the kernel, so it exports no ref — otherwise every+ // unnamed cell would claim the same name and shadow the others. Only a cell with no+ // attribute at all predates the optional name and keeps the legacy 'df'.+ const document = doc(+ sqlNode('a', 'df1'),+ pythonNode('py', 'new_events'),+ pythonNode('py2', ''),+ pythonNode('py3')+ )expect(collectSqlV2Refs(document, 'self')).toEqual({df1: hogql('a'),new_events: local('py'),- df: local('py2'), // returnVariable defaults to 'df', matching the python cell UI+ df: local('py3'),})})diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.tsindex 3943baaabe1b..4f3efbfba067 100644--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts@@ -55,7 +55,8 @@ export function collectSqlV2Refs(doc: JSONContent | null | undefined, selfNodeId}}for (const node of collectPythonKernelNodes(doc)) {- if (node.nodeId && node.nodeId !== selfNodeId && !(node.returnVariable in refs)) {+ // An unnamed python cell binds nothing in the kernel, so there is no frame to reference.+ if (node.nodeId && node.nodeId !== selfNodeId && node.returnVariable && !(node.returnVariable in refs)) {refs[node.returnVariable] = { node_id: node.nodeId, kind: 'local' }}}