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
improve: Easier `altair` installation dataframe panel helper text (QoL) (#3961)
Srihari Thyagarajanc87e9053ab17
diff --git a/frontend/src/components/datasources/__tests__/install-package-button.test.tsx b/frontend/src/components/datasources/__tests__/install-package-button.test.tsxnew file mode 100644index 0000000000..e4c1c0c26c--- /dev/null+++ b/frontend/src/components/datasources/__tests__/install-package-button.test.tsx@@ -0,0 +1,67 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { describe, it, expect, vi, beforeEach } from "vitest";+import { render, screen, fireEvent } from "@testing-library/react";+import { InstallPackageButton } from "../install-package-button";++const mockOpenApplication = vi.fn();+vi.mock("@/components/editor/chrome/state", () => ({+ useChromeActions: () => ({+ openApplication: mockOpenApplication,+ }),+}));++const mockSetPackagesToInstall = vi.fn();+vi.mock("jotai", () => ({+ atom: () => ({}),+ useSetAtom: () => mockSetPackagesToInstall,+}));++vi.mock("@/components/editor/chrome/panels/packages-state", () => ({+ packagesToInstallAtom: {},+}));++const mockRequestAnimationFrame = vi.fn((callback) => {+ callback();+ return 1;+});+vi.stubGlobal("requestAnimationFrame", mockRequestAnimationFrame);++const mockInput = {+ focus: vi.fn(),+ value: "",+ dispatchEvent: vi.fn(),+};+vi.spyOn(document, "getElementById").mockReturnValue(+ mockInput as unknown as HTMLInputElement,+);++describe("InstallPackageButton", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("should not render when packages is undefined", () => {+ const { container } = render(<InstallPackageButton packages={undefined} />);+ expect(container.firstChild).toBeNull();+ });++ it("should not render when packages is empty", () => {+ const { container } = render(<InstallPackageButton packages={[]} />);+ expect(container.firstChild).toBeNull();+ });++ it("should render with the correct package names", () => {+ render(<InstallPackageButton packages={["altair", "pandas"]} />);+ expect(screen.getByText("Install altair, pandas")).toBeInTheDocument();+ });++ it("should open the packages panel when clicked", () => {+ render(<InstallPackageButton packages={["altair"]} />);++ fireEvent.click(screen.getByText("Install altair"));++ expect(mockSetPackagesToInstall).toHaveBeenCalledWith("altair");++ expect(mockOpenApplication).toHaveBeenCalledWith("packages");+ });+});diff --git a/frontend/src/components/datasources/datasources.tsx b/frontend/src/components/datasources/datasources.tsxindex 1be8c18a7b..018f1486ee 100644--- a/frontend/src/components/datasources/datasources.tsx+++ b/frontend/src/components/datasources/datasources.tsx@@ -73,6 +73,7 @@ import {LoadingState,RotatingChevron,} from "./components";+import { InstallPackageButton } from "./install-package-button";const sortedTablesAtom = atom((get) => {const tables = get(datasetTablesAtom);@@ -792,7 +793,12 @@ const DatasetColumnPreview: React.FC<{}const error = preview.error && (- <span className="text-xs text-muted-foreground">{preview.error}</span>+ <div className="text-xs text-muted-foreground p-2 block border border-muted rounded flex items-center">+ <span>{preview.error}</span>+ {preview.missing_packages && (+ <InstallPackageButton packages={preview.missing_packages} />+ )}+ </div>);const summary = preview.summary && (diff --git a/frontend/src/components/datasources/install-package-button.tsx b/frontend/src/components/datasources/install-package-button.tsxnew file mode 100644index 0000000000..570082f143--- /dev/null+++ b/frontend/src/components/datasources/install-package-button.tsx@@ -0,0 +1,41 @@+/* Copyright 2024 Marimo. All rights reserved. */+import React from "react";+import { Button } from "@/components/ui/button";+import { useChromeActions } from "@/components/editor/chrome/state";+import { packagesToInstallAtom } from "@/components/editor/chrome/panels/packages-state";+import { useSetAtom } from "jotai";++interface InstallPackageButtonProps {+ packages: string[] | undefined;+}++/**+ * Button to install missing packages+ * Opens the packages panel and focuses the input+ */+export const InstallPackageButton: React.FC<InstallPackageButtonProps> = ({+ packages,+}) => {+ const chromeActions = useChromeActions();+ const setPackagesToInstall = useSetAtom(packagesToInstallAtom);++ if (!packages || packages.length === 0) {+ return null;+ }++ const handleClick = () => {+ const packagesString = packages.join(", ");++ // Set the packages to install+ setPackagesToInstall(packagesString);++ // Open the packages panel+ chromeActions.openApplication("packages");+ };++ return (+ <Button variant="outline" size="xs" onClick={handleClick} className="ml-2">+ Install {packages.join(", ")}+ </Button>+ );+};diff --git a/frontend/src/components/editor/chrome/panels/packages-panel.tsx b/frontend/src/components/editor/chrome/panels/packages-panel.tsxindex 53513d62e4..16fabca7b8 100644--- a/frontend/src/components/editor/chrome/panels/packages-panel.tsx+++ b/frontend/src/components/editor/chrome/panels/packages-panel.tsx@@ -29,6 +29,8 @@ import { Events } from "@/utils/events";import { copyToClipboard } from "@/utils/copy";import { PACKAGES_INPUT_ID } from "./constants";import { useOpenSettingsToTab } from "@/components/app-config/state";+import { packagesToInstallAtom } from "./packages-state";+import { useAtomValue, useSetAtom } from "jotai";export const PackagesPanel: React.FC = () => {const [config] = useResolvedMarimoConfig();@@ -65,6 +67,19 @@ const InstallPackageForm: React.FC<{const [loading, setLoading] = React.useState(false);const { handleClick: openSettings } = useOpenSettingsToTab();+ // Get the packages to install from the atom+ const packagesToInstall = useAtomValue(packagesToInstallAtom);+ const setPackagesToInstall = useSetAtom(packagesToInstallAtom);++ // Set the input value when packagesToInstall changes+ React.useEffect(() => {+ if (packagesToInstall) {+ setInput(packagesToInstall);+ // Clear the atom after setting the input+ setPackagesToInstall(null);+ }+ }, [packagesToInstall, setPackagesToInstall]);+const handleAddPackage = async () => {try {setLoading(true);diff --git a/frontend/src/components/editor/chrome/panels/packages-state.ts b/frontend/src/components/editor/chrome/panels/packages-state.tsnew file mode 100644index 0000000000..04a527a47c--- /dev/null+++ b/frontend/src/components/editor/chrome/panels/packages-state.ts@@ -0,0 +1,5 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { atom } from "jotai";++// Atom to store the packages to prefill in the packages panel+export const packagesToInstallAtom = atom<string | null>(null);diff --git a/marimo/_data/preview_column.py b/marimo/_data/preview_column.pyindex a861cd1585..5115d9423e 100644--- a/marimo/_data/preview_column.py+++ b/marimo/_data/preview_column.py@@ -58,11 +58,10 @@ def get_column_preview_dataframe(# We require altair to render the charterror = None+ missing_packages = Noneif not DependencyManager.altair.has():- error = (- "Altair is required to render charts. "- "Install it with `pip install altair`."- )+ error = "Altair is required to render charts."+ missing_packages = ["altair"]else:# Check for special characters that can't be escaped easily# (e.g. backslash, quotes)@@ -101,6 +100,7 @@ def get_column_preview_dataframe(chart_code=chart_code,summary=summary,error=error,+ missing_packages=missing_packages,)except Exception as e:LOGGER.warning(@@ -113,6 +113,7 @@ def get_column_preview_dataframe(table_name=table_name,column_name=column_name,error=str(e),+ missing_packages=None,)@@ -128,6 +129,8 @@ def get_column_preview_for_sql(chart_spec = Nonechart_code = Nonechart_max_rows_errors = False+ error = None+ missing_packages = Noneif histogram_data and DependencyManager.altair.has():chart_builder = get_chart_builder(column_type, False)@@ -143,7 +146,8 @@ def get_column_preview_for_sql(chart_spec=chart_spec,chart_code=chart_code,summary=summary,- error=None,+ error=error,+ missing_packages=missing_packages,)diff --git a/marimo/_messaging/ops.py b/marimo/_messaging/ops.pyindex 4a63bcf79e..1d9f4fabb6 100644--- a/marimo/_messaging/ops.py+++ b/marimo/_messaging/ops.py@@ -623,6 +623,7 @@ class DataColumnPreview(Op):chart_max_rows_errors: bool = Falsechart_code: Optional[str] = Noneerror: Optional[str] = None+ missing_packages: Optional[list[str]] = Nonesummary: Optional[ColumnSummary] = Nonediff --git a/openapi/api.yaml b/openapi/api.yamlindex 4eccee26fa..986c1bf48c 100644--- a/openapi/api.yaml+++ b/openapi/api.yaml@@ -365,6 +365,11 @@ components:error:nullable: truetype: string+ missing_packages:+ nullable: true+ type: array+ items:+ type: stringname:enum:- data-column-previewdiff --git a/openapi/src/api.ts b/openapi/src/api.tsindex 1241dfcaf3..f74fbc63b9 100644--- a/openapi/src/api.ts+++ b/openapi/src/api.ts@@ -2296,6 +2296,7 @@ export interface components {chart_spec?: string | null;column_name: string;error?: string | null;+ missing_packages?: string[] | null;/** @enum {string} */name: "data-column-preview";summary?: components["schemas"]["ColumnSummary"];