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: more waiting for connection logic (#5316)

Myles Scolnick37b25b15d020
diff --git a/frontend/src/components/editor/controls/Controls.tsx b/frontend/src/components/editor/controls/Controls.tsx
index 23faf2677f..ede392919a 100644
--- a/frontend/src/components/editor/controls/Controls.tsx
+++ b/frontend/src/components/editor/controls/Controls.tsx
@@ -15,6 +15,7 @@ import { ShutdownButton } from "@/components/editor/controls/shutdown-button";
import { Button } from "@/components/editor/inputs/Inputs";
import { FindReplace } from "@/components/find-replace/find-replace";
import type { AppConfig } from "@/core/config/config-schema";
+import { isConnectedAtom } from "@/core/network/connection";
import { SaveComponent } from "@/core/saving/save-component";
import { cn } from "@/utils/cn";
import { Functions } from "@/utils/functions";
@@ -137,6 +138,8 @@ const RunControlButton = ({
needsRun: boolean;
onRun: () => void;
}) => {
+ const isConnected = useAtomValue(isConnectedAtom);
+
if (needsRun) {
return (
<Tooltip content={renderShortcut("global.runStale")}>
@@ -161,6 +164,7 @@ const RunControlButton = ({
color="disabled"
size="medium"
shape="circle"
+ disabled={!isConnected}
>
<PlayIcon strokeWidth={1.5} size={16} />
</Button>
diff --git a/frontend/src/components/editor/renderers/CellArray.tsx b/frontend/src/components/editor/renderers/CellArray.tsx
index b19e56a9cd..b2329e18c8 100644
--- a/frontend/src/components/editor/renderers/CellArray.tsx
+++ b/frontend/src/components/editor/renderers/CellArray.tsx
@@ -23,6 +23,7 @@ import type { CellId } from "@/core/cells/ids";
import { MarkdownLanguageAdapter } from "@/core/codemirror/language/languages/markdown";
import { SQLLanguageAdapter } from "@/core/codemirror/language/languages/sql";
import { aiEnabledAtom, autoInstantiateAtom } from "@/core/config/config";
+import { isConnectedAtom } from "@/core/network/connection";
import { useBoolean } from "@/hooks/useBoolean";
import { cn } from "@/utils/cn";
import { Functions } from "@/utils/functions";
@@ -295,6 +296,7 @@ const AddCellButtons: React.FC<{
const autoInstantiate = useAtomValue(autoInstantiateAtom);
const [isAiButtonOpen, isAiButtonOpenActions] = useBoolean(false);
const aiEnabled = useAtomValue(aiEnabledAtom);
+ const isConnected = useAtomValue(isConnectedAtom);
const buttonClass = cn(
"mb-0 rounded-none sm:px-4 md:px-5 lg:px-8 tracking-wide no-wrap whitespace-nowrap",
@@ -312,6 +314,7 @@ const AddCellButtons: React.FC<{
className={buttonClass}
variant="text"
size="sm"
+ disabled={!isConnected}
onClick={() =>
createNewCell({
cellId: { type: "__end__", columnId },
@@ -326,6 +329,7 @@ const AddCellButtons: React.FC<{
className={buttonClass}
variant="text"
size="sm"
+ disabled={!isConnected}
onClick={() => {
maybeAddMarimoImport(autoInstantiate, createNewCell);
@@ -343,6 +347,7 @@ const AddCellButtons: React.FC<{
className={buttonClass}
variant="text"
size="sm"
+ disabled={!isConnected}
onClick={() => {
maybeAddMarimoImport(autoInstantiate, createNewCell);
@@ -367,7 +372,7 @@ const AddCellButtons: React.FC<{
className={buttonClass}
variant="text"
size="sm"
- disabled={!aiEnabled}
+ disabled={!aiEnabled || !isConnected}
onClick={isAiButtonOpenActions.toggle}
>
<SparklesIcon className="mr-2 size-4 flex-shrink-0" />
diff --git a/frontend/src/components/scratchpad/scratchpad.tsx b/frontend/src/components/scratchpad/scratchpad.tsx
index b503dc264e..861c366ccb 100644
--- a/frontend/src/components/scratchpad/scratchpad.tsx
+++ b/frontend/src/components/scratchpad/scratchpad.tsx
@@ -67,7 +67,7 @@ export const ScratchPad: React.FC = () => {
const history = useAtomValue(scratchpadHistoryAtom);
const handleRun = useEvent(() => {
- sendRunScratchpad({ code });
+ void sendRunScratchpad({ code });
addToHistory(code);
});
@@ -88,7 +88,7 @@ export const ScratchPad: React.FC = () => {
code: "",
formattingChange: false,
});
- sendRunScratchpad({ code: "" });
+ void sendRunScratchpad({ code: "" });
const ev = ref.current;
if (ev) {
ev.dispatch({
diff --git a/frontend/src/core/network/connection.ts b/frontend/src/core/network/connection.ts
index 283fdd8be0..ea5255ff19 100644
--- a/frontend/src/core/network/connection.ts
+++ b/frontend/src/core/network/connection.ts
@@ -21,3 +21,8 @@ export const isConnectingAtom = atom((get) => {
const connection = get(connectionAtom);
return connection.state === WebSocketState.CONNECTING;
});
+
+export const isConnectedAtom = atom((get) => {
+ const connection = get(connectionAtom);
+ return connection.state === WebSocketState.OPEN;
+});
diff --git a/frontend/src/core/network/requests-network.ts b/frontend/src/core/network/requests-network.ts
index c3d5572b3d..fc08d7a25a 100644
--- a/frontend/src/core/network/requests-network.ts
+++ b/frontend/src/core/network/requests-network.ts
@@ -82,14 +82,16 @@ export function createNetworkRequests(): EditRequests & RunRequests {
.POST("/api/kernel/shutdown")
.then(handleResponseReturnNull);
},
- sendRun: (request) => {
+ sendRun: async (request) => {
+ await waitForConnectionOpen();
return getClient()
.POST("/api/kernel/run", {
body: request,
})
.then(handleResponseReturnNull);
},
- sendRunScratchpad: (request) => {
+ sendRunScratchpad: async (request) => {
+ await waitForConnectionOpen();
return getClient()
.POST("/api/kernel/scratchpad/run", {
body: request,