Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: add data attributes for the cell to serve as selectors (#3497)

Myles Scolnickf7f1de4da25c
diff --git a/docs/guides/configuration/theming.md b/docs/guides/configuration/theming.md
index 91c7b885f0..da2aae8598 100644
--- a/docs/guides/configuration/theming.md
+++ b/docs/guides/configuration/theming.md
@@ -47,6 +47,22 @@ You can further customize your notebook by adding custom HTML in the `<head>` se
See the [Custom HTML Head](html_head.md) guide for more details.
+## Targeting cells
+
+You can target a cell's styles from the `data-cell-name` attribute. You can also target a cell's output with the `data-cell-role="output"` attribute.
+
+```css
+/* Target the cell named "My Cell" */
+[data-cell-name="my_cell"] {
+ background-color: light-dark(navy, pink);
+}
+
+/* Target the output of the cell named "My Cell" */
+[data-cell-name="my_cell"] [data-cell-role="output"] {
+ background-color: light-dark(navy, pink);
+}
+```
+
## Community Themes
The marimo community maintains a [library of custom themes](https://github.com/metaboulie/marimo-themes) that you can use in your notebooks. The library includes various themes like "coldme", "nord", "mininini", and "wigwam", each supporting both light and dark modes.
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 57fad18077..fbfe3eb642 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -130,7 +130,6 @@ export interface CellProps
collapseCount: number;
}
-// TODO(akshayka): a component for displaying/editing the cell's name.
const CellComponent = (
{
theme,
@@ -592,7 +591,13 @@ const CellComponent = (
canMoveX={canMoveX}
title={cellTitle()}
>
- <div className={className} id={HTMLId} ref={cellContainerRef}>
+ <div
+ className={className}
+ id={HTMLId}
+ ref={cellContainerRef}
+ data-cell-id={cellId}
+ data-cell-name={name}
+ >
{cellOutput === "above" && outputArea}
<div className={cn("tray")} data-hidden={isMarkdownCodeHidden}>
<div className="absolute right-2 -top-4 z-10">
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index 63cafabbd0..a2962ed92d 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -407,6 +407,7 @@ const ExpandableOutput = React.memo(
</div>
<div
{...props}
+ data-cell-role="output"
className={cn(
"relative fullscreen:bg-background fullscreen:flex fullscreen:items-center fullscreen:justify-center",
"fullscreen:[align-items:safe_center]",
diff --git a/frontend/src/components/editor/__tests__/data-attributes.test.tsx b/frontend/src/components/editor/__tests__/data-attributes.test.tsx
new file mode 100644
index 0000000000..bd516677de
--- /dev/null
+++ b/frontend/src/components/editor/__tests__/data-attributes.test.tsx
@@ -0,0 +1,146 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { render } from "@testing-library/react";
+import { beforeAll, describe, expect, it } from "vitest";
+import { Cell } from "../Cell";
+import { OutputArea } from "../Output";
+import type { CellId } from "@/core/cells/ids";
+import type { OutputMessage } from "@/core/kernel/messages";
+import { Functions } from "@/utils/functions";
+import type { UserConfig } from "@/core/config/config-schema";
+import { TooltipProvider } from "@/components/ui/tooltip";
+
+beforeAll(() => {
+ global.ResizeObserver = class ResizeObserver {
+ observe() {
+ // do nothing
+ }
+ unobserve() {
+ // do nothing
+ }
+ disconnect() {
+ // do nothing
+ }
+ };
+ global.HTMLDivElement.prototype.scrollIntoView = () => {
+ // do nothing
+ };
+});
+
+describe("Cell data attributes", () => {
+ it("should render cell with data-cell-id and data-cell-name", () => {
+ const cellId = "test" as CellId;
+ const cellName = "test_cell";
+
+ const userConfig: UserConfig = {
+ display: {
+ cell_output: "below",
+ code_editor_font_size: 14,
+ dataframes: "rich",
+ default_width: "normal",
+ theme: "light",
+ },
+ keymap: { preset: "default" },
+ completion: {
+ activate_on_typing: true,
+ copilot: false,
+ },
+ formatting: { line_length: 88 },
+ package_management: { manager: "pip" },
+ runtime: {
+ auto_instantiate: false,
+ auto_reload: "off",
+ on_cell_change: "lazy",
+ },
+ server: {
+ browser: "default",
+ follow_symlink: false,
+ },
+ save: { autosave: "off", autosave_delay: 1000, format_on_save: false },
+ ai: {},
+ };
+
+ const { container } = render(
+ <TooltipProvider>
+ <Cell
+ id={cellId}
+ name={cellName}
+ code=""
+ output={null}
+ consoleOutputs={[]}
+ status="idle"
+ edited={false}
+ interrupted={false}
+ errored={false}
+ stopped={false}
+ staleInputs={false}
+ runStartTimestamp={null}
+ lastRunStartTimestamp={null}
+ runElapsedTimeMs={null}
+ serializedEditorState={null}
+ mode="edit"
+ debuggerActive={false}
+ appClosed={false}
+ canDelete={true}
+ updateCellCode={Functions.NOOP}
+ prepareForRun={Functions.NOOP}
+ createNewCell={Functions.NOOP}
+ deleteCell={Functions.NOOP}
+ focusCell={Functions.NOOP}
+ moveCell={Functions.NOOP}
+ setStdinResponse={Functions.NOOP}
+ moveToNextCell={Functions.NOOP}
+ updateCellConfig={Functions.NOOP}
+ clearSerializedEditorState={Functions.NOOP}
+ sendToBottom={Functions.NOOP}
+ sendToTop={Functions.NOOP}
+ collapseCell={Functions.NOOP}
+ expandCell={Functions.NOOP}
+ userConfig={userConfig}
+ outline={null}
+ isCollapsed={false}
+ collapseCount={0}
+ config={{
+ disabled: false,
+ hide_code: false,
+ column: null,
+ }}
+ canMoveX={false}
+ theme="light"
+ showPlaceholder={false}
+ allowFocus={true}
+ />
+ </TooltipProvider>,
+ );
+
+ const cellElement = container.querySelector(`[data-cell-id="${cellId}"]`);
+ expect(cellElement).toBeTruthy();
+ expect(cellElement?.getAttribute("data-cell-name")).toBe(cellName);
+ });
+});
+
+describe("Output data attributes", () => {
+ it("should render output with data-cell-role", () => {
+ const cellId = "test" as CellId;
+ const output: OutputMessage = {
+ channel: "output",
+ mimetype: "text/plain",
+ data: "test output",
+ timestamp: 0,
+ };
+
+ const { container } = render(
+ <TooltipProvider>
+ <OutputArea
+ output={output}
+ cellId={cellId}
+ stale={false}
+ allowExpand={true}
+ className="test-output"
+ />
+ </TooltipProvider>,
+ );
+
+ const outputElement = container.querySelector('[data-cell-role="output"]');
+ expect(outputElement).toBeTruthy();
+ });
+});