Jupyter
Original docs at <https://pyprql.readthedocs.io/en/latest/magic_readme.html>
Work with pandas and PRQL in an IPython terminal or Jupyter notebook.
Implementation
This is a thin wrapper around the fantastic IPython-sql magic. Roughly speaking, all we do is parse PRQL to SQL and pass that through to ipython-sql. A full documentation of the supported features is available at their repository. Here, we document those places where we differ from them, plus those features we think you are mostly likely to find useful.
Usage
Installation
If you have already installed PyPRQL into your environment, then you should be could to go! We bundle in IPython and pandas, though you’ll need to install Jupyter separately. If you haven’t installed PyPRQL, that’s as simple as:
1. pip install pyprql
Set up
Open up either an IPython terminal or Jupyter notebook. First, we need to load the extension and connect to a database.
1. In [1]: %load_ext pyprql.magic
Connecting a database
We have two options for connecting a database
- Create an in-memory DB. This is the easiest way to get started.
1. In [2]: %prql duckdb:///:memory:
However, in-memory databases start off empty! So, we need to add some data. We have a two options:
-
We can easily add a pandas dataframe to the
DuckDBdatabase like so:1. In [3]: %prql --persist dfwhere
dfis a pandas dataframe. This adds a table nameddfto the in-memoryDuckDBinstance. - Or download a CSV and query it directly, with DuckDB:1. !wget https://github.com/graphql-compose/graphql-compose-examples/blob/master/examples/northwind/data/csv/products.csv…and then
from products.csvwill work. 2. Connect to an existing database
When connecting to a database, pass the connection string as an argument to the line magic %prql. The connection string needs to be in SQLAlchemy format, so any connection supported by SQLAlchemy is supported by the magic. Additional connection parameters can be passed as a dictionary using the --connection_arguments flag to the the %prql line magic. We ship with the necessary extensions to use DuckDB as the backend, and here connect to an in-memory database.
Querying
Now, let’s do a query! By default, PRQLMagic always returns the results as dataframe, and always prints the results. The results of the previous query are accessible in the _ variable.
These examples are based on the products.csv example above.
3. In [4]: %%prql
4. ...: from p = products.csv
5. ...: filter supplierID == 1
7. Done.
8. Returning data to local variable _
9. productID productName supplierID categoryID quantityPerUnit unitPrice unitsInStock unitsOnOrder reorderLevel discontinued
10. 0 1 Chai 1 1 10 boxes x 20 bags 18.0 39 0 10 0
11. 1 2 Chang 1 1 24 - 12 oz bottles 19.0 17 40 25 0
12. 2 3 Aniseed Syrup 1 2 12 - 550 ml bottles 10.0 13 70 25 0
1. In [5]: %%prql
2. ...: from p = products.csv
3. ...: group categoryID (
4. ...: aggregate [average unitPrice]
5. ...: )
7. Done.
8. Returning data to local variable _
9. categoryID avg("unitPrice")
10. 0 1 37.979167
11. 1 2 23.062500
12. 2 7 32.370000
13. 3 6 54.006667
14. 4 8 20.682500
15. 5 4 28.730000
16. 6 3 25.160000
17. 7 5 20.250000
We can capture the results into a different variable like so:
1. In [6]: %%prql results <<
2. ...: from p = products.csv
3. ...: aggregate [min unitsInStock, max unitsInStock]
5. Done.
6. Returning data to local variable results
7. min("unitsInStock") max("unitsInStock")
8. 0 0 125
Now, the output of the query is saved to results.
