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.

misn: allow the FE to recieve banner messages from the kernel (#585)

Myles Scolnicka4dc822b75de
diff --git a/frontend/src/components/editor/notebook-banner.tsx b/frontend/src/components/editor/notebook-banner.tsx
new file mode 100644
index 0000000000..cb02a3b343
--- /dev/null
+++ b/frontend/src/components/editor/notebook-banner.tsx
@@ -0,0 +1,31 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+import { bannerAtom } from "@/core/errors/state";
+import { renderHTML } from "@/plugins/core/RenderHTML";
+import { Banner } from "@/plugins/impl/common/error-banner";
+import { useAtom } from "jotai";
+import { AlertCircleIcon, XIcon } from "lucide-react";
+import React from "react";
+import { Button } from "../ui/button";
+
+export const NotebookBanner: React.FC = (props) => {
+ const [banner, setBanner] = useAtom(bannerAtom);
+
+ if (!banner) {
+ return null;
+ }
+
+ return (
+ <Banner kind={banner.variant} className="mb-10 flex flex-col rounded p-3">
+ <div className="flex justify-between">
+ <span className="font-bold text-lg flex items-center mb-2">
+ <AlertCircleIcon className="w-5 h-5 inline-block mr-2" />
+ {banner.title}
+ </span>
+ <Button variant="text" size="icon" onClick={() => setBanner(undefined)}>
+ <XIcon className="w-5 h-5" />
+ </Button>
+ </div>
+ <span>{renderHTML({ html: banner.description })}</span>
+ </Banner>
+ );
+};
diff --git a/frontend/src/components/editor/renderers/CellArray.tsx b/frontend/src/components/editor/renderers/CellArray.tsx
index fbe65ec1b7..3d9b68dffa 100644
--- a/frontend/src/components/editor/renderers/CellArray.tsx
+++ b/frontend/src/components/editor/renderers/CellArray.tsx
@@ -21,6 +21,7 @@ import { VerticalLayoutWrapper } from "./vertical-layout/vertical-layout-wrapper
import { useDelayVisibility } from "./vertical-layout/useDelayVisibility";
import { useChromeActions } from "../chrome/state";
import { Functions } from "@/utils/functions";
+import { NotebookBanner } from "../notebook-banner";
interface CellArrayProps {
notebook: NotebookState;
@@ -90,6 +91,7 @@ export const CellArray: React.FC<CellArrayProps> = ({
return (
<VerticalLayoutWrapper invisible={invisible} appConfig={appConfig}>
+ <NotebookBanner />
{cells.map((cell) => (
<Cell
key={cell.id.toString()}
diff --git a/frontend/src/core/errors/state.ts b/frontend/src/core/errors/state.ts
new file mode 100644
index 0000000000..c355ab3389
--- /dev/null
+++ b/frontend/src/core/errors/state.ts
@@ -0,0 +1,11 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+
+import { atom } from "jotai";
+
+interface BannerState {
+ title: string;
+ description: string;
+ variant?: "danger";
+}
+
+export const bannerAtom = atom<BannerState | undefined>(undefined);
diff --git a/frontend/src/core/kernel/messages.ts b/frontend/src/core/kernel/messages.ts
index 9782c056e5..d71122ae51 100644
--- a/frontend/src/core/kernel/messages.ts
+++ b/frontend/src/core/kernel/messages.ts
@@ -245,4 +245,12 @@ export type OperationMessage =
description: string;
variant?: "danger";
};
+ }
+ | {
+ op: "banner";
+ data: {
+ title: string;
+ description: string;
+ variant?: "danger";
+ };
};
diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsx
index 1e813d58ba..28736fa715 100644
--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx
+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx
@@ -26,6 +26,7 @@ import { isStaticNotebook } from "../static/static-state";
import { useRef } from "react";
import { jsonParseWithSpecialChar } from "@/utils/json/json-parser";
import { VirtualFileTracker } from "../static/virtual-file-tracker";
+import { bannerAtom } from "../errors/state";
/**
* WebSocket that connects to the Marimo kernel and handles incoming messages.
@@ -45,6 +46,7 @@ export function useMarimoWebSocket(opts: {
const setLayoutView = useSetAtom(layoutViewAtom);
const setLayoutData = useSetAtom(layoutDataAtom);
const [connStatus, setConnStatus] = useAtom(connectionAtom);
+ const setBanner = useSetAtom(bannerAtom);
const handleMessage = (e: MessageEvent<string>) => {
const msg = jsonParseWithSpecialChar<OperationMessage>(e.data);
@@ -159,6 +161,9 @@ export function useMarimoWebSocket(opts: {
variant: msg.data.variant,
});
return;
+ case "banner":
+ setBanner(msg.data);
+ return;
default:
logNever(msg);
}