Spaces:
Sleeping
Sleeping
File size: 7,321 Bytes
5aaa673 8d99d46 5aaa673 3b7283c 5aaa673 ddd7855 5aaa673 3b7283c 5aaa673 3b7283c 5aaa673 3b7283c 5aaa673 3b7283c 5aaa673 3b7283c 5aaa673 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | # /// script
# requires-python = ">=3.12"
# dependencies = [
# "duckdb==1.4.3",
# "kagglehub==0.3.13",
# "marimo",
# "polars==1.36.1",
# "pyarrow==22.0.0",
# "sqlalchemy==2.0.45",
# "sqlglot==28.3.0",
# ]
# ///
import marimo
__generated_with = "0.18.4"
app = marimo.App(width="medium")
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## SQL Features in Marimo and Polars
_By [etrotta](https://github.com/etrotta)_
For this Notebook, we'll be using a [hotel booking analytics](https://www.kaggle.com/datasets/alperenmyung/international-hotel-booking-analytics) dataset.
We will see many ways in which you can use SQL inside of marimo and how each feature interacts with polars, including:
- How to read data from a SQLite file (or any Database connection)
- What are SQL Cells in Marimo
- How to load an SQL query into a DataFrame
- How to query DataFrames using SQL
""")
return
@app.cell
def _(mo, sqlite_engine):
_df = mo.sql(
f"""
SELECT * FROM reviews LIMIT 100
""",
engine=sqlite_engine,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
We will start by using `kagglehub` to download a `.sqlite` file, then create an `SQLAlchemy` engine to let marimo know about the database.
""")
return
@app.cell
def _(kagglehub):
dataset_id = "alperenmyung/international-hotel-booking-analytics"
cached_file = kagglehub.dataset_download(dataset_id, "booking_db.sqlite")
return (cached_file,)
@app.cell
def _(cached_file):
cached_file
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Using Marimo's SQL Cells
""")
return
@app.cell
def _(cached_file, sqlalchemy):
sqlite_engine = sqlalchemy.create_engine("sqlite:///" + cached_file)
return (sqlite_engine,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
After creating the Engine, you should be able to see it in the **Data Sources** panel in the sidebar. Whenever you create an SQLAlchemy engine as a global variable, Marimo picks up on it and makes it available for use in SQL Cells
You can use it to consult all tables and their columns, as well as click "Add table to notebook" to get the code to use it, creating our first SQL Cell:
""")
return
@app.cell
def _(mo, sqlite_engine):
_df = mo.sql(
f"""
SELECT * FROM hotels LIMIT 10
""",
engine=sqlite_engine,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
The `Output variable:` can be used to save the output as a polars DataFrame you can access later
For example, fetching all scores then performing a group by in polars
""")
return
@app.cell
def _(mo, sqlite_engine):
polars_age_groups = mo.sql(
f"""
SELECT reviews.*, age_group FROM reviews JOIN users ON reviews.user_id = users.user_id LIMIT 1000
""",
engine=sqlite_engine,
)
return (polars_age_groups,)
@app.cell
def _(pl, polars_age_groups):
_mean_scores = pl.col("^score_.*$").mean()
_age_group_start = pl.col("age_group").str.slice(0, 2).cast(int)
polars_age_groups.group_by("age_group").agg(_mean_scores).sort(_age_group_start)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
Although you could also calculate it directly in SQL, this gives you the flexibility to use polars for operations that are harder to describe in SQL
""")
return
@app.cell
def _(mo, sqlite_engine):
_df = mo.sql(
f"""
SELECT age_group, AVG(reviews.score_overall) FROM reviews JOIN users ON reviews.user_id = users.user_id GROUP BY age_group
""",
engine=sqlite_engine,
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
You can also use SQL Cells to query DataFrames via DuckDB, but remember to change the Engine from the SQLite engine into the DuckDB Memory engine when doing so
""")
return
@app.cell
def _(mo):
_df = mo.sql(
f"""
SELECT * FROM polars_age_groups LIMIT 10
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Using Polars directly
""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
Polars also offers some methods to interact with databases and query DataFrames using SQL directly, which you can use inside or outside of marimo the same.
""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
Reading data from Databases:
""")
return
@app.cell
def _(pl, sqlite_engine):
hotels = pl.read_database("SELECT * FROM hotels LIMIT 10", sqlite_engine)
hotels
return (hotels,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
Querying DataFrames with SQL:
""")
return
@app.cell
def _(hotels):
hotels.sql("SELECT * from self ORDER BY cleanliness_base DESC LIMIT 5")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Using DuckDB
""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
While marimo's SQL Cells are very practical and polars's direct methods are about as portable as it gets using polars, you can also use other libraries that integrate with polars via Arrow tables or input plugins.
One example of such integrations is DuckDB, which can be used with polars's Lazy mode as of 1.4.0
""")
return
@app.cell
def _(cached_file, duckdb):
duckdb_conn = duckdb.connect(cached_file)
return (duckdb_conn,)
@app.cell
def _(duckdb_conn):
# Loading into a normal DataFrame:
duckdb_conn.sql("SELECT * FROM hotels LIMIT 10").pl()
return
@app.cell
def _(duckdb_conn):
# Loading into a LazyFrame:
duckdb_conn.sql("SELECT * FROM hotels").pl(lazy=True).limit(10).collect()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
Note that this is very similar to SQL cells backed by DuckDB, with the biggest difference being that you can control how the result is consumed as opposed to it always being loaded into memory.
Many features such as querying from DataFrames work the same using DuckDB directly as they do in DuckDB-backed SQL Cells, and vice-versa
""")
return
@app.cell
def _(duckdb):
duckdb.sql("SELECT * FROM hotels").pl(lazy=True).sort("cleanliness_base", descending=True).limit(5).collect()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Utilities
""")
return
@app.cell(hide_code=True)
def _(mo):
delete_file_button = mo.ui.run_button(label="Delete cached file", kind="warn")
mo.vstack([mo.md("If you want to delete the downloaded file from your cache"), delete_file_button])
return (delete_file_button,)
@app.cell
def _(cached_file, delete_file_button, pathlib):
if delete_file_button.value:
pathlib.Path(cached_file).unlink()
return
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _():
import polars as pl
return (pl,)
@app.cell
def _():
import duckdb
return (duckdb,)
@app.cell
def _():
import sqlalchemy
return (sqlalchemy,)
@app.cell
def _():
import kagglehub
return (kagglehub,)
@app.cell
def _():
import pathlib
return (pathlib,)
if __name__ == "__main__":
app.run()
|