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.

example: duckdb connection, fix openapi doc url (#1781)

Myles Scolnickb0efec00d898
diff --git a/docs/guides/ai_completion.md b/docs/guides/ai_completion.md
index 25aad82906..b2b18f78e4 100644
--- a/docs/guides/ai_completion.md
+++ b/docs/guides/ai_completion.md
@@ -56,7 +56,7 @@ api_key = "sk-..."
# Choose a model, we recommend "gpt-4-turbo"
model = "gpt-4-turbo"
# Change the base_url if you are using a different OpenAI-compatible API
-base_url = "https://api.openai.com"
+base_url = "https://api.openai.com/v1"
```
Once enabled, you can use AI completion by pressing `Ctrl/Cmd-Shift-e` in a
diff --git a/examples/sql/duckdb_connections.py b/examples/sql/duckdb_connections.py
new file mode 100644
index 0000000000..c7bc8bede2
--- /dev/null
+++ b/examples/sql/duckdb_connections.py
@@ -0,0 +1,193 @@
+import marimo
+
+__generated_with = "0.7.2"
+app = marimo.App(width="full")
+
+
+def __():
+ import marimo as mo
+ import os
+ return mo, os
+
+
+def __(mo):
+ mo.md(r"## Create a connection")
+ return
+
+
+def __(mo, os):
+ database_url = mo.ui.text(
+ label="Database URL",
+ full_width=True,
+ value=os.environ.get("DATABASE_URL", ""),
+ )
+ database_url
+ return database_url,
+
+
+def __(database_url):
+ import duckdb
+
+ if database_url.value:
+ duckdb.sql(
+ f"""
+ INSTALL postgres;
+ LOAD postgres;
+
+ DETACH DATABASE IF EXISTS my_db;
+ ATTACH DATABASE '{database_url.value}' AS my_db (TYPE postgres, READ_ONLY);
+ """
+ )
+ return duckdb,
+
+
+def __(duckdb):
+ duckdb.sql("SHOW DATABASES").show()
+ return
+
+
+def __(mo):
+ mo.md(r"## Tables")
+ return
+
+
+def __(mo):
+ _df = mo.sql(
+ f"""
+ SHOW ALL TABLES;
+ """
+ )
+ return
+
+
+def __(duckdb):
+ # Create another table, if wanted
+ if False:
+ duckdb.execute(
+ """
+ CREATE TABLE IF NOT EXISTS penguins AS SELECT * FROM read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv', AUTO_DETECT=TRUE);
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(r"## Other meta table functions")
+ return
+
+
+def __():
+ FUNCTIONS = [
+ "duckdb_columns()", # columns
+ "duckdb_constraints()", # constraints
+ "duckdb_databases()", # lists the databases that are accessible from within the current DuckDB process
+ "duckdb_dependencies()", # dependencies between objects
+ "duckdb_extensions()", # extensions
+ "duckdb_functions()", # functions
+ "duckdb_indexes()", # secondary indexes
+ "duckdb_keywords()", # DuckDB's keywords and reserved words
+ "duckdb_optimizers()", # the available optimization rules in the DuckDB instance
+ "duckdb_schemas()", # schemas
+ "duckdb_sequences()", # sequences
+ "duckdb_settings()", # settings
+ "duckdb_tables()", # base tables
+ "duckdb_types()", # data types
+ "duckdb_views()", # views
+ "duckdb_temporary_files()", # the temporary files DuckDB has written to disk, to offload data from memory
+ ]
+ return FUNCTIONS,
+
+
+def __(FUNCTIONS, mo):
+ function = mo.ui.dropdown(
+ label="Dropdown",
+ options=FUNCTIONS,
+ value=FUNCTIONS[0],
+ )
+ function
+ return function,
+
+
+def __(_, function, mo):
+ _df = mo.sql(
+ f"""
+ SELECT * FROM {function.value} WHERE database_name == 'my_db'
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(r"## Interact with your tables")
+ return
+
+
+def __(duckdb, mo):
+ tables = duckdb.execute(
+ """
+ SELECT table_name FROM duckdb_tables() WHERE internal = False;
+ """
+ ).df()
+ table_names = list(tables["table_name"])
+ mo.accordion({f"Found {len(table_names)} tables": table_names})
+ return table_names, tables
+
+
+def __(mo, table_names):
+ mo.stop(not table_names)
+ table_select = mo.ui.dropdown(
+ label="Table",
+ options=table_names,
+ )
+ limit = mo.ui.slider(
+ label="Limit",
+ start=100,
+ stop=10_000,
+ step=10,
+ debounce=True,
+ show_value=True,
+ )
+ mo.hstack([table_select, limit]).left()
+ return limit, table_select
+
+
+def __(mo, table_select):
+ mo.stop(not table_select.value)
+ table_select_value = table_select.value
+ return table_select_value,
+
+
+def __(limit, mo, table_select_value):
+ selected_table = mo.sql(
+ f"""
+ select * from my_db.{table_select_value} LIMIT {limit.value};
+ """
+ )
+ return selected_table,
+
+
+def __(mo, selected_table):
+ mo.ui.data_explorer(selected_table)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index bc76b19030..06549739af 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -539,7 +539,7 @@ export const UserConfigForm: React.FC = () => {
<Input
data-testid="ai-base-url-input"
className="m-0 inline-flex"
- placeholder="https://api.openai.com"
+ placeholder="https://api.openai.com/v1"
{...field}
/>
</FormControl>