-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathparametrizing_sql_queries.py
127 lines (95 loc) · 2.85 KB
/
parametrizing_sql_queries.py
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
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "vega-datasets==0.9.0",
# ]
# ///
import marimo
__generated_with = "0.9.1"
app = marimo.App(width="medium")
@app.cell
def __():
import marimo as mo
return (mo,)
@app.cell(hide_code=True)
def __(mo):
mo.md(
"""
# Parametrizing SQL Queries
This notebook shows parametrize SQL queries with Python values, using Python f-string interpolation.
First, we create a dataframe called `df`.
"""
)
return
@app.cell
def __():
from vega_datasets import data
df = data.iris()
df
return data, df
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""Next, we create a dropdown that selects the iris species.""")
return
@app.cell
def __(mo):
species_dropdown = mo.ui.dropdown(["setosa", "versicolor", "virginica"], value="setosa")
species_dropdown
return (species_dropdown,)
@app.cell(hide_code=True)
def __(mo):
mo.md(
"""
Next, we **create a SQL cell** that filters the table to the selected species.
Notice that we can reference the Python variable `species_dropdown` in our query
using **curly braces**. This is because marimo represents SQL queries as Python
f-strings.
"""
)
return
@app.cell(hide_code=True)
def __(mo):
mo.accordion(
{
"Tip: Creating SQL Cells": mo.md(
f"""
Create a SQL cell in one of two ways:
1. Click the {mo.icon("lucide:database")} `SQL` button at the **bottom of your notebook**
2. **Right-click** the {mo.icon("lucide:circle-plus")} button to the **left of a cell**, and choose `SQL`.
In the SQL cell, you can query dataframes in your notebook as if
they were tables — just reference them by name.
"""
)
}
)
return
@app.cell
def __(df, mo, species_dropdown):
result = mo.sql(
f"""
SELECT * FROM df where species == '{species_dropdown.value}'
""", output=False
)
return (result,)
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
The query output is returned to Python as a dataframe (Polars if you have it installed, Pandas otherwise).
Choose the dataframe name via the **output variable** input in the bottom-left
of the cell. If the name starts with an underscore, it won't be made available
to other cells. In this case, we've named the output `result`.
Try changing the selected species in the `species_dropdown`, and watch how the query result changes.
"""
)
return
@app.cell
def __(result):
result
return
if __name__ == "__main__":
app.run()