forked from catherinedevlin/ipython-sql
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for parameterizing queries using
{{variable}}
(#137)
- Loading branch information
1 parent
bed8b36
commit 6c31651
Showing
8 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
jupytext: | ||
text_representation: | ||
extension: .md | ||
format_name: myst | ||
format_version: 0.13 | ||
jupytext_version: 1.14.5 | ||
kernelspec: | ||
display_name: Python 3 (ipykernel) | ||
language: python | ||
name: python3 | ||
--- | ||
|
||
# Template | ||
|
||
## Variable Expansion as `{{variable}}` | ||
|
||
We support the variable expansion in the form of `{{variable}}`, this also allows the user to write the query as template with some dynamic variables | ||
|
||
```{code-cell} ipython3 | ||
:tags: [remove-cell] | ||
%load_ext sql | ||
from pathlib import Path | ||
from urllib.request import urlretrieve | ||
if not Path("penguins.csv").is_file(): | ||
urlretrieve( | ||
"https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv", | ||
"penguins.csv", | ||
) | ||
%sql duckdb:// | ||
``` | ||
|
||
Now, let's give a simple query template and define some variables where we will apply in the template: | ||
|
||
```{code-cell} ipython3 | ||
dynamic_limit = 5 | ||
dynamic_column = "island, sex" | ||
``` | ||
|
||
```{code-cell} ipython3 | ||
%sql SELECT {{dynamic_column}} FROM penguins.csv LIMIT {{dynamic_limit}} | ||
``` | ||
|
||
Note that variables will be fetched from the local namespace into the SQL statement. | ||
|
||
Please aware that we also support the `$variable` or `{variable_name}` way, but those will be deprecated in future version, [see more](https://jupysql.ploomber.io/en/latest/intro.html?highlight=variable#variable-substitution). | ||
|
||
```{code-cell} ipython3 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters