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
example: sqlite (#1974)
Akshay Agrawala5f6fd469287
diff --git a/examples/sql/duckdb_connections.py b/examples/sql/duckdb_connections.pyindex c7bc8bede2..f26ebe3002 100644--- a/examples/sql/duckdb_connections.py+++ b/examples/sql/duckdb_connections.py@@ -1,6 +1,6 @@import marimo-__generated_with = "0.7.2"+__generated_with = "0.7.18"app = marimo.App(width="full")@@ -13,7 +13,7 @@ def __():@app.celldef __(mo):- mo.md(r"## Create a connection")+ mo.md(r"""## Create a connection""")return@@ -53,7 +53,7 @@ def __(duckdb):@app.celldef __(mo):- mo.md(r"## Tables")+ mo.md(r"""## Tables""")return@@ -81,7 +81,7 @@ def __(duckdb):@app.celldef __(mo):- mo.md(r"## Other meta table functions")+ mo.md(r"""## Other meta table functions""")return@@ -131,7 +131,7 @@ def __(_, function, mo):@app.celldef __(mo):- mo.md(r"## Interact with your tables")+ mo.md(r"""## Interact with your tables""")returndiff --git a/examples/sql/sqlite.py b/examples/sql/sqlite.pynew file mode 100644index 0000000000..68565bceec--- /dev/null+++ b/examples/sql/sqlite.py@@ -0,0 +1,77 @@+import marimo++__generated_with = "0.7.18"+app = marimo.App(width="medium")++[email protected](hide_code=True)+def __(mo):+ mo.md(+ r"""+ # SQLite!++ You can use marimo's SQL cells to read from (and write to) SQLite databases.++ The first step is to attach a SQLite database. We attach to a sample database in a read-only mode below.+ """+ )+ return+++def __(mo):+ _df = mo.sql(+ f"""+ -- boilerplate: detach the database so this cell works when you re-run it+ DETACH DATABASE IF EXISTS chinook;+ -- attach the database; omit READ_ONLY if you want to write to the database.+ ATTACH 'Chinook_Sqlite.sqlite' as chinook (TYPE SQLITE, READ_ONLY);++ SELECT table_name FROM INFORMATION_SCHEMA.TABLES where table_catalog == 'chinook';+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(r"""Once the database is attached, you can query it with SQL. For example, the next cell computes the average track length of each composer in the chinook database.""")+ return+++def __(mo, track):+ _df = mo.sql(+ f"""+ SELECT composer, MEAN(Milliseconds) as avg_track_ms from chinook.track GROUP BY composer ORDER BY avg_track_ms DESC;+ """+ )+ return++[email protected](hide_code=True)+def __():+ import marimo as mo+++ def download_sample_data():+ import os+ import requests++ url = "https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite"+ filename = "Chinook_Sqlite.sqlite"+ if not os.path.exists(filename):+ print("Downloading the Chinook database ...")+ response = requests.get(url)+ with open(filename, "wb") as f:+ f.write(response.content)+ else:+ print("Using cached database.")+++ download_sample_data()+ return download_sample_data, mo+++if __name__ == "__main__":+ app.run()