diff --git a/docs/guides/index.md b/docs/guides/index.md index 18b13e6..8b11d22 100644 --- a/docs/guides/index.md +++ b/docs/guides/index.md @@ -22,3 +22,4 @@ For detailed guides on using dbc, see the following pages: - [Finding Drivers](./finding_drivers.md) - [Using a Driver List](./driver_list.md) - [Installing a Driver Manager](./driver_manager.md) +- [Python Notebooks](./python_notebooks.md) diff --git a/docs/guides/python_notebooks.md b/docs/guides/python_notebooks.md new file mode 100644 index 0000000..ddcc135 --- /dev/null +++ b/docs/guides/python_notebooks.md @@ -0,0 +1,61 @@ + + +# Python Notebooks + +dbc can be installed and used directly in Python notebooks (such as [Jupyter](https://jupyter.org) or [Google Colab](https://colab.google)). +Each of the following code blocks is designed to be executed as an individual cell in your notebook. + +Install the `dbc`, `adbc-driver-manager`, and `pyarrow` packages: + +```python +%pip install dbc adbc_driver_manager pyarrow +``` + +Install the `duckdb` driver: + +```python +!dbc install duckdb +``` + +!!! note + + This guide uses the DuckDB driver for simplicity. + To list all available drivers, run `!dbc search`. + +Import the `dbapi` module: + +```python +from adbc_driver_manager import dbapi +``` + +Connect to a database via ADBC, create a cursor, execute queries, and fetch the result as a PyArrow Table: + +```python +with ( + dbapi.connect(driver="duckdb") as con, + con.cursor() as cursor, +): + cursor.execute("CREATE TABLE IF NOT EXISTS penguins AS FROM 'https://blobs.duckdb.org/data/penguins.csv'") + cursor.execute("SELECT * FROM penguins") + table = cursor.fetch_arrow_table() +``` + +Print the table: + +```python +print(table) +``` diff --git a/mkdocs.yml b/mkdocs.yml index 22ad58d..193763a 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -44,6 +44,7 @@ nav: - Finding Drivers: guides/finding_drivers.md - Using a Driver List: guides/driver_list.md - Driver Managers: guides/driver_manager.md + - Python Notebooks: guides/python_notebooks.md - Concepts: - concepts/index.md - Driver: concepts/driver.md