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.

16.9 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { once } from "@/utils/once";
import { getRuntimeManager } from "../runtime/config";
import { API, createClientWithRuntimeManager } from "./api";
import {
  getDefaultExportFilename,
  getDefaultMarkdownExportFilename,
} from "./export-filename";
import {
  waitForConnectionOpen,
  waitForConnectionOpenIfNotebook,
} from "./connection";
import { withDevAssetUrl } from "./export-asset-url";
import type { EditRequests, RunRequests } from "./types";

/**
 * Options for POSTing FormData via openapi-fetch. openapi-fetch types
 * request bodies from the JSON schema, so we bypass the body type and
 * override the serializer to pass the FormData through unchanged; the
 * browser then sets the multipart Content-Type with boundary.
 */
function multipartInit(formData: FormData) {
  return {
    body: formData as never,
    bodySerializer: (body: unknown) => body as never,
  };
}

const { handleExportResponse, handleResponse, handleResponseReturnNull } = API;

export function createNetworkRequests(): EditRequests & RunRequests {
  const getClient = once(() => {
    const runtimeManager = getRuntimeManager();
    return createClientWithRuntimeManager(runtimeManager);
  });

  const getHeaders = () => {
    const runtimeManager = getRuntimeManager();
    return runtimeManager.sessionHeaders();
  };

  const getParams = () => {
    return {
      header: getHeaders(),
    };
  };

  return {
    sendComponentValues: (request) => {
      return getClient()
        .POST("/api/kernel/set_ui_element_value", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendModelValue: (request) => {
      return getClient()
        .POST("/api/kernel/set_model_value", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendRestart: () => {
      return getClient()
        .POST("/api/kernel/restart_session", {
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendRename: (request) => {
      return getClient()
        .POST("/api/kernel/rename", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendCopy: (request) => {
      return getClient()
        .POST("/api/kernel/copy", {
          body: request,
          parseAs: "text",
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendFormat: (request) => {
      return getClient()
        .POST("/api/kernel/format", {
          body: request,
        })
        .then(handleResponse);
    },
    sendInterrupt: () => {
      return getClient()
        .POST("/api/kernel/interrupt", {
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendShutdown: () => {
      return getClient()
        .POST("/api/kernel/shutdown")
        .then(handleResponseReturnNull);
    },
    sendRun: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/kernel/run", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendRunScratchpad: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/kernel/scratchpad/run", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendInstantiate: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/kernel/instantiate", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendCodeCompletionRequest: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/kernel/code_autocomplete", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    saveUserConfig: (request) => {
      return getClient()
        .POST("/api/kernel/save_user_config", {
          body: request,
        })
        .then(handleResponseReturnNull);
    },
    saveLayout: (request) => {
      return getClient()
        .POST("/api/kernel/save_layout", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    saveAppConfig: (request) => {
      return getClient()
        .POST("/api/kernel/save_app_config", {
          body: request,
          parseAs: "text",
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendFunctionRequest: (request) => {
      return getClient()
        .POST("/api/kernel/function_call", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendStdin: (request) => {
      return getClient()
        .POST("/api/kernel/stdin", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendInstallMissingPackages: (request) => {
      return getClient()
        .POST("/api/kernel/install_missing_packages", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    readCode: async () => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/kernel/read_code", {
          params: getParams(),
        })
        .then(handleResponse);
    },
    readSnippets: async () => {
      await waitForConnectionOpen();
      return getClient()
        .GET("/api/documentation/snippets", {
          params: getParams(),
        })
        .then(handleResponse);
    },
    previewDatasetColumn: (request) => {
      return getClient()
        .POST("/api/datasources/preview_column", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    previewSQLTable: (request) => {
      return getClient()
        .POST("/api/datasources/preview_sql_table", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    previewSQLTableList: (request) => {
      return getClient()
        .POST("/api/datasources/preview_sql_table_list", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    previewSQLSchemaList: (request) => {
      return getClient()
        .POST("/api/datasources/preview_sql_schema_list", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    previewDataSourceConnection: (request) => {
      return getClient()
        .POST("/api/datasources/preview_datasource_connection", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    discoverDataSources: (request) => {
      return getClient()
        .POST("/api/datasources/discover", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    validateSQL: (request) => {
      return getClient()
        .POST("/api/sql/validate", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    openFile: async (request) => {
      await waitForConnectionOpen();
      await getClient()
        .POST("/api/files/open", {
          body: request,
        })
        .then(handleResponseReturnNull);
      return null;
    },
    getUsageStats: async () => {
      await waitForConnectionOpen();
      return getClient().GET("/api/usage").then(handleResponse);
    },
    getEnvironmentInfo: () => {
      return getClient().GET("/api/environment").then(handleResponse);
    },
    sendPdb: (request) => {
      return getClient()
        .POST("/api/kernel/pdb/pm", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    sendSetBreakpoints: (request) => {
      return getClient()
        .POST("/api/kernel/pdb/breakpoints", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    getFileRoots: async () => {
      await waitForConnectionOpen();
      return getClient().GET("/api/files/roots").then(handleResponse);
    },
    sendListFiles: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/files/list_files", {
          body: request,
        })
        .then(handleResponse);
    },
    sendSearchFiles: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/files/search", {
          body: request,
        })
        .then(handleResponse);
    },
    sendCreateFileOrFolder: async (request) => {
      await waitForConnectionOpen();
      const formData = new FormData();
      formData.append("path", request.path);
      formData.append("type", request.type);
      formData.append("name", request.name);
      if (request.file) {
        formData.append("file", request.file, request.name);
      }
      return getClient()
        .POST("/api/files/create", multipartInit(formData))
        .then(handleResponse);
    },
    sendDeleteFileOrFolder: async (request) => {
      return getClient()
        .POST("/api/files/delete", {
          body: request,
        })
        .then(handleResponse);
    },
    sendCopyFileOrFolder: async (request) => {
      return getClient()
        .POST("/api/files/copy", {
          body: request,
        })
        .then(handleResponse);
    },
    sendRenameFileOrFolder: async (request) => {
      return getClient()
        .POST("/api/files/move", {
          body: request,
        })
        .then(handleResponse);
    },
    sendUpdateFile: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/files/update", {
          body: request,
        })
        .then(handleResponse);
    },
    sendFileDetails: async (request) => {
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/files/file_details", {
          body: request,
        })
        .then(handleResponse);
    },
    openTutorial: (request) => {
      return getClient()
        .POST("/api/home/tutorial/open", {
          body: request,
        })
        .then(handleResponse);
    },
    getRecentFiles: () => {
      return getClient().POST("/api/home/recent_files").then(handleResponse);
    },
    getWorkspaceFiles: (request) => {
      return getClient()
        .POST("/api/home/workspace_files", {
          body: request,
        })
        .then(handleResponse);
    },
    getRunningNotebooks: () => {
      return getClient()
        .POST("/api/home/running_notebooks")
        .then(handleResponse);
    },
    shutdownSession: (request) => {
      return getClient()
        .POST("/api/home/shutdown_session", {
          body: request,
        })
        .then(handleResponse);
    },
    getExportAvailability: () => {
      return getClient().GET("/api/export/availability").then(handleResponse);
    },
    installExportRequirements: (request) => {
      return getClient()
        .POST("/api/export/requirements/install", {
          body: request,
          params: getParams(),
        })
        .then(handleResponse);
    },
    exportAsHTML: async (request) => {
      return getClient()
        .POST("/api/export/html", {
          body: withDevAssetUrl(request),
          parseAs: "text",
          params: getParams(),
        })
        .then((response) =>
          handleExportResponse(response, {
            defaultFilename: getDefaultExportFilename("html"),
          }),
        );
    },
    exportAsMarkdown: async (request) => {
      return getClient()
        .POST("/api/export/markdown", {
          body: request,
          parseAs: "text",
          params: getParams(),
        })
        .then((response) =>
          handleExportResponse(response, {
            defaultFilename: getDefaultMarkdownExportFilename(request.flavor),
          }),
        );
    },
    exportAsScript: async (request) => {
      return getClient()
        .POST("/api/export/script", {
          body: request,
          parseAs: "text",
          params: getParams(),
        })
        .then((response) =>
          handleExportResponse(response, {
            defaultFilename: getDefaultExportFilename("script.py"),
          }),
        );
    },
    exportAsIPYNB: async (request) => {
      return getClient()
        .POST("/api/export/ipynb", {
          body: request,
          parseAs: "text",
          params: getParams(),
        })
        .then((response) =>
          handleExportResponse(response, {
            defaultFilename: getDefaultExportFilename("ipynb"),
          }),
        );
    },
    exportAsPDF: async (request) => {
      return getClient()
        .POST("/api/export/pdf", {
          body: request,
          parseAs: "blob",
          params: getParams(),
        })
        .then((response) =>
          handleExportResponse(response, {
            defaultFilename: getDefaultExportFilename("pdf"),
          }),
        );
    },
    autoExportAsHTML: async (request) => {
      return getClient()
        .POST("/api/export/auto_export/html", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    autoExportAsMarkdown: async (request) => {
      return getClient()
        .POST("/api/export/auto_export/markdown", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    autoExportAsIPYNB: async (request) => {
      return getClient()
        .POST("/api/export/auto_export/ipynb", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    updateCellOutputs: async (request) => {
      return getClient()
        .POST("/api/export/update_cell_outputs", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    getSandbox: (request) =>
      getClient()
        .POST("/api/packages/sandbox", { body: request, params: getParams() })
        .then(handleResponse),
    updateManifest: (request) =>
      getClient()
        .POST("/api/packages/manifest", { body: request, params: getParams() })
        .then(handleResponse),
    syncSandbox: (request) =>
      getClient()
        .POST("/api/packages/sync", { body: request, params: getParams() })
        .then(handleResponse),
    addPackage: (request) => {
      return getClient()
        .POST("/api/packages/add", {
          body: request,
        })
        .then(handleResponse);
    },
    removePackage: (request) => {
      return getClient()
        .POST("/api/packages/remove", {
          body: request,
        })
        .then(handleResponse);
    },
    getPackageList: async () => {
      // If the sidebar is already open, it may try to load before the session has been initialized
      await waitForConnectionOpenIfNotebook();
      return getClient().GET("/api/packages/list").then(handleResponse);
    },
    getDependencyTree: async () => {
      // If the sidebar is already open, it may try to load before the session has been initialized
      await waitForConnectionOpenIfNotebook();
      return getClient().GET("/api/packages/tree").then(handleResponse);
    },
    listSecretKeys: async (request) => {
      // If the sidebar is already open, it may try to load before the session has been initialized
      await waitForConnectionOpen();
      return getClient()
        .POST("/api/secrets/keys", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    writeSecret: async (request) => {
      return getClient()
        .POST("/api/secrets/create", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    invokeAiTool: async (request) => {
      return getClient()
        .POST("/api/ai/invoke_tool", {
          body: request,
          params: getParams(),
        })
        .then(handleResponse);
    },
    clearCache: async () => {
      return getClient()
        .POST("/api/cache/clear", {
          body: {},
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    getCacheInfo: async () => {
      return getClient()
        .POST("/api/cache/info", {
          body: {},
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    listStorageEntries: (request) => {
      return getClient()
        .POST("/api/storage/list_entries", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
    downloadStorage: (request) => {
      return getClient()
        .POST("/api/storage/download", {
          body: request,
          params: getParams(),
        })
        .then(handleResponseReturnNull);
    },
  };
}