Skip to content

Commit

Permalink
docs: python binding (databendlabs#12680)
Browse files Browse the repository at this point in the history
* added

* Update jupyter.md
  • Loading branch information
soyeric128 authored Sep 5, 2023
1 parent dc09e17 commit d1e6079
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
97 changes: 89 additions & 8 deletions docs/doc/13-integrations/jupyter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ title: Jupyter Notebook

[Jupyter Notebook](https://jupyter.org) is a web-based interactive application that enables you to create notebook documents that feature live code, interactive plots, widgets, equations, images, etc., and share these documents easily. It is also quite versatile as it can support many programming languages via kernels such as Julia, Python, Ruby, Scala, Haskell, and R.

With the SQLAlchemy library in Python or [ipython-sql](https://github.com/catherinedevlin/ipython-sql), you can establish a connection to Databend within a Jupyter Notebook, allowing you to execute queries and visualize your data from Databend directly in the Notebook.
With the SQLAlchemy library in Python or [ipython-sql](https://github.com/catherinedevlin/ipython-sql), you can establish a connection to Databend within a Jupyter Notebook, allowing you to execute queries and visualize your data from Databend directly in the notebook. Alternatively, you can run SQL queries in Python using the [Databend Python Binding](https://pypi.org/project/databend/) library, allowing you to harness DataBend's capabilities directly within your local Python environment or online services like Jupyter Notebook and Google Colab without the need to deploy a separate DataBend instance.

## Tutorial: Integrate with Jupyter Notebook using SQLAlchemy
## Tutorial-1: Integrate with Jupyter Notebook using SQLAlchemy

In this tutorial, you will first deploy a local Databend instance and Jupyter Notebook, and then run a sample notebook to connect to your local Databend through the SQLAlchemy library, as well as write and visualize data within the notebook.

Expand Down Expand Up @@ -59,7 +59,7 @@ jupyter notebook

![Alt text](../../public/img/integration/integration-gui-jupyter.png)

## Tutorial: Integrate with Jupyter Notebook using ipython-sql
## Tutorial-2: Integrate with Jupyter Notebook using ipython-sql

In this tutorial, you will first deploy a local Databend instance and Jupyter Notebook, and then run a sample notebook to connect to your local Databend through [ipython-sql](https://github.com/catherinedevlin/ipython-sql), as well as write and visualize data within the notebook.

Expand Down Expand Up @@ -110,11 +110,11 @@ jupyter notebook

3. Run the following code sequentially in separate cells. By doing so, you create a table containing 5 rows in your local Databend, and visualize the data with a bar chart.

```python
```python title='In [1]:'
%load_ext sql
```

```sql
```sql title='In [2]:'
%%sql databend://user1:abc123@localhost:8000/default
create table if not exists user(created_at Date, count Int32);
insert into user values('2022-04-01', 5);
Expand All @@ -124,17 +124,98 @@ insert into user values('2022-04-03', 1);
insert into user values('2022-04-04', 10);
```

```python
```python title='In [3]:'
result = %sql select created_at as date, count(*) as count from user group by created_at;
result
```

```python
```python title='In [4]:'
%matplotlib inline

df = result.DataFrame()
df.plot.bar(x='date', y='count')
```

You can now see a bar chart on the notebook:

![Alt text](../../public/img/integration/jupyter-ipython-sql.png)

## Tutorial-3: Integrate with Jupyter Notebook with Python Binding Library

In this tutorial, you will first deploy a local Databend instance and Jupyter Notebook, and then run queries in a notebook through the [Databend Python Binding](https://pypi.org/project/databend/) library, as well as write and visualize data within the notebook.

Before you start, ensure that you have [Python](https://www.python.org/) installed on your system.

### Step 1. Deploy Jupyter Notebook

1. Install Jupyter Notebook with pip:

```shell
pip install notebook
```

2. Install dependencies with pip:

```shell
pip install databend
pip install matplotlib
```
### Step 2. Create a Notebook

1. Run the command below to start Jupyter Notebook:

```shell
jupyter notebook
```

This will start up Jupyter and your default browser should start (or open a new tab) to the following URL: http://localhost:8888/tree

![Alt text](../../public/img/integration/notebook-tree.png)

2. Select **New** > **Python 3** to create a notebook.

3. Run the following code sequentially in separate cells:

```python title='In [1]:'
# Import the necessary libraries
from databend import SessionContext

# Create a DataBend session
ctx = SessionContext()
```

```python title='In [2]:'
# Create a table in DataBend
ctx.sql("CREATE TABLE IF NOT EXISTS user (created_at Date, count Int32)")
```

```python title='In [3]:'
# Insert multiple rows of data into the table
ctx.sql("INSERT INTO user VALUES ('2022-04-01', 5), ('2022-04-01', 3), ('2022-04-03', 4), ('2022-04-03', 1), ('2022-04-04', 10)")
```

```python title='In [4]:'
# Execute a query
result = ctx.sql("SELECT created_at as date, count(*) as count FROM user GROUP BY created_at")

# Display the query result
result.show()
```

```python title='In [5]:'
# Import libraries for data visualization
import matplotlib.pyplot as plt

# Convert the query result to a Pandas DataFrame
df = result.to_pandas()
```

```python title='In [6]:'
# Create a bar chart to visualize the data
df.plot.bar(x='date', y='count')
plt.show()
```

You can now see a bar chart on the notebook:

![Alt text](../../public/img/integration/jupyter-ipython-sql.png)
![Alt text](../../public/img/integration/localhost_8888_notebooks_Untitled.ipynb.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d1e6079

Please sign in to comment.