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: multi-select filter fn (#460)
Myles Scolnicka0a6e7df8e2e
diff --git a/frontend/src/core/kernel/RuntimeState.ts b/frontend/src/core/kernel/RuntimeState.tsindex 8e81914c19..4a4679a7e1 100644--- a/frontend/src/core/kernel/RuntimeState.ts+++ b/frontend/src/core/kernel/RuntimeState.ts@@ -7,6 +7,7 @@ import {import { UI_ELEMENT_REGISTRY, UIElementRegistry } from "../dom/uiregistry";import { isStaticNotebook } from "../static/static-state";import { RunRequests } from "../network/types";+import { repl } from "@/utils/repl";/*** Manager to track running cells.@@ -40,6 +41,7 @@ export class RuntimeState {) {this.runningCount = 0;this.componentsToUpdate = new Set();+ repl(RuntimeState.INSTANCE, "RuntimeState");}/**@@ -86,7 +88,7 @@ export class RuntimeState {})).filter((update) => update.value !== undefined)).catch(() => {- // This happens if the run was failed ot register (403, network error, etc.)+ // This happens if the run was failed ot register (401, 403, network error, etc.)// If the run fails, restore the components to update and finish the run// A run may fail if the kernel is restarted or the notebook is closed,// but if we don't restore registerRunEnd() will never be able to flush updates.diff --git a/frontend/src/plugins/impl/MultiselectPlugin.tsx b/frontend/src/plugins/impl/MultiselectPlugin.tsxindex 7511d2d100..a65b4da294 100644--- a/frontend/src/plugins/impl/MultiselectPlugin.tsx+++ b/frontend/src/plugins/impl/MultiselectPlugin.tsx@@ -58,6 +58,7 @@ const Multiselect = (props: MultiselectProps): JSX.Element => {displayValue={(option) => option}placeholder="Select..."multiple={true}+ filterFn={multiselectFilterFn}className={cn({"w-full": props.fullWidth,})}@@ -73,3 +74,28 @@ const Multiselect = (props: MultiselectProps): JSX.Element => {</Labeled>);};++/**+ * We override the default filter function which focuses on sorting by relevance with a fuzzy-match,+ * instead of filtering out.+ * The default filter function is `command-score`.+ *+ * Our filter function only matches if all words in the value are present in the option.+ * This is more strict than the default, but more lenient than an exact match.+ *+ * Examples:+ * - "foo bar" matches "foo bar"+ * - "bar foo" matches "foo bar"+ * - "foob" does not matches "foo bar"+ */+function multiselectFilterFn(option: string, value: string): number {+ const words = value.split(/\s+/);+ const match = words.every((word) =>+ option.toLowerCase().includes(word.toLowerCase())+ );+ return match ? 1 : 0;+}++export const exportedForTesting = {+ multiselectFilterFn,+};diff --git a/frontend/src/plugins/impl/__tests__/MultiSelectPlugin.test.ts b/frontend/src/plugins/impl/__tests__/MultiSelectPlugin.test.tsnew file mode 100644index 0000000000..90a861c53d--- /dev/null+++ b/frontend/src/plugins/impl/__tests__/MultiSelectPlugin.test.ts@@ -0,0 +1,42 @@+/* Copyright 2023 Marimo. All rights reserved. */+import { expect, it } from "vitest";+import { exportedForTesting } from "../MultiselectPlugin";++const filterFn = exportedForTesting.multiselectFilterFn;+function filterOptions(filter: string, items: string[]) {+ return items.filter((option) => filterFn(option, filter));+}++it("can filter to relevant words", () => {+ const options = ["a", "b", "c", "foo", "bar", "foo bar", "foobar"];++ expect(filterOptions("a", options)).toEqual([+ "a",+ "bar",+ "foo bar",+ "foobar",+ ]);++ expect(filterOptions("b", options)).toEqual([+ "b",+ "bar",+ "foo bar",+ "foobar",+ ]);++ expect(filterOptions("f", options)).toEqual(["foo", "foo bar", "foobar"]);++ expect(filterOptions("foo", options)).toEqual(["foo", "foo bar", "foobar"]);++ expect(filterOptions("foo ", options)).toEqual(["foo", "foo bar", "foobar"]);++ expect(filterOptions("foo b", options)).toEqual(["foo bar", "foobar"]);++ expect(filterOptions("foo ba", options)).toEqual(["foo bar", "foobar"]);++ expect(filterOptions("foo bar", options)).toEqual(["foo bar", "foobar"]);++ expect(filterOptions("foob", options)).toEqual(["foobar"]);++ expect(filterOptions("foob foo", options)).toEqual(["foobar"]);+});diff --git a/frontend/src/utils/repl.ts b/frontend/src/utils/repl.tsindex 8b6039e5ee..bc8cd062a4 100644--- a/frontend/src/utils/repl.ts+++ b/frontend/src/utils/repl.ts@@ -16,7 +16,7 @@ export function repl(item: unknown, name: string) {}const fullName = `__marimo__${name}`;- if (window[fullName]) {+ if (window[fullName] && process.env.NODE_ENV !== "test") {Logger.warn(`Overwriting existing debug object ${fullName}`);}window[fullName] = item;