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: show engine's tables in the datasources panel (#3665)

Myles Scolnick4c6c6d1c89b6
diff --git a/frontend/src/components/databases/display.tsx b/frontend/src/components/databases/display.tsx
new file mode 100644
index 0000000000..329a9870a6
--- /dev/null
+++ b/frontend/src/components/databases/display.tsx
@@ -0,0 +1,52 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+export function dbDisplayName(name: string) {
+ switch (name) {
+ case "duckdb":
+ return "DuckDB";
+ case "sqlite":
+ return "SQLite";
+ case "postgres":
+ case "postgresql":
+ return "PostgreSQL";
+ case "mysql":
+ return "MySQL";
+ case "mariadb":
+ return "MariaDB";
+ case "mssql":
+ return "Microsoft SQL Server";
+ case "oracle":
+ return "Oracle";
+ case "redshift":
+ return "Amazon Redshift";
+ case "snowflake":
+ return "Snowflake";
+ case "bigquery":
+ return "Google BigQuery";
+ case "clickhouse":
+ return "ClickHouse";
+ case "databricks":
+ return "Databricks";
+ case "db2":
+ return "IBM Db2";
+ case "hive":
+ return "Apache Hive";
+ case "impala":
+ return "Apache Impala";
+ case "presto":
+ return "Presto";
+ case "trino":
+ return "Trino";
+ case "cockroachdb":
+ return "CockroachDB";
+ case "timescaledb":
+ return "TimescaleDB";
+ case "singlestore":
+ return "SingleStore";
+ case "cassandra":
+ return "Apache Cassandra";
+ case "mongodb":
+ return "MongoDB";
+ default:
+ return name;
+ }
+}
diff --git a/frontend/src/components/databases/engine-variable.tsx b/frontend/src/components/databases/engine-variable.tsx
new file mode 100644
index 0000000000..df2195a9f0
--- /dev/null
+++ b/frontend/src/components/databases/engine-variable.tsx
@@ -0,0 +1,51 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { getCellEditorView } from "@/core/cells/cells";
+import type { CellId } from "@/core/cells/ids";
+import { goToVariableDefinition } from "@/core/codemirror/go-to-definition/commands";
+import { store } from "@/core/state/jotai";
+import { variablesAtom } from "@/core/variables/state";
+import type { VariableName } from "@/core/variables/types";
+import { cn } from "@/utils/cn";
+
+interface Props {
+ variableName: VariableName;
+ className?: string;
+}
+
+export const EngineVariable: React.FC<Props> = ({
+ variableName,
+ className,
+}) => {
+ const onClick = () => {
+ const cellId = findCellId(variableName);
+ if (!cellId) {
+ return;
+ }
+ const editorView = getCellEditorView(cellId);
+ if (editorView) {
+ goToVariableDefinition(editorView, variableName);
+ }
+ };
+
+ return (
+ <button
+ type="button"
+ onClick={onClick}
+ className={cn(
+ "text-link opacity-80 hover:opacity-100 hover:underline",
+ className,
+ )}
+ >
+ {variableName}
+ </button>
+ );
+};
+
+function findCellId(variableName: VariableName): CellId | undefined {
+ const variables = store.get(variablesAtom);
+ const variable = variables[variableName];
+ if (!variable) {
+ return undefined;
+ }
+ return variable.declaredBy[0];
+}
diff --git a/frontend/src/components/databases/icon.tsx b/frontend/src/components/databases/icon.tsx
new file mode 100644
index 0000000000..d83fecb5ee
--- /dev/null
+++ b/frontend/src/components/databases/icon.tsx
@@ -0,0 +1,49 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import type { FC } from "react";
+import SQLiteIcon from "./icons/sqlite.svg";
+import DuckDBIcon from "./icons/duckdb.svg";
+import PostgresQLIcon from "./icons/postgresql.svg";
+import MySQLIcon from "./icons/mysql.svg";
+import SnowflakeIcon from "./icons/snowflake.svg";
+import DatabricksIcon from "./icons/databricks.svg";
+import ClickhouseIcon from "./icons/clickhouse.svg";
+
+import { cn } from "@/utils/cn";
+
+/**
+ * Icons are from https://simpleicons.org/
+ */
+
+interface DatabaseLogoProps {
+ name: string;
+ className?: string;
+}
+
+export const DatabaseLogo: FC<DatabaseLogoProps> = ({ name, className }) => {
+ const lowerName = name.toLowerCase();
+
+ const URLS: Record<string, string | undefined> = {
+ sqlite: SQLiteIcon,
+ duckdb: DuckDBIcon,
+ postgres: PostgresQLIcon,
+ postgresql: PostgresQLIcon,
+ mysql: MySQLIcon,
+ snowflake: SnowflakeIcon,
+ databricks: DatabricksIcon,
+ clickhouse: ClickhouseIcon,
+ };
+
+ const url = URLS[lowerName];
+
+ if (!url) {
+ return null;
+ }
+
+ return (
+ <img
+ src={url}
+ alt={name}
+ className={cn(className, "invert-[.5] dark:invert-[.7]")}
+ />
+ );
+};
diff --git a/frontend/src/components/databases/icons/clickhouse.svg b/frontend/src/components/databases/icons/clickhouse.svg
new file mode 100644
index 0000000000..689aca3791
--- /dev/null
+++ b/frontend/src/components/databases/icons/clickhouse.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>ClickHouse</title><path d="M21.333 10H24v4h-2.667ZM16 1.335h2.667v21.33H16Zm-5.333 0h2.666v21.33h-2.666ZM0 22.665V1.335h2.667v21.33zm5.333-21.33H8v21.33H5.333Z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/databases/icons/databricks.svg b/frontend/src/components/databases/icons/databricks.svg
new file mode 100644
index 0000000000..bdb6e414cf
--- /dev/null
+++ b/frontend/src/components/databases/icons/databricks.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Databricks</title><path d="M.95 14.184L12 20.403l9.919-5.55v2.21L12 22.662l-10.484-5.96-.565.308v.77L12 24l11.05-6.218v-4.317l-.515-.309L12 19.118l-9.867-5.653v-2.21L12 16.805l11.05-6.218V6.32l-.515-.308L12 11.974 2.647 6.681 12 1.388l7.76 4.368.668-.411v-.566L12 0 .95 6.27v.72L12 13.207l9.919-5.55v2.26L12 15.52 1.516 9.56l-.565.308Z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/databases/icons/duckdb.svg b/frontend/src/components/databases/icons/duckdb.svg
new file mode 100644
index 0000000000..feaab85bc5
--- /dev/null
+++ b/frontend/src/components/databases/icons/duckdb.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>DuckDB</title><path d="M12 0C5.363 0 0 5.363 0 12s5.363 12 12 12 12-5.363 12-12S18.637 0 12 0zM9.502 7.03a4.974 4.974 0 0 1 4.97 4.97 4.974 4.974 0 0 1-4.97 4.97A4.974 4.974 0 0 1 4.532 12a4.974 4.974 0 0 1 4.97-4.97zm6.563 3.183h2.351c.98 0 1.787.782 1.787 1.762s-.807 1.789-1.787 1.789h-2.351v-3.551z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/databases/icons/mysql.svg b/frontend/src/components/databases/icons/mysql.svg
new file mode 100644
index 0000000000..195caada06
--- /dev/null
+++ b/frontend/src/components/databases/icons/mysql.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>MySQL</title><path d="M16.405 5.501c-.115 0-.193.014-.274.033v.013h.014c.054.104.146.18.214.273.054.107.1.214.154.32l.014-.015c.094-.066.14-.172.14-.333-.04-.047-.046-.094-.08-.14-.04-.067-.126-.1-.18-.153zM5.77 18.695h-.927a50.854 50.854 0 00-.27-4.41h-.008l-1.41 4.41H2.45l-1.4-4.41h-.01a72.892 72.892 0 00-.195 4.41H0c.055-1.966.192-3.81.41-5.53h1.15l1.335 4.064h.008l1.347-4.064h1.095c.242 2.015.384 3.86.428 5.53zm4.017-4.08c-.378 2.045-.876 3.533-1.492 4.46-.482.716-1.01 1.073-1.583 1.073-.153 0-.34-.046-.566-.138v-.494c.11.017.24.026.386.026.268 0 .483-.075.647-.222.197-.18.295-.382.295-.605 0-.155-.077-.47-.23-.944L6.23 14.615h.91l.727 2.36c.164.536.233.91.205 1.123.4-1.064.678-2.227.835-3.483zm12.325 4.08h-2.63v-5.53h.885v4.85h1.745zm-3.32.135l-1.016-.5c.09-.076.177-.158.255-.25.433-.506.648-1.258.648-2.253 0-1.83-.718-2.746-2.155-2.746-.704 0-1.254.232-1.65.697-.43.508-.646 1.256-.646 2.245 0 .972.19 1.686.574 2.14.35.41.877.615 1.583.615.264 0 .506-.033.725-.098l1.325.772.36-.622zM15.5 17.588c-.225-.36-.337-.94-.337-1.736 0-1.393.424-2.09 1.27-2.09.443 0 .77.167.977.5.224.362.336.936.336 1.723 0 1.404-.424 2.108-1.27 2.108-.445 0-.77-.167-.978-.5zm-1.658-.425c0 .47-.172.856-.516 1.156-.344.3-.803.45-1.384.45-.543 0-1.064-.172-1.573-.515l.237-.476c.438.22.833.328 1.19.328.332 0 .593-.073.783-.22a.754.754 0 00.3-.615c0-.33-.23-.61-.648-.845-.388-.213-1.163-.657-1.163-.657-.422-.307-.632-.636-.632-1.177 0-.45.157-.81.47-1.085.315-.278.72-.415 1.22-.415.512 0 .98.136 1.4.41l-.213.476a2.726 2.726 0 00-1.064-.23c-.283 0-.502.068-.654.206a.685.685 0 00-.248.524c0 .328.234.61.666.85.393.215 1.187.67 1.187.67.433.305.648.63.648 1.168zm9.382-5.852c-.535-.014-.95.04-1.297.188-.1.04-.26.04-.274.167.055.053.063.14.11.214.08.134.218.313.346.407.14.11.28.216.427.31.26.16.555.255.81.416.145.094.293.213.44.313.073.05.12.14.214.172v-.02c-.046-.06-.06-.147-.105-.214-.067-.067-.134-.127-.2-.193a3.223 3.223 0 00-.695-.675c-.214-.146-.682-.35-.77-.595l-.013-.014c.146-.013.32-.066.46-.106.227-.06.435-.047.67-.106.106-.027.213-.06.32-.094v-.06c-.12-.12-.21-.283-.334-.395a8.867 8.867 0 00-1.104-.823c-.21-.134-.476-.22-.697-.334-.08-.04-.214-.06-.26-.127-.12-.146-.19-.34-.275-.514a17.69 17.69 0 01-.547-1.163c-.12-.262-.193-.523-.34-.763-.69-1.137-1.437-1.826-2.586-2.5-.247-.14-.543-.2-.856-.274-.167-.008-.334-.02-.5-.027-.11-.047-.216-.174-.31-.235-.38-.24-1.364-.76-1.644-.072-.18.434.267.862.422 1.082.115.153.26.328.34.5.047.116.06.235.107.356.106.294.207.622.347.897.073.14.153.287.247.413.054.073.146.107.167.227-.094.136-.1.334-.154.5-.24.757-.146 1.693.194 2.25.107.166.362.534.703.393.3-.12.234-.5.32-.835.02-.08.007-.133.048-.187v.015c.094.188.188.367.274.555.206.328.566.668.867.895.16.12.287.328.487.402v-.02h-.015c-.043-.058-.1-.086-.154-.133a3.445 3.445 0 01-.35-.4 8.76 8.76 0 01-.747-1.218c-.11-.21-.202-.436-.29-.643-.04-.08-.04-.2-.107-.24-.1.146-.247.273-.32.453-.127.288-.14.642-.188 1.01-.027.007-.014 0-.027.014-.214-.052-.287-.274-.367-.46-.2-.475-.233-1.238-.06-1.785.047-.14.247-.582.167-.716-.042-.127-.174-.2-.247-.303a2.478 2.478 0 01-.24-.427c-.16-.374-.24-.788-.414-1.162-.08-.173-.22-.354-.334-.513-.127-.18-.267-.307-.368-.52-.033-.073-.08-.194-.027-.274.014-.054.042-.075.094-.09.088-.072.335.022.422.062.247.1.455.194.662.334.094.066.195.193.315.226h.14c.214.047.455.014.655.073.355.114.675.28.962.46a5.953 5.953 0 012.085 2.286c.08.154.115.295.188.455.14.33.313.663.455.982.14.315.275.636.476.897.1.14.502.213.682.286.133.06.34.115.46.188.23.14.454.3.67.454.11.076.443.243.463.378z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/databases/icons/postgresql.svg b/frontend/src/components/databases/icons/postgresql.svg
new file mode 100644
index 0000000000..dcf75b726d
--- /dev/null
+++ b/frontend/src/components/databases/icons/postgresql.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>PostgreSQL</title><path d="M23.5594 14.7228a.5269.5269 0 0 0-.0563-.1191c-.139-.2632-.4768-.3418-1.0074-.2321-1.6533.3411-2.2935.1312-2.5256-.0191 1.342-2.0482 2.445-4.522 3.0411-6.8297.2714-1.0507.7982-3.5237.1222-4.7316a1.5641 1.5641 0 0 0-.1509-.235C21.6931.9086 19.8007.0248 17.5099.0005c-1.4947-.0158-2.7705.3461-3.1161.4794a9.449 9.449 0 0 0-.5159-.0816 8.044 8.044 0 0 0-1.3114-.1278c-1.1822-.0184-2.2038.2642-3.0498.8406-.8573-.3211-4.7888-1.645-7.2219.0788C.9359 2.1526.3086 3.8733.4302 6.3043c.0409.818.5069 3.334 1.2423 5.7436.4598 1.5065.9387 2.7019 1.4334 3.582.553.9942 1.1259 1.5933 1.7143 1.7895.4474.1491 1.1327.1441 1.8581-.7279.8012-.9635 1.5903-1.8258 1.9446-2.2069.4351.2355.9064.3625 1.39.3772a.0569.0569 0 0 0 .0004.0041 11.0312 11.0312 0 0 0-.2472.3054c-.3389.4302-.4094.5197-1.5002.7443-.3102.064-1.1344.2339-1.1464.8115-.0025.1224.0329.2309.0919.3268.2269.4231.9216.6097 1.015.6331 1.3345.3335 2.5044.092 3.3714-.6787-.017 2.231.0775 4.4174.3454 5.0874.2212.5529.7618 1.9045 2.4692 1.9043.2505 0 .5263-.0291.8296-.0941 1.7819-.3821 2.5557-1.1696 2.855-2.9059.1503-.8707.4016-2.8753.5388-4.1012.0169-.0703.0357-.1207.057-.1362.0007-.0005.0697-.0471.4272.0307a.3673.3673 0 0 0 .0443.0068l.2539.0223.0149.001c.8468.0384 1.9114-.1426 2.5312-.4308.6438-.2988 1.8057-1.0323 1.5951-1.6698zM2.371 11.8765c-.7435-2.4358-1.1779-4.8851-1.2123-5.5719-.1086-2.1714.4171-3.6829 1.5623-4.4927 1.8367-1.2986 4.8398-.5408 6.108-.13-.0032.0032-.0066.0061-.0098.0094-2.0238 2.044-1.9758 5.536-1.9708 5.7495-.0002.0823.0066.1989.0162.3593.0348.5873.0996 1.6804-.0735 2.9184-.1609 1.1504.1937 2.2764.9728 3.0892.0806.0841.1648.1631.2518.2374-.3468.3714-1.1004 1.1926-1.9025 2.1576-.5677.6825-.9597.5517-1.0886.5087-.3919-.1307-.813-.5871-1.2381-1.3223-.4796-.839-.9635-2.0317-1.4155-3.5126zm6.0072 5.0871c-.1711-.0428-.3271-.1132-.4322-.1772.0889-.0394.2374-.0902.4833-.1409 1.2833-.2641 1.4815-.4506 1.9143-1.0002.0992-.126.2116-.2687.3673-.4426a.3549.3549 0 0 0 .0737-.1298c.1708-.1513.2724-.1099.4369-.0417.156.0646.3078.26.3695.4752.0291.1016.0619.2945-.0452.4444-.9043 1.2658-2.2216 1.2494-3.1676 1.0128zm2.094-3.988-.0525.141c-.133.3566-.2567.6881-.3334 1.003-.6674-.0021-1.3168-.2872-1.8105-.8024-.6279-.6551-.9131-1.5664-.7825-2.5004.1828-1.3079.1153-2.4468.079-3.0586-.005-.0857-.0095-.1607-.0122-.2199.2957-.2621 1.6659-.9962 2.6429-.7724.4459.1022.7176.4057.8305.928.5846 2.7038.0774 3.8307-.3302 4.7363-.084.1866-.1633.3629-.2311.5454zm7.3637 4.5725c-.0169.1768-.0358.376-.0618.5959l-.146.4383a.3547.3547 0 0 0-.0182.1077c-.0059.4747-.054.6489-.115.8693-.0634.2292-.1353.4891-.1794 1.0575-.11 1.4143-.8782 2.2267-2.4172 2.5565-1.5155.3251-1.7843-.4968-2.0212-1.2217a6.5824 6.5824 0 0 0-.0769-.2266c-.2154-.5858-.1911-1.4119-.1574-2.5551.0165-.5612-.0249-1.9013-.3302-2.6462.0044-.2932.0106-.5909.019-.8918a.3529.3529 0 0 0-.0153-.1126 1.4927 1.4927 0 0 0-.0439-.208c-.1226-.4283-.4213-.7866-.7797-.9351-.1424-.059-.4038-.1672-.7178-.0869.067-.276.1831-.5875.309-.9249l.0529-.142c.0595-.16.134-.3257.213-.5012.4265-.9476 1.0106-2.2453.3766-5.1772-.2374-1.0981-1.0304-1.6343-2.2324-1.5098-.7207.0746-1.3799.3654-1.7088.5321a5.6716 5.6716 0 0 0-.1958.1041c.0918-1.1064.4386-3.1741 1.7357-4.4823a4.0306 4.0306 0 0 1 .3033-.276.3532.3532 0 0 0 .1447-.0644c.7524-.5706 1.6945-.8506 2.802-.8325.4091.0067.8017.0339 1.1742.081 1.939.3544 3.2439 1.4468 4.0359 2.3827.8143.9623 1.2552 1.9315 1.4312 2.4543-1.3232-.1346-2.2234.1268-2.6797.779-.9926 1.4189.543 4.1729 1.2811 5.4964.1353.2426.2522.4522.2889.5413.2403.5825.5515.9713.7787 1.2552.0696.087.1372.1714.1885.245-.4008.1155-1.1208.3825-1.0552 1.717-.0123.1563-.0423.4469-.0834.8148-.0461.2077-.0702.4603-.0994.7662zm.8905-1.6211c-.0405-.8316.2691-.9185.5967-1.0105a2.8566 2.8566 0 0 0 .135-.0406 1.202 1.202 0 0 0 .1342.103c.5703.3765 1.5823.4213 3.0068.1344-.2016.1769-.5189.3994-.9533.6011-.4098.1903-1.0957.333-1.7473.3636-.7197.0336-1.0859-.0807-1.1721-.151zm.5695-9.2712c-.0059.3508-.0542.6692-.1054 1.0017-.055.3576-.112.7274-.1264 1.1762-.0142.4368.0404.8909.0932 1.3301.1066.887.216 1.8003-.2075 2.7014a3.5272 3.5272 0 0 1-.1876-.3856c-.0527-.1276-.1669-.3326-.3251-.6162-.6156-1.1041-2.0574-3.6896-1.3193-4.7446.3795-.5427 1.3408-.5661 2.1781-.463zm.2284 7.0137a12.3762 12.3762 0 0 0-.0853-.1074l-.0355-.0444c.7262-1.1995.5842-2.3862.4578-3.4385-.0519-.4318-.1009-.8396-.0885-1.2226.0129-.4061.0666-.7543.1185-1.0911.0639-.415.1288-.8443.1109-1.3505.0134-.0531.0188-.1158.0118-.1902-.0457-.4855-.5999-1.938-1.7294-3.253-.6076-.7073-1.4896-1.4972-2.6889-2.0395.5251-.1066 1.2328-.2035 2.0244-.1859 2.0515.0456 3.6746.8135 4.8242 2.2824a.908.908 0 0 1 .0667.1002c.7231 1.3556-.2762 6.2751-2.9867 10.5405zm-8.8166-6.1162c-.025.1794-.3089.4225-.6211.4225a.5821.5821 0 0 1-.0809-.0056c-.1873-.026-.3765-.144-.5059-.3156-.0458-.0605-.1203-.178-.1055-.2844.0055-.0401.0261-.0985.0925-.1488.1182-.0894.3518-.1226.6096-.0867.3163.0441.6426.1938.6113.4186zm7.9305-.4114c.0111.0792-.049.201-.1531.3102-.0683.0717-.212.1961-.4079.2232a.5456.5456 0 0 1-.075.0052c-.2935 0-.5414-.2344-.5607-.3717-.024-.1765.2641-.3106.5611-.352.297-.0414.6111.0088.6356.1851z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/databases/icons/snowflake.svg b/frontend/src/components/databases/icons/snowflake.svg
new file mode 100644
index 0000000000..27742797ea
--- /dev/null
+++ b/frontend/src/components/databases/icons/snowflake.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Snowflake</title><path d="M24 3.459c0 .646-.418 1.18-1.141 1.18-.723 0-1.142-.534-1.142-1.18 0-.647.419-1.18 1.142-1.18.723 0 1.141.533 1.141 1.18zm-.228 0c0-.533-.38-.951-.913-.951s-.913.38-.913.95c0 .533.38.952.913.952.57 0 .913-.419.913-.951zm-1.37-.533h.495c.266 0 .456.152.456.38 0 .153-.076.229-.19.305l.19.266v.038h-.266l-.19-.266h-.229v.266h-.266zm.495.228h-.229v.267h.229c.114 0 .152-.038.152-.114.038-.077-.038-.153-.152-.153zM7.602 12.4c.038-.151.076-.304.076-.456 0-.114-.038-.228-.038-.342-.114-.343-.304-.647-.646-.838l-4.87-2.777c-.685-.38-1.56-.152-1.94.533-.381.685-.153 1.56.532 1.94l2.701 1.56-2.701 1.56c-.685.38-.913 1.256-.533 1.94.38.685 1.256.914 1.94.533l4.832-2.777c.343-.267.571-.533.647-.876zm1.332 2.626c-.266-.038-.57.038-.837.19l-4.832 2.777c-.685.38-.913 1.256-.532 1.94.38.686 1.255.914 1.94.533l2.701-1.56v3.12c0 .8.647 1.408 1.446 1.408.799 0 1.407-.647 1.407-1.408v-5.592c0-.761-.57-1.37-1.293-1.408zm4.946-6.088c.266.038.57-.038.837-.19l4.832-2.777c.685-.38.913-1.256.532-1.94-.38-.686-1.255-.914-1.94-.533l-2.701 1.56V1.975c0-.799-.647-1.408-1.446-1.408-.799 0-1.446.609-1.446 1.408V7.53c0 .76.609 1.37 1.332 1.407zM3.265 5.97l4.832 2.777c.266.152.533.19.837.19.723-.038 1.331-.684 1.331-1.407V1.975c0-.799-.646-1.408-1.407-1.408-.799 0-1.446.647-1.446 1.408v3.12l-2.701-1.56c-.685-.38-1.56-.152-1.94.533-.419.646-.19 1.521.494 1.902zm9.093 6.011a.412.412 0 00-.114-.266l-.57-.571a.346.346 0 00-.267-.114.412.412 0 00-.266.114l-.571.57a.411.411 0 00-.114.267c0 .076.038.19.114.267l.57.57a.345.345 0 00.267.114c.076 0 .19-.038.266-.114l.571-.57a.412.412 0 00.114-.267zm1.598.533L11.94 14.53c-.039.038-.153.114-.229.114h-.608a.411.411 0 01-.267-.114L8.82 12.514a.408.408 0 01-.076-.229v-.608c0-.076.038-.19.114-.267l2.016-2.016a.41.41 0 01.267-.114h.608a.41.41 0 01.267.114l2.016 2.016a.347.347 0 01.114.267v.608c-.076.077-.114.19-.19.229zm5.593 5.44l-4.832-2.777c-.266-.152-.57-.19-.837-.152-.723.038-1.332.684-1.332 1.408v5.554c0 .8.647 1.408 1.408 1.408.799 0 1.446-.647 1.446-1.408v-3.12l2.7 1.56c.686.38 1.561.152 1.941-.533.419-.646.19-1.521-.494-1.94zm2.549-7.533l-2.701 1.56 2.7 1.56c.686.38.914 1.256.533 1.94-.38.685-1.255.913-1.94.533l-4.832-2.778a1.644 1.644 0 01-.647-.798c-.037-.153-.076-.305-.076-.457 0-.114.039-.228.039-.342.114-.343.342-.647.646-.837l4.832-2.778c.685-.38 1.56-.152 1.94.533.457.609.19 1.484-.494 1.864"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/databases/icons/sqlalchemy.svg b/frontend/src/components/databases/icons/sqlalchemy.svg
new file mode 100644
index 0000000000..83c5965852
--- /dev/null
+++ b/frontend/src/components/databases/icons/sqlalchemy.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>SQLAlchemy</title><path d="M11.8 15.955a44.068 44.068 0 0 1-1.673-.691c-1.736-.757-1.981-.772-2.499-.143-.119.146-.25.236-.287.2-.111-.111.219-.644.617-.993.325-.285.433-.325.791-.285.228.025.985.29 1.682.586 1.573.669 2.034.811 2.635.811.731 0 1.106-.512.876-1.192-.057-.171-.04-.228.074-.228.213 0 .322.797.168 1.255a1.617 1.617 0 0 1-.424.614c-.251.211-.41.257-.879.254a3.853 3.853 0 0 1-1.082-.188h.001Zm.301-2.225c0-.048.179-.134.401-.188l.401-.099.086-1.446c.094-1.599.025-3.172-.148-3.383-.063-.074-.253-.165-.427-.205-.705-.156-.236-.264 1.133-.264 1.368 0 1.803.099 1.152.264-.561.14-.564.148-.564 2.43 0 1.266.046 2.22.111 2.342.092.171.228.207.752.207 1.081 0 1.453-.255 1.747-1.203.088-.284.315-.233.236.054-.037.134-.097.54-.134.91l-.068.669H14.44c-1.286 0-2.339-.04-2.339-.088Zm5.312-.068c0-.086.083-.171.219-.236.183-.086.302-.265.734-1.11.686-1.337 1.767-3.634 1.87-3.978.079-.262.097-.276.392-.31.171-.02.313-.031.316-.025l.527 1.152c.284.628.856 1.824 1.271 2.654.695 1.397.772 1.523 1.005 1.636.142.069.253.174.253.237 0 .099-.122.111-1.175.111-1.056 0-1.175-.012-1.175-.114 0-.068.091-.142.236-.191.134-.043.236-.122.236-.182 0-.057-.139-.432-.31-.834l-.31-.731h-2.35l-.225.495c-.421.928-.43 1.147-.037 1.252.196.054.25.097.227.185-.025.103-.127.117-.867.117-.794.006-.837 0-.837-.128Zm-15.652.025a10.933 10.933 0 0 1-.808-.196l-.549-.154.282-.518.281-.518-.227-.281c-.322-.399-.737-1.272-.74-1.554-.003-.657.851-1.61 1.898-2.122.72-.353 1.291-.362 2.009-.026l.54.253.157-.224c.085-.123.156-.285.156-.356 0-.071.071-.134.157-.134.085 0 .156.023.156.048 0 .063-.629 1.651-.669 1.691-.017.016-.187-.063-.381-.177-.546-.321-1.232-.535-1.764-.549-1.238-.031-1.667 1.178-.794 2.236l.308.373.839-.68c.942-.76 1.05-.777 1.784-.27.825.569.839 1.434.042 2.339-.705.805-1.431 1.027-2.677.819Zm5.984-.165c-.646-.301-1.229-.876-1.565-1.547-.538-1.076-.373-1.765.646-2.695.856-.782 1.556-1.087 2.498-1.087.68 0 .825.037 1.266.307 1.044.646 1.303 1.878.675 3.221-.737 1.577-2.294 2.37-3.52 1.801Zm-3.872-.702c.409-.322.381-.917-.063-1.389-.558-.592-.731-.566-1.713.253-.976.814-.982.783.185 1.155.771.251 1.255.242 1.591-.019Zm6.034-.046c.484-.239.817-1.343.68-2.259-.17-1.13-1.698-1.901-2.819-1.423-1.153.493-1.17 1.804-.037 2.985.791.828 1.471 1.044 2.176.697Zm11.359-1.414c.04-.071-.845-2.003-.928-2.023-.06-.017-.976 1.872-.976 2.014 0 .072 1.861.08 1.904.009Z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/databases/icons/sqlite.svg b/frontend/src/components/databases/icons/sqlite.svg
new file mode 100644
index 0000000000..a776ea18f3
--- /dev/null
+++ b/frontend/src/components/databases/icons/sqlite.svg
@@ -0,0 +1 @@
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>SQLite</title><path d="M21.678.521c-1.032-.92-2.28-.55-3.513.544a8.71 8.71 0 0 0-.547.535c-2.109 2.237-4.066 6.38-4.674 9.544.237.48.422 1.093.544 1.561a13.044 13.044 0 0 1 .164.703s-.019-.071-.096-.296l-.05-.146a1.689 1.689 0 0 0-.033-.08c-.138-.32-.518-.995-.686-1.289-.143.423-.27.818-.376 1.176.484.884.778 2.4.778 2.4s-.025-.099-.147-.442c-.107-.303-.644-1.244-.772-1.464-.217.804-.304 1.346-.226 1.478.152.256.296.698.422 1.186.286 1.1.485 2.44.485 2.44l.017.224a22.41 22.41 0 0 0 .056 2.748c.095 1.146.273 2.13.5 2.657l.155-.084c-.334-1.038-.47-2.399-.41-3.967.09-2.398.642-5.29 1.661-8.304 1.723-4.55 4.113-8.201 6.3-9.945-1.993 1.8-4.692 7.63-5.5 9.788-.904 2.416-1.545 4.684-1.931 6.857.666-2.037 2.821-2.912 2.821-2.912s1.057-1.304 2.292-3.166c-.74.169-1.955.458-2.362.629-.6.251-.762.337-.762.337s1.945-1.184 3.613-1.72C21.695 7.9 24.195 2.767 21.678.521m-18.573.543A1.842 1.842 0 0 0 1.27 2.9v16.608a1.84 1.84 0 0 0 1.835 1.834h9.418a22.953 22.953 0 0 1-.052-2.707c-.006-.062-.011-.141-.016-.2a27.01 27.01 0 0 0-.473-2.378c-.121-.47-.275-.898-.369-1.057-.116-.197-.098-.31-.097-.432 0-.12.015-.245.037-.386a9.98 9.98 0 0 1 .234-1.045l.217-.028c-.017-.035-.014-.065-.031-.097l-.041-.381a32.8 32.8 0 0 1 .382-1.194l.2-.019c-.008-.016-.01-.038-.018-.053l-.043-.316c.63-3.28 2.587-7.443 4.8-9.791.066-.069.133-.128.198-.194Z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/components/editor/chrome/panels/datasources-panel.tsx b/frontend/src/components/editor/chrome/panels/datasources-panel.tsx
index c416a800eb..f1e899392f 100644
--- a/frontend/src/components/editor/chrome/panels/datasources-panel.tsx
+++ b/frontend/src/components/editor/chrome/panels/datasources-panel.tsx
@@ -44,6 +44,10 @@ import { variablesAtom } from "@/core/variables/state";
import { sortBy } from "lodash-es";
import { logNever } from "@/utils/assertNever";
import { Objects } from "@/utils/objects";
+import { DatabaseLogo } from "@/components/databases/icon";
+import { EngineVariable } from "@/components/databases/engine-variable";
+import type { VariableName } from "@/core/variables/types";
+import { dbDisplayName } from "@/components/databases/display";
const sortedTablesAtom = atom((get) => {
const tables = get(datasetTablesAtom);
@@ -113,8 +117,7 @@ export const DataSourcesPanel: React.FC = () => {
code = `_df = mo.sql(f"SELECT * FROM ${table.name} LIMIT 100")`;
break;
case "connection":
- // TODO: Handle connection source
- code = `_df = mo.sql("SELECT 1", engine=${table.source_type})`;
+ code = `_df = mo.sql(f"SELECT * FROM ${table.name} LIMIT 100", engine=${table.engine})`;
break;
default:
logNever(table.source_type);
@@ -219,10 +222,12 @@ const TableList: React.FC<{
isTableExpanded,
isColumnExpanded,
}) => {
+ // Tables grouped by engine (if exists) or source
+ // The engine is a more specific source, so it should be grouped first
let groupedBySource = Object.entries(
Objects.groupBy(
tables,
- (table) => table.source,
+ (table) => table.engine || table.source,
(table) => table,
),
);
@@ -290,16 +295,47 @@ const TableList: React.FC<{
return groupedBySource[0][1].flatMap(renderTable);
}
+ const renderLabel = (source: string, tables: DataTable[]) => {
+ if (source === "memory") {
+ return "In-Memory";
+ }
+
+ if (source === "duckdb") {
+ return "DuckDB";
+ }
+
+ const firstTable = tables[0];
+ if (firstTable.engine) {
+ return (
+ <>
+ <DatabaseLogo
+ className="h-4 w-4 text-muted-foreground"
+ name={firstTable.source}
+ />
+ {dbDisplayName(firstTable.source)}
+ <span className="text-xs text-muted-foreground">
+ (
+ <EngineVariable
+ variableName={firstTable.engine as VariableName}
+ />
+ )
+ </span>
+ </>
+ );
+ }
+ return source;
+ };
+
return groupedBySource.flatMap(([source, tables], idx) => {
return [
<div
className={cn(
- "font-bold px-2 py-1 text-muted-foreground bg-[var(--slate-2)] border-t text-sm",
+ "font-bold px-2 py-1 text-muted-foreground bg-[var(--slate-2)] border-t text-sm flex items-center gap-1",
idx > 0 && "border-t",
)}
key={source}
>
- {source === "memory" ? "In-Memory" : source}
+ {renderLabel(source, tables)}
</div>,
...tables.flatMap(renderTable),
];
@@ -378,7 +414,6 @@ const DatasetColumnItem: React.FC<{
table: DataTable;
column: DataTableColumn;
onExpandColumn: (table: DataTable, column: DataTableColumn) => void;
- // onAddColumnChart: (code: string) => void;
isExpanded: boolean;
}> = ({ table, column, onExpandColumn }) => {
const Icon = DATA_TYPE_ICON[column.type];
@@ -426,6 +461,23 @@ const DatasetColumnPreview: React.FC<{
}> = ({ table, column, preview, onAddColumnChart }) => {
const { theme } = useTheme();
+ if (table.source_type === "connection") {
+ return (
+ <span className="text-xs text-muted-foreground gap-2 flex items-center justify-between">
+ {column.name} ({column.external_type})
+ <Button
+ variant="outline"
+ size="xs"
+ onClick={Events.stopPropagation(() => {
+ onAddColumnChart(sqlCode(table, column));
+ })}
+ >
+ <PlusSquareIcon className="h-3 w-3 mr-1" /> Add SQL cell
+ </Button>
+ </span>
+ );
+ }
+
if (!preview) {
return <span className="text-xs text-muted-foreground">Loading...</span>;
}
@@ -498,8 +550,7 @@ const DatasetColumnPreview: React.FC<{
size="icon"
className="z-10 bg-background absolute right-1 -top-1"
onClick={Events.stopPropagation(() => {
- const code = `_df = mo.sql(f'SELECT "${column.name}" FROM ${table.name} LIMIT 100')`;
- onAddColumnChart(code);
+ onAddColumnChart(sqlCode(table, column));
})}
>
<PlusSquareIcon className="h-3 w-3" />
@@ -528,3 +579,10 @@ const DatasetColumnPreview: React.FC<{
</div>
);
};
+
+function sqlCode(table: DataTable, column: DataTableColumn) {
+ if (table.engine) {
+ return `_df = mo.sql(f'SELECT "${column.name}" FROM ${table.name} LIMIT 100', engine=${table.engine})`;
+ }
+ return `_df = mo.sql(f'SELECT "${column.name}" FROM ${table.name} LIMIT 100')`;
+}
diff --git a/marimo/_data/get_datasets.py b/marimo/_data/get_datasets.py
index 4b60a6184c..3de318a44d 100644
--- a/marimo/_data/get_datasets.py
+++ b/marimo/_data/get_datasets.py
@@ -1,7 +1,7 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from typing import List, Optional, cast
+from typing import TYPE_CHECKING, List, Optional, cast
from marimo import _loggers
from marimo._data.models import DataTable, DataTableColumn, DataType
@@ -9,6 +9,9 @@ from marimo._plugins.ui._impl.tables.utils import get_table_manager_or_none
LOGGER = _loggers.marimo_logger()
+if TYPE_CHECKING:
+ import duckdb
+
def get_datasets_from_variables(
variables: List[tuple[str, object]],
@@ -74,17 +77,21 @@ def has_updates_to_datasource(query: str) -> bool:
)
-def get_datasets_from_duckdb() -> List[DataTable]:
+def get_datasets_from_duckdb(
+ connection: Optional[duckdb.DuckDBPyConnection],
+ engine_name: Optional[str] = None,
+) -> List[DataTable]:
try:
- return _get_datasets_from_duckdb_internal()
+ return _get_datasets_from_duckdb_internal(connection, engine_name)
except Exception as e:
LOGGER.error(e)
return []
-def _get_datasets_from_duckdb_internal() -> List[DataTable]:
- import duckdb # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
-
+def _get_datasets_from_duckdb_internal(
+ connection: Optional[duckdb.DuckDBPyConnection],
+ engine_name: Optional[str] = None,
+) -> List[DataTable]:
# Columns
# 0:"database"
# 1:"schema"
@@ -92,7 +99,12 @@ def _get_datasets_from_duckdb_internal() -> List[DataTable]:
# 3:"column_names"
# 4:"column_types"
# 5:"temporary"
- databases = duckdb.execute("SHOW ALL TABLES").fetchall()
+ if connection is None:
+ import duckdb
+
+ databases = duckdb.execute("SHOW ALL TABLES").fetchall()
+ else:
+ databases = connection.execute("SHOW ALL TABLES").fetchall()
if not len(databases):
# No tables
return []
@@ -126,13 +138,14 @@ def _get_datasets_from_duckdb_internal() -> List[DataTable]:
tables.append(
DataTable(
- source_type="duckdb",
+ source_type="duckdb" if engine_name is None else "connection",
source=database,
name=f"{database}.{schema}.{name}",
num_rows=None,
num_columns=len(columns),
variable_name=None,
columns=columns,
+ engine=engine_name,
)
)
diff --git a/marimo/_data/models.py b/marimo/_data/models.py
index 40f40afe4c..17fe24f532 100644
--- a/marimo/_data/models.py
+++ b/marimo/_data/models.py
@@ -39,6 +39,8 @@ class DataTableColumn:
DataTableSource = Literal["local", "duckdb", "connection"]
+VariableName = str
+
@dataclass
class DataTable:
@@ -50,9 +52,10 @@ class DataTable:
name (str): The name of the data table.
num_rows (Optional[int]): The number of rows in the data table.
num_columns (Optional[int]): The number of columns in the data table.
- variable_name (Optional[str]): The variable name associated with
+ variable_name (Optional[VariableName]): The variable name associated with
the data table.
columns (List[DataTableColumn]): The list of columns in the data table.
+ engine (Optional[VariableName]): The engine associated with the data table.
"""
source_type: DataTableSource
@@ -60,8 +63,9 @@ class DataTable:
name: str
num_rows: Optional[int]
num_columns: Optional[int]
- variable_name: Optional[str]
+ variable_name: Optional[VariableName]
columns: List[DataTableColumn]
+ engine: Optional[VariableName] = None
NumericLiteral = Union[int, float, Decimal]
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index 83c126b7f9..8e6ebcf6a8 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -164,6 +164,12 @@ def _broadcast_data_source_connection(
]
).broadcast()
+ for variable, engine in engines:
+ tables = engine.get_tables()
+ if tables:
+ LOGGER.debug(f"Broadcasting engine tables for {variable}")
+ Datasets(tables=tables).broadcast()
+
@kernel_tracer.start_as_current_span("broadcast_duckdb_tables")
def _broadcast_duckdb_tables(
@@ -190,7 +196,7 @@ def _broadcast_duckdb_tables(
if not modifies_datasources:
return
- tables = get_datasets_from_duckdb()
+ tables = get_datasets_from_duckdb(connection=None)
if not tables:
return
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 0a8249a186..d0e677f1a1 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -2043,8 +2043,12 @@ class Kernel:
dataset = self.globals[table_name]
column_preview = get_column_preview_dataframe(dataset, request)
elif source_type == "connection":
- # TODO: Handle data-source-connection / engine display
- pass
+ DataColumnPreview(
+ error="Column preview for connection data sources is not supported",
+ column_name=column_name,
+ table_name=table_name,
+ ).broadcast()
+ return
else:
assert_never(source_type)
diff --git a/marimo/_smoke_tests/sql/sqlmodel_engine.py b/marimo/_smoke_tests/sql/sqlmodel_engine.py
index 333d502df0..86e056abc1 100644
--- a/marimo/_smoke_tests/sql/sqlmodel_engine.py
+++ b/marimo/_smoke_tests/sql/sqlmodel_engine.py
@@ -11,16 +11,56 @@
import marimo
-__generated_with = "0.10.17"
+__generated_with = "0.10.19"
app = marimo.App(width="medium")
+def _(mo, sqlite):
+ _df = mo.sql(
+ f"""
+
+ """,
+ engine=sqlite
+ )
+ return
+
+
+def _():
+ import ibis
+ import duckdb
+
+ # con = ibis.duckdb.connect()
+
+ data = {
+ "id": [1, 2, 3, 4],
+ "name": ["Alice", "Bob", "Charlie", "David"],
+ "age": [25, 30, 35, 40],
+ }
+
+ table = ibis.memtable(data)
+ return data, duckdb, ibis, table
+
+
+def _():
+ import altair as alt
+ return (alt,)
+
+
@app.cell
def _():
import marimo as mo
return (mo,)
+def _(mo, products_df):
+ mo.ui.table(products_df)
+ return
+
+
@app.cell(hide_code=True)
def _(mo):
mo.md("""# SQLModel with `mo.sql()`""")
@@ -203,5 +243,88 @@ def _(information_schema, mo, my_postgres, tables):
return
+def _(connection, mo):
+ _df = mo.sql(
+ f"""
+ SELECT 1
+ """,
+ engine=connection
+ )
+ return
+
+
+def _(mo, products, sqlite):
+ _df = mo.sql(
+ f"""
+ SELECT * FROM products LIMIT 100
+ """,
+ engine=sqlite
+ )
+ return
+
+
+def _(mo, products, sqlalchemy):
+ _df = mo.sql(
+ f"""
+ SELECT * FROM products LIMIT 100
+ """,
+ engine=sqlalchemy
+ )
+ return
+
+
+def _(mo):
+ _df = mo.sql(
+ f"""
+ CREATE OR REPLACE TABLE foo AS
+ FROM 'hf://datasets/julien040/hacker-news-posts/story.parquet' LIMIT 500
+ """
+ )
+ return (foo,)
+
+
+def _(foo, mo):
+ _df = mo.sql(
+ f"""
+ SELECT "id" FROM memory.main.foo LIMIT 100
+ """
+ )
+ return
+
+
+def _(mo, products, sqlite):
+ _df = mo.sql(
+ f"""
+ SELECT "id" FROM products LIMIT 100
+ """,
+ engine=sqlite
+ )
+ return
+
+
+def _(mo, products, sqlite):
+ _df = mo.sql(
+ f"""
+ SELECT "name" FROM products LIMIT 100
+ """,
+ engine=sqlite
+ )
+ return
+
+
+def _():
+
+
+ return
+
+
if __name__ == "__main__":
app.run()
diff --git a/marimo/_sql/engines.py b/marimo/_sql/engines.py
index 69ff5ea4d1..7f009ff37e 100644
--- a/marimo/_sql/engines.py
+++ b/marimo/_sql/engines.py
@@ -3,10 +3,15 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Any, Optional
+from marimo import _loggers
+from marimo._data.get_datasets import get_datasets_from_duckdb
+from marimo._data.models import DataTable, DataTableColumn, DataType
from marimo._dependencies.dependencies import DependencyManager
from marimo._sql.types import SQLEngine
from marimo._sql.utils import wrapped_sql
+LOGGER = _loggers.marimo_logger()
+
if TYPE_CHECKING:
import duckdb
from sqlalchemy import Engine
@@ -24,9 +29,12 @@ class DuckDBEngine(SQLEngine):
"""DuckDB SQL engine."""
def __init__(
- self, connection: Optional[duckdb.DuckDBPyConnection]
+ self,
+ connection: Optional[duckdb.DuckDBPyConnection],
+ engine_name: Optional[str] = None,
) -> None:
self._connection = connection
+ self._engine_name = engine_name
@property
def source(self) -> str:
@@ -59,16 +67,22 @@ class DuckDBEngine(SQLEngine):
return isinstance(var, duckdb.DuckDBPyConnection)
+ def get_tables(self) -> list[DataTable]:
+ return get_datasets_from_duckdb(self._connection, self._engine_name)
+
class SQLAlchemyEngine(SQLEngine):
"""SQLAlchemy engine."""
- def __init__(self, engine: Engine) -> None:
+ def __init__(
+ self, engine: Engine, engine_name: Optional[str] = None
+ ) -> None:
self._engine = engine
+ self._engine_name = engine_name
@property
def source(self) -> str:
- return "sqlalchemy"
+ return str(self._engine.dialect.name)
@property
def dialect(self) -> str:
@@ -108,3 +122,66 @@ class SQLAlchemyEngine(SQLEngine):
from sqlalchemy.engine import Engine
return isinstance(var, Engine)
+
+ def _should_include_columns(self) -> bool:
+ # Including columns can fan out to a lot of requests,
+ # so this is disabled for now.
+ # Maybe in future we can enable this as a flag or for certain connection types.
+ return False
+
+ def get_tables(self) -> list[DataTable]:
+ from sqlalchemy import MetaData
+
+ try:
+ metadata = MetaData()
+ metadata.reflect(bind=self._engine)
+ except Exception:
+ LOGGER.debug("Failed to reflect tables", exc_info=True)
+ # If we fail to reflect, we don't want to crash the app.
+ # Just return an empty list.
+ return []
+
+ tables: list[DataTable] = []
+ for table_name, table in metadata.tables.items():
+ tables.append(
+ DataTable(
+ source_type="connection",
+ source=self.dialect,
+ name=table_name,
+ num_rows=None,
+ num_columns=len(table.columns),
+ variable_name=None,
+ engine=self._engine_name,
+ columns=(
+ [
+ DataTableColumn(
+ name=col.name,
+ type=_sql_type_to_data_type(str(col.type)),
+ external_type=str(col.type),
+ sample_values=[],
+ )
+ for col in table.columns
+ ]
+ ),
+ )
+ )
+ return tables
+
+
+def _sql_type_to_data_type(type_str: str) -> DataType:
+ """Convert SQL type string to DataType"""
+ type_str = type_str.lower()
+ if any(x in type_str for x in ("int", "serial")):
+ return "integer"
+ elif any(x in type_str for x in ("float", "double", "decimal", "numeric")):
+ return "number"
+ elif any(x in type_str for x in ("timestamp", "datetime")):
+ return "datetime"
+ elif "date" in type_str:
+ return "date"
+ elif "bool" in type_str:
+ return "boolean"
+ elif any(x in type_str for x in ("char", "text")):
+ return "string"
+ else:
+ return "string"
diff --git a/marimo/_sql/get_engines.py b/marimo/_sql/get_engines.py
index ead48a3d7f..47c4b882d0 100644
--- a/marimo/_sql/get_engines.py
+++ b/marimo/_sql/get_engines.py
@@ -14,9 +14,21 @@ def get_engines_from_variables(
engines: list[tuple[str, SQLEngine]] = []
for variable_name, value in variables:
if SQLAlchemyEngine.is_compatible(value):
- engines.append((variable_name, SQLAlchemyEngine(cast(Any, value))))
+ engines.append(
+ (
+ variable_name,
+ SQLAlchemyEngine(
+ cast(Any, value), engine_name=variable_name
+ ),
+ )
+ )
elif DuckDBEngine.is_compatible(value):
- engines.append((variable_name, DuckDBEngine(cast(Any, value))))
+ engines.append(
+ (
+ variable_name,
+ DuckDBEngine(cast(Any, value), engine_name=variable_name),
+ )
+ )
return engines
diff --git a/marimo/_sql/types.py b/marimo/_sql/types.py
index 259c7cb1aa..dfb5056f82 100644
--- a/marimo/_sql/types.py
+++ b/marimo/_sql/types.py
@@ -4,6 +4,8 @@ from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any
+from marimo._data.models import DataTable
+
class SQLEngine(ABC):
"""Protocol for SQL engines that can execute queries."""
@@ -30,3 +32,8 @@ class SQLEngine(ABC):
def is_compatible(var: Any) -> bool:
"""Check if a variable is a compatible engine."""
pass
+
+ @abstractmethod
+ def get_tables(self) -> list[DataTable]:
+ """Get data tables from a variable."""
+ pass
diff --git a/openapi/api.yaml b/openapi/api.yaml
index 2747723e60..6cd4ab8f08 100644
--- a/openapi/api.yaml
+++ b/openapi/api.yaml
@@ -416,6 +416,9 @@ components:
items:
$ref: '#/components/schemas/DataTableColumn'
type: array
+ engine:
+ nullable: true
+ type: string
name:
type: string
num_columns:
@@ -2019,7 +2022,7 @@ components:
type: object
info:
title: marimo API
- version: 0.10.17
+ version: 0.10.19
openapi: 3.1.0
paths:
/@file/{filename_and_length}:
diff --git a/openapi/src/api.ts b/openapi/src/api.ts
index 0d2a79ed4e..396a46efc0 100644
--- a/openapi/src/api.ts
+++ b/openapi/src/api.ts
@@ -2243,6 +2243,7 @@ export interface components {
};
DataTable: {
columns: components["schemas"]["DataTableColumn"][];
+ engine?: string | null;
name: string;
num_columns?: number | null;
num_rows?: number | null;
diff --git a/tests/_data/test_get_datasets.py b/tests/_data/test_get_datasets.py
index f229d8ffa7..9a7fd376cd 100644
--- a/tests/_data/test_get_datasets.py
+++ b/tests/_data/test_get_datasets.py
@@ -26,11 +26,12 @@ def test_has_updates_to_datasource() -> None:
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
def test_get_datasets() -> None:
- assert get_datasets_from_duckdb() == []
+ assert get_datasets_from_duckdb(connection=None) == []
import duckdb
- duckdb.execute("""
+ duckdb.execute(
+ """
CREATE TABLE all_types (
col_boolean BOOLEAN,
col_tinyint TINYINT,
@@ -55,8 +56,9 @@ def test_get_datasets() -> None:
col_uuid UUID,
col_json JSON
)
- """)
- assert get_datasets_from_duckdb() == [
+ """
+ )
+ assert get_datasets_from_duckdb(connection=None) == [
DataTable(
name="memory.main.all_types",
source_type="duckdb",
diff --git a/tests/_sql/test_engines.py b/tests/_sql/test_engines.py
index 027352ea80..25fe97cb66 100644
--- a/tests/_sql/test_engines.py
+++ b/tests/_sql/test_engines.py
@@ -7,6 +7,7 @@ from unittest.mock import MagicMock
import pytest
+from marimo._data.models import DataTable
from marimo._dependencies.dependencies import DependencyManager
from marimo._sql.engines import (
DuckDBEngine,
@@ -158,3 +159,74 @@ def test_raise_df_import_error() -> None:
"""Test raise_df_import_error function."""
with pytest.raises(ImportError):
raise_df_import_error("test_pkg")
+
+
[email protected](not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")
+def test_sqlalchemy_engine_get_tables(sqlite_engine: sa.Engine) -> None:
+ """Test SQLAlchemyEngine get_tables method."""
+ engine = SQLAlchemyEngine(sqlite_engine, engine_name="test_sqlite")
+ tables = engine.get_tables()
+
+ assert isinstance(tables, list)
+ assert len(tables) == 1
+
+ table = tables[0]
+ assert isinstance(table, DataTable)
+ assert table.name == "test"
+ assert table.engine == "test_sqlite"
+ assert len(table.columns) == 2
+ assert table.columns[0].name == "id"
+ assert table.columns[0].type == "integer"
+ assert table.columns[1].name == "name"
+ assert table.columns[1].type == "string"
+
+
[email protected](not HAS_DUCKDB, reason="DuckDB not installed")
+def test_duckdb_engine_get_tables(
+ duckdb_connection: duckdb.DuckDBPyConnection,
+) -> None:
+ """Test DuckDBEngine get_tables method."""
+ engine = DuckDBEngine(duckdb_connection, engine_name="test_duckdb")
+ tables = engine.get_tables()
+
+ assert isinstance(tables, list)
+ assert len(tables) == 1
+
+ table = tables[0]
+ assert isinstance(table, DataTable)
+ assert table.name == "memory.main.test"
+ assert table.engine == "test_duckdb"
+ assert table.source_type == "connection"
+ assert len(table.columns) == 2
+ assert table.columns[0].name == "id"
+ assert table.columns[0].type == "integer"
+ assert table.columns[1].name == "name"
+ assert table.columns[1].type == "string"
+
+
+ not HAS_DUCKDB or not HAS_SQLALCHEMY,
+ reason="Duckdb and sqlalchemy not installed",
+)
+def test_engine_name_initialization() -> None:
+ """Test engine name initialization."""
+ import duckdb
+ import sqlalchemy as sa
+
+ duckdb_conn = duckdb.connect(":memory:")
+ sqlite_engine = sa.create_engine("sqlite:///:memory:")
+
+ duck_engine = DuckDBEngine(duckdb_conn, engine_name="my_duck")
+ sql_engine = SQLAlchemyEngine(sqlite_engine, engine_name="my_sql")
+
+ assert duck_engine._engine_name == "my_duck"
+ assert sql_engine._engine_name == "my_sql"
+
+ # Test default names
+ duck_engine_default = DuckDBEngine(duckdb_conn)
+ sql_engine_default = SQLAlchemyEngine(sqlite_engine)
+
+ assert duck_engine_default._engine_name is None
+ assert sql_engine_default._engine_name is None
+
+ duckdb_conn.close()
diff --git a/tests/_sql/test_get_engines.py b/tests/_sql/test_get_engines.py
index ffac683ab2..58eaeb12d3 100644
--- a/tests/_sql/test_get_engines.py
+++ b/tests/_sql/test_get_engines.py
@@ -34,7 +34,7 @@ def test_engine_to_data_source_connection():
"my_postgres", sqlalchemy_engine
)
assert isinstance(connection, DataSourceConnection)
- assert connection.source == "sqlalchemy"
+ assert connection.source == "postgresql"
assert connection.dialect == "postgresql"
assert connection.name == "my_postgres"
assert connection.display_name == "postgresql (my_postgres)"