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.

improvement: install packages directly (#5434)

Shahmir Varqhae044d49aa931
diff --git a/frontend/src/components/data-table/column-explorer-panel/column-explorer.tsx b/frontend/src/components/data-table/column-explorer-panel/column-explorer.tsx
index 0096888d48..53ca5c6593 100644
--- a/frontend/src/components/data-table/column-explorer-panel/column-explorer.tsx
+++ b/frontend/src/components/data-table/column-explorer-panel/column-explorer.tsx
@@ -78,7 +78,7 @@ export const ColumnExplorerPanel = ({
className="h-3 w-3 ml-1 mt-0.5"
/>
</span>
- <Command className="h-5/6" shouldFilter={false}>
+ <Command className="h-5/6 bg-background" shouldFilter={false}>
<CommandInput
placeholder="Search columns..."
value={searchValue}
@@ -170,7 +170,12 @@ const ColumnPreview = ({
}) => {
const { theme } = useTheme();
- const { data, error, isPending } = useAsyncData(async () => {
+ const {
+ data,
+ error,
+ isPending,
+ refetch: refetchPreview,
+ } = useAsyncData(async () => {
const response = await previewColumn({ column: columnName });
return response;
}, []);
@@ -196,7 +201,8 @@ const ColumnPreview = ({
} = data;
const errorState =
- previewError && renderPreviewError(previewError, missing_packages);
+ previewError &&
+ renderPreviewError(previewError, missing_packages, refetchPreview);
const previewStats = stats && renderStats(stats, dataType);
diff --git a/frontend/src/components/datasources/__tests__/install-package-button.test.tsx b/frontend/src/components/datasources/__tests__/install-package-button.test.tsx
index 34b7a75dff..4ce2368b07 100644
--- a/frontend/src/components/datasources/__tests__/install-package-button.test.tsx
+++ b/frontend/src/components/datasources/__tests__/install-package-button.test.tsx
@@ -1,6 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { fireEvent, render, screen } from "@testing-library/react";
+import { render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { InstallPackageButton } from "../install-package-button";
@@ -11,12 +11,6 @@ vi.mock("@/components/editor/chrome/state", () => ({
}),
}));
-const mockSetPackagesToInstall = vi.fn();
-vi.mock("jotai", () => ({
- atom: () => ({}),
- useSetAtom: () => mockSetPackagesToInstall,
-}));
-
vi.mock("@/components/editor/chrome/panels/packages-state", () => ({
packagesToInstallAtom: {},
}));
@@ -65,14 +59,4 @@ describe("InstallPackageButton", () => {
);
expect(screen.getByText("Install altair")).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/column-preview.tsx b/frontend/src/components/datasources/column-preview.tsx
index e975280013..835e0bb85f 100644
--- a/frontend/src/components/datasources/column-preview.tsx
+++ b/frontend/src/components/datasources/column-preview.tsx
@@ -41,6 +41,18 @@ export const DatasetColumnPreview: React.FC<{
}> = ({ table, column, preview, onAddColumnChart, sqlTableContext }) => {
const { theme } = useTheme();
+ const previewColumn = () => {
+ previewDatasetColumn({
+ source: table.source,
+ tableName: table.name,
+ columnName: column.name,
+ sourceType: table.source_type,
+ fullyQualifiedTableName: sqlTableContext
+ ? `${sqlTableContext.database}.${sqlTableContext.schema}.${table.name}`
+ : table.name,
+ });
+ };
+
useOnMount(() => {
if (preview) {
return;
@@ -51,15 +63,7 @@ export const DatasetColumnPreview: React.FC<{
return;
}
- previewDatasetColumn({
- source: table.source,
- tableName: table.name,
- columnName: column.name,
- sourceType: table.source_type,
- fullyQualifiedTableName: sqlTableContext
- ? `${sqlTableContext.database}.${sqlTableContext.schema}.${table.name}`
- : table.name,
- });
+ previewColumn();
});
if (table.source_type === "connection") {
@@ -93,7 +97,7 @@ export const DatasetColumnPreview: React.FC<{
const error =
preview.error &&
- renderPreviewError(preview.error, preview.missing_packages);
+ renderPreviewError(preview.error, preview.missing_packages, previewColumn);
const stats = preview.stats && renderStats(preview.stats, column.type);
@@ -137,15 +141,17 @@ export const DatasetColumnPreview: React.FC<{
export function renderPreviewError(
error: string,
missing_packages?: string[] | null,
+ refetchPreview?: () => void,
) {
return (
- <div className="text-xs text-muted-foreground p-2 border border-muted rounded flex items-center justify-between">
+ <div className="text-xs text-muted-foreground p-2 border border-border rounded flex items-center justify-between">
<span>{error}</span>
{missing_packages && (
<InstallPackageButton
packages={missing_packages}
showMaxPackages={1}
className="w-32"
+ onInstall={refetchPreview}
/>
)}
</div>
diff --git a/frontend/src/components/datasources/install-package-button.tsx b/frontend/src/components/datasources/install-package-button.tsx
index a32d46bc75..df6f4a74fa 100644
--- a/frontend/src/components/datasources/install-package-button.tsx
+++ b/frontend/src/components/datasources/install-package-button.tsx
@@ -1,16 +1,15 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { useSetAtom } from "jotai";
import React from "react";
-import { packagesToInstallAtom } from "@/components/editor/chrome/panels/packages-state";
-import { useChromeActions } from "@/components/editor/chrome/state";
import { Button } from "@/components/ui/button";
+import { useInstallPackages } from "@/core/packages/useInstallPackage";
import { cn } from "@/utils/cn";
interface InstallPackageButtonProps {
packages: string[] | undefined;
showMaxPackages?: number;
className?: string;
+ onInstall?: () => void;
}
/**
@@ -21,29 +20,21 @@ export const InstallPackageButton: React.FC<InstallPackageButtonProps> = ({
packages,
showMaxPackages,
className,
+ onInstall,
}) => {
- const chromeActions = useChromeActions();
- const setPackagesToInstall = useSetAtom(packagesToInstallAtom);
+ const { handleInstallPackages } = useInstallPackages();
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}
+ onClick={() => {
+ handleInstallPackages(packages, onInstall);
+ }}
className={cn("ml-2", className)}
>
Install{" "}
diff --git a/frontend/src/components/editor/chrome/panels/packages-panel.tsx b/frontend/src/components/editor/chrome/panels/packages-panel.tsx
index dcd7d52f8a..41b35b243a 100644
--- a/frontend/src/components/editor/chrome/panels/packages-panel.tsx
+++ b/frontend/src/components/editor/chrome/panels/packages-panel.tsx
@@ -10,7 +10,6 @@ import React from "react";
import { useOpenSettingsToTab } from "@/components/app-config/state";
import { Spinner } from "@/components/icons/spinner";
import { SearchInput } from "@/components/ui/input";
-import { Kbd } from "@/components/ui/kbd";
import {
Table,
TableBody,
@@ -29,6 +28,11 @@ import {
removePackage,
} from "@/core/network/requests";
import type { DependencyTreeNode } from "@/core/network/types";
+import {
+ showRemovePackageToast,
+ showUpgradePackageToast,
+} from "@/core/packages/toast-components";
+import { useInstallPackages } from "@/core/packages/useInstallPackage";
import { isWasm } from "@/core/wasm/utils";
import { useAsyncData } from "@/hooks/useAsyncData";
import { ErrorBanner } from "@/plugins/impl/common/error-banner";
@@ -41,84 +45,6 @@ import { packagesToInstallAtom } from "./packages-state";
type ViewMode = "tree" | "list";
-const showAddPackageToast = (packageName: string, error?: string | null) => {
- if (error) {
- toast({
- title: "Failed to add package",
- description: error,
- variant: "danger",
- });
- } else {
- toast({
- title: "Package added",
- description: (
- <div>
- <div>
- The package <Kbd className="inline">{packageName}</Kbd> and its
- dependencies has been added to your environment.
- </div>
- <div className="text-xs text-muted-foreground mt-1">
- Some Python packages may require a kernel restart to see changes.
- </div>
- </div>
- ),
- });
- }
-};
-
-const showUpgradePackageToast = (
- packageName: string,
- error?: string | null,
-) => {
- if (error) {
- toast({
- title: "Failed to upgrade package",
- description: error,
- variant: "danger",
- });
- } else {
- toast({
- title: "Package upgraded",
- description: (
- <div>
- <div>
- The package <Kbd className="inline">{packageName}</Kbd> has been
- upgraded.
- </div>
- <div className="text-xs text-muted-foreground mt-1">
- Some Python packages may require a kernel restart to see changes.
- </div>
- </div>
- ),
- });
- }
-};
-
-const showRemovePackageToast = (packageName: string, error?: string | null) => {
- if (error) {
- toast({
- title: "Failed to remove package",
- description: error,
- variant: "danger",
- });
- } else {
- toast({
- title: "Package removed",
- description: (
- <div>
- <div>
- The package <Kbd className="inline">{packageName}</Kbd> has been
- removed from your environment.
- </div>
- <div className="text-xs text-muted-foreground mt-1">
- Some Python packages may require a kernel restart to see changes.
- </div>
- </div>
- ),
- });
- }
-};
-
const PackageActionButton: React.FC<{
onClick: () => void;
loading: boolean;
@@ -245,7 +171,6 @@ const InstallPackageForm: React.FC<{
onSuccess: () => void;
}> = ({ onSuccess, packageManager }) => {
const [input, setInput] = React.useState("");
- const [loading, setLoading] = React.useState(false);
const { handleClick: openSettings } = useOpenSettingsToTab();
// Get the packages to install from the atom
@@ -261,27 +186,17 @@ const InstallPackageForm: React.FC<{
}
}, [packagesToInstall, setPackagesToInstall]);
- const handleAddPackage = async () => {
- try {
- setLoading(true);
- const packages = input.split(",").map((p) => p.trim());
- for (const [idx, packageName] of packages.entries()) {
- const response = await addPackage({ package: packageName });
- if (response.success) {
- showAddPackageToast(packageName);
- } else {
- showAddPackageToast(packageName, response.error);
- }
- // Wait 1s if there are more packages to install
- if (idx < packages.length - 1) {
- await new Promise((resolve) => setTimeout(resolve, 1000));
- }
- }
- onSuccess();
- } finally {
- setInput("");
- setLoading(false);
- }
+ const { loading, handleInstallPackages } = useInstallPackages();
+ const onSuccessInstallPackages = () => {
+ onSuccess();
+ setInput("");
+ };
+
+ const installPackages = () => {
+ handleInstallPackages(
+ input.split(",").map((p) => p.trim()),
+ onSuccessInstallPackages,
+ );
};
return (
@@ -309,7 +224,7 @@ const InstallPackageForm: React.FC<{
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
- handleAddPackage();
+ installPackages();
}
}}
onChange={(e) => setInput(e.target.value)}
@@ -376,7 +291,7 @@ const InstallPackageForm: React.FC<{
input && "bg-accent text-accent-foreground",
"disabled:cursor-not-allowed disabled:opacity-50",
)}
- onClick={handleAddPackage}
+ onClick={installPackages}
disabled={!input}
>
Add
diff --git a/frontend/src/core/packages/toast-components.tsx b/frontend/src/core/packages/toast-components.tsx
new file mode 100644
index 0000000000..2979edc4c5
--- /dev/null
+++ b/frontend/src/core/packages/toast-components.tsx
@@ -0,0 +1,88 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { Kbd } from "@/components/ui/kbd";
+import { toast } from "@/components/ui/use-toast";
+
+export const showAddPackageToast = (
+ packageName: string,
+ error?: string | null,
+) => {
+ if (error) {
+ toast({
+ title: "Failed to add package",
+ description: error,
+ variant: "danger",
+ });
+ } else {
+ toast({
+ title: "Package added",
+ description: (
+ <div>
+ <div>
+ The package <Kbd className="inline">{packageName}</Kbd> and its
+ dependencies has been added to your environment.
+ </div>
+ <div className="text-xs text-muted-foreground mt-1">
+ Some Python packages may require a kernel restart to see changes.
+ </div>
+ </div>
+ ),
+ });
+ }
+};
+
+export const showUpgradePackageToast = (
+ packageName: string,
+ error?: string | null,
+) => {
+ if (error) {
+ toast({
+ title: "Failed to upgrade package",
+ description: error,
+ variant: "danger",
+ });
+ } else {
+ toast({
+ title: "Package upgraded",
+ description: (
+ <div>
+ <div>
+ The package <Kbd className="inline">{packageName}</Kbd> has been
+ upgraded.
+ </div>
+ <div className="text-xs text-muted-foreground mt-1">
+ Some Python packages may require a kernel restart to see changes.
+ </div>
+ </div>
+ ),
+ });
+ }
+};
+
+export const showRemovePackageToast = (
+ packageName: string,
+ error?: string | null,
+) => {
+ if (error) {
+ toast({
+ title: "Failed to remove package",
+ description: error,
+ variant: "danger",
+ });
+ } else {
+ toast({
+ title: "Package removed",
+ description: (
+ <div>
+ <div>
+ The package <Kbd className="inline">{packageName}</Kbd> has been
+ removed from your environment.
+ </div>
+ <div className="text-xs text-muted-foreground mt-1">
+ Some Python packages may require a kernel restart to see changes.
+ </div>
+ </div>
+ ),
+ });
+ }
+};
diff --git a/frontend/src/core/packages/useInstallPackage.ts b/frontend/src/core/packages/useInstallPackage.ts
new file mode 100644
index 0000000000..fed9abc696
--- /dev/null
+++ b/frontend/src/core/packages/useInstallPackage.ts
@@ -0,0 +1,45 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { useState } from "react";
+import { addPackage } from "@/core/network/requests";
+import { Logger } from "@/utils/Logger";
+import { showAddPackageToast } from "./toast-components";
+
+export function useInstallPackages(): {
+ handleInstallPackages: (
+ packages: string[],
+ onSuccess?: () => void,
+ ) => Promise<void>;
+ loading: boolean;
+} {
+ const [loading, setLoading] = useState(false);
+
+ const handleInstallPackages = async (
+ packages: string[],
+ onSuccess?: () => void,
+ ) => {
+ setLoading(true);
+
+ try {
+ for (const [idx, packageName] of packages.entries()) {
+ const response = await addPackage({ package: packageName });
+ if (response.success) {
+ showAddPackageToast(packageName);
+ } else {
+ showAddPackageToast(packageName, response.error);
+ }
+ // Wait 1s if there are more packages to install
+ if (idx < packages.length - 1) {
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ }
+ }
+ onSuccess?.();
+ } catch (error) {
+ Logger.error(error);
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ return { loading, handleInstallPackages };
+}