Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
psychemedia committed Feb 12, 2025
2 parents ae9c121 + 8c6a662 commit 1e9504a
Showing 1 changed file with 49 additions and 47 deletions.
96 changes: 49 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Jupyter [`anywidget`](https://anywidget.dev/) and magic for working with [`pglite`](https://github.com/electric-sql/pglite) (single user postgres wasm build) (provides access to a single PostgreSQL db instance running solely in the the browser).

[Try it in JupyterLite]( https://innovationoutside.github.io/jupyter_anywidget_pglite/) (in pyodide kernel, `%pip install anywidget==0.9.13 jupyter_anywidget_pglite`)

Install from PyPi as: `pip install jupyter_anywidget_pglite`

![Example of usage for pglite anywidget and magic](images/pglite_anywidget_magic.png)
Expand Down Expand Up @@ -45,6 +47,53 @@ To persist the database in browser storage, set the `idb='DBNAME` parameter when

`pg_headless_persist = pglite_headless(idb="pglitetest1")`

### Running queries using magics

To run a query, place the query insde a `%%pglite` cell block magic.

- use the `-w / --widget-name` setting to set the widget within the magic and it does not need to be passed again (for example, `%%pglite_magic -w pg`)
- alternatively, prior to calling the block magic, set the widget used in the magic via a line magic: `%setwidget pg`

Running queries on the database using IPython cell block magic `%%pglite -w WIDGET_VARIABLE`:

```python
%%pglite_magic -w pg
CREATE TABLE IF NOT EXISTS test (
id serial primary key,
title varchar not null
);

#----
%%pglite_magic
INSERT INTO test (title) VALUES ('dummy');

#----
%%pglite_magic
SELECT * FROM test;

```

To run multiple SQL statements in the same cell:

- use the `-m / --multiple-statements` flag (default: `False`) when calling the cell block magic [NOT RECOMMENDED]. This will naively split the query on each `;` character, and then run each split item as a separate command. The response will be set to the response from the final query;
- use the `-M / --multiple-statement-block` flag to run all the tems using the `pglite` `.exec()` command.

We can also run queries (with the same arguments) using the `%pglite_query` line magic, with the query set via the `-q / --query` parameter:

`%pglite_query -r -q 'SELECT * FROM test LIMIT 1;'`

Add the `-d / --dataframe` flag to return the query result as a dataframe (not JupyterLite).

Having made a query onto the database via a magic cell, we can retrieve the response:

```python
pg.response
```

If `pandas` is installed, we can get rows returned from a query response as a dataframe:

`pg.df()`

## Running queries

We run a query by setting query state on the widget. The following Python function helps with that:
Expand Down Expand Up @@ -98,53 +147,6 @@ conn2 = create_engine(pg_headless)
pd.read_sql("SELECT * FROM test;", conn2)
```

### Running queries using magics

To run a query, place the query insde a `%%pglite` cell block magic.

- use the `-w / --widget-name` setting to set the widget within the magic and it does not need to be passed again (for example, `%%pglite_magic -w pg`)
- alternatively, prior to calling the block magic, set the widget used in the magic via a line magic: `%setwidget pg`

Running queries on the database using IPython cell block magic `%%pglite -w WIDGET_VARIABLE`:

```python
%%pglite_magic -w pg
CREATE TABLE IF NOT EXISTS test (
id serial primary key,
title varchar not null
);

#----
%%pglite_magic
INSERT INTO test (title) VALUES ('dummy');

#----
%%pglite_magic
SELECT * FROM test;

```

To run multiple SQL statements in the same cell:

- use the `-m / --multiple-statements` flag (default: `False`) when calling the cell block magic [NOT RECOMMENDED]. This will naively split the query on each `;` character, and then run each split item as a separate command. The response will be set to the response from the final query;
- use the `-M / --multiple-statement-block` flag to run all the tems using the `pglite` `.exec()` command.

We can also run queries (with the same arguments) using the `%pglite_query` line magic, with the query set via the `-q / --query` parameter:

`%pglite_query -r -q 'SELECT * FROM test LIMIT 1;'`

Add the `-d / --dataframe` flag to return the query result as a dataframe (not JupyterLite).

Having made a query onto the database via a magic cell, we can retrieve the response:

```python
pg.response
```

If `pandas` is installed, we can get rows returned from a query response as a dataframe:

`pg.df()`

### Blocking

Note that the `pglite` query runs asynchronously, so how do we know on the Python side when the response is ready?
Expand Down

0 comments on commit 1e9504a

Please sign in to comment.