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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Refactor dependency graph custom node and memo reach components (#322)
Myles Scolnickc108a4a8131c
diff --git a/frontend/src/components/dependency-graph/custom-node.tsx b/frontend/src/components/dependency-graph/custom-node.tsxindex e1d6c09ae3..f31eeb5234 100644--- a/frontend/src/components/dependency-graph/custom-node.tsx+++ b/frontend/src/components/dependency-graph/custom-node.tsx@@ -1,15 +1,10 @@/* Copyright 2023 Marimo. All rights reserved. */-import { CellData } from "@/core/model/cells";import { TinyCode } from "@/editor/cell/TinyCode";import { cn } from "@/lib/utils";-import { Atom, useAtomValue } from "jotai";+import { useAtomValue } from "jotai";import { memo } from "react";-import { Handle, Position, NodeProps, useStore } from "reactflow";--export function getHeight(linesOfCode: number) {- const LINE_HEIGHT = 11; // matches TinyCode.css- return Math.min(linesOfCode * LINE_HEIGHT + 35, 200);-}+import { Handle, Position, useStore } from "reactflow";+import { CustomNodeProps, getNodeHeight } from "./elements";function getWidth(canvasWidth: number) {const minWidth = 100;@@ -18,7 +13,7 @@ function getWidth(canvasWidth: number) {return Math.min(Math.max(canvasWidth - padding * 2, minWidth), maxWidth);}-export const CustomNode = memo((props: NodeProps<{ atom: Atom<CellData> }>) => {+export const CustomNode = memo((props: CustomNodeProps) => {const { data, selected, id } = props;const cell = useAtomValue(data.atom);const nonSelectedColor = "var(--gray-3)";@@ -47,7 +42,7 @@ export const CustomNode = memo((props: NodeProps<{ atom: Atom<CellData> }>) => {selected && "border-primary")}style={{- height: getHeight(linesOfCode),+ height: getNodeHeight(linesOfCode),width: getWidth(reactFlowWidth),}}>diff --git a/frontend/src/components/dependency-graph/dependency-graph.tsx b/frontend/src/components/dependency-graph/dependency-graph.tsxindex f4dbaaa98d..01c4e0d08c 100644--- a/frontend/src/components/dependency-graph/dependency-graph.tsx+++ b/frontend/src/components/dependency-graph/dependency-graph.tsx@@ -2,7 +2,6 @@import ReactFlow, {Node,Edge,- MarkerType,ReactFlowProvider,useEdgesState,useNodesState,@@ -13,19 +12,16 @@ import ReactFlow, {} from "reactflow";import React, { useEffect, useMemo, useState } from "react";-import {- CustomNode,- getHeight,-} from "@/components/dependency-graph/custom-node";+import { CustomNode } from "@/components/dependency-graph/custom-node";import { Variables } from "@/core/variables/types";import { CellId } from "@/core/model/ids";import { CellData } from "@/core/model/cells";import { Atom } from "jotai";-import { store } from "@/core/state/jotai";import "reactflow/dist/style.css";import "./dependency-graph.css";import { useDebouncedCallback } from "@/hooks/useDebounce";+import { createElements } from "./elements";interface Props {cellIds: CellId[];@@ -134,71 +130,6 @@ const DependencyGraphInternal: React.FC<Props> = ({);};-function createEdge(source: CellId, target: CellId, direction: string): Edge {- return {- type: "smoothstep",- pathOptions: {- offset: 20,- borderRadius: 100,- },- data: {- direction: direction,- },- // animated: true,- markerEnd: {- type: MarkerType.Arrow,- },- id: `${source}-${target}-${direction}`,- source: source,- sourceHandle: direction,- targetHandle: direction,- target: target,- };-}--function createNode(id: string, atom: Atom<CellData>, prevY: number): Node {- const linesOfCode = store.get(atom).code.trim().split("\n").length;- const height = getHeight(linesOfCode);- return {- id: id,- data: { atom },- width: 250,- type: "custom",- height: height,- position: { x: 0, y: prevY + 20 },- };-}--function createElements(- cellIds: CellId[],- cellAtoms: Array<Atom<CellData>>,- variables: Variables-) {- let prevY = 0;- const nodes: Node[] = [];- const edges: Edge[] = [];- let index = 0;- for (const cellId of cellIds) {- const node = createNode(cellId, cellAtoms[index], prevY);- nodes.push(node);- prevY = node.position.y + (node.height || 0);- index++;- }-- for (const variable of Object.values(variables)) {- const { declaredBy, usedBy } = variable;- for (const fromId of declaredBy) {- for (const toId of usedBy) {- edges.push(- createEdge(fromId, toId, "inputs"),- createEdge(fromId, toId, "outputs")- );- }- }- }- return { nodes, edges };-}-// Limit the extent of the graph to just the visible nodes.// The top node and bottom node can be scrolled to the middle of the graph.function useTranslateExtent(nodes: Node[], height: number): CoordinateExtent {diff --git a/frontend/src/components/dependency-graph/elements.ts b/frontend/src/components/dependency-graph/elements.tsnew file mode 100644index 0000000000..114c602806--- /dev/null+++ b/frontend/src/components/dependency-graph/elements.ts@@ -0,0 +1,88 @@+/* Copyright 2023 Marimo. All rights reserved. */++import { CellData } from "@/core/model/cells";+import { CellId } from "@/core/model/ids";+import { store } from "@/core/state/jotai";+import { Variables } from "@/core/variables/types";+import { Edge, MarkerType, Node, NodeProps } from "reactflow";+import { Atom } from "jotai";++export interface NodeData {+ atom: Atom<CellData>;+}+export type CustomNodeProps = NodeProps<NodeData>;++export function getNodeHeight(linesOfCode: number) {+ const LINE_HEIGHT = 11; // matches TinyCode.css+ return Math.min(linesOfCode * LINE_HEIGHT + 35, 200);+}++/* Copyright 2023 Marimo. All rights reserved. */+function createEdge(source: CellId, target: CellId, direction: string): Edge {+ return {+ type: "smoothstep",+ pathOptions: {+ offset: 20,+ borderRadius: 100,+ },+ data: {+ direction: direction,+ },+ // animated: true,+ markerEnd: {+ type: MarkerType.Arrow,+ },+ id: `${source}-${target}-${direction}`,+ source: source,+ sourceHandle: direction,+ targetHandle: direction,+ target: target,+ };+}++function createNode(+ id: string,+ atom: Atom<CellData>,+ prevY: number+): Node<NodeData> {+ const linesOfCode = store.get(atom).code.trim().split("\n").length;+ const height = getNodeHeight(linesOfCode);+ return {+ id: id,+ data: { atom },+ width: 250,+ type: "custom",+ height: height,+ position: { x: 0, y: prevY + 20 },+ };+}++export function createElements(+ cellIds: CellId[],+ cellAtoms: Array<Atom<CellData>>,+ variables: Variables+) {+ let prevY = 0;+ const nodes: Array<Node<NodeData>> = [];+ const edges: Edge[] = [];+ let index = 0;+ for (const cellId of cellIds) {+ const node = createNode(cellId, cellAtoms[index], prevY);+ nodes.push(node);+ prevY = node.position.y + (node.height || 0);+ index++;+ }++ for (const variable of Object.values(variables)) {+ const { declaredBy, usedBy } = variable;+ for (const fromId of declaredBy) {+ for (const toId of usedBy) {+ edges.push(+ createEdge(fromId, toId, "inputs"),+ createEdge(fromId, toId, "outputs")+ );+ }+ }+ }+ return { nodes, edges };+}diff --git a/frontend/src/editor/cell/TinyCode.tsx b/frontend/src/editor/cell/TinyCode.tsxindex 939e93c655..098b36a032 100644--- a/frontend/src/editor/cell/TinyCode.tsx+++ b/frontend/src/editor/cell/TinyCode.tsx@@ -1,5 +1,5 @@/* Copyright 2023 Marimo. All rights reserved. */-import React from "react";+import React, { memo } from "react";import CodeMirror, { minimalSetup } from "@uiw/react-codemirror";import { python } from "@codemirror/lang-python";@@ -10,7 +10,7 @@ interface Props {code: string;}-export const TinyCode: React.FC<Props> = ({ code }) => {+export const TinyCode: React.FC<Props> = memo(({ code }) => {const { theme } = useTheme();return (@@ -26,7 +26,7 @@ export const TinyCode: React.FC<Props> = ({ code }) => {python(),minimalSetup({syntaxHighlighting: true,- // Rest false+ // Other options are falsehighlightSpecialChars: false,history: false,drawSelection: false,@@ -38,4 +38,6 @@ export const TinyCode: React.FC<Props> = ({ code }) => {/></div>);-};+});++TinyCode.displayName = "TinyCode";diff --git a/frontend/src/plugins/impl/data-frames/python/code-panel.tsx b/frontend/src/plugins/impl/data-frames/python/code-panel.tsxindex b425366cae..e4b7f3058b 100644--- a/frontend/src/plugins/impl/data-frames/python/code-panel.tsx+++ b/frontend/src/plugins/impl/data-frames/python/code-panel.tsx@@ -1,5 +1,5 @@/* Copyright 2023 Marimo. All rights reserved. */-import React from "react";+import React, { memo } from "react";import { Transformations } from "../schema";import { python } from "@codemirror/lang-python";import CodeMirror, { EditorView } from "@uiw/react-codemirror";@@ -27,7 +27,7 @@ export const CodePanel: React.FC<Props> = ({ transforms, dataframeName }) => {);};-const PythonCode = (props: { code: string }) => {+const PythonCode = memo((props: { code: string }) => {const { theme } = useThemeForPlugin();return (<div className="relative min-h-[200px]">@@ -44,7 +44,8 @@ const PythonCode = (props: { code: string }) => {/></div>);-};+});+PythonCode.displayName = "PythonCode";const FloatingCopyButton = (props: { text: string }) => {const copy = Events.stopPropagation(() => {