|
| 1 | +# ggsql |
| 2 | + |
| 3 | +Python bindings for [ggsql](https://github.com/georgestagg/ggsql), a SQL extension for declarative data visualization. |
| 4 | + |
| 5 | +This package provides a thin wrapper around the Rust `ggsql` crate, enabling Python users to render Vega-Lite visualizations from polars DataFrames using ggsql's VISUALISE syntax. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +### From PyPI (when published) |
| 10 | + |
| 11 | +```bash |
| 12 | +pip install ggsql |
| 13 | +``` |
| 14 | + |
| 15 | +### From source |
| 16 | + |
| 17 | +Building from source requires: |
| 18 | +- Rust toolchain (install via [rustup](https://rustup.rs/)) |
| 19 | +- Python 3.10+ |
| 20 | +- [maturin](https://github.com/PyO3/maturin) |
| 21 | + |
| 22 | +```bash |
| 23 | +# Clone the monorepo |
| 24 | +git clone https://github.com/georgestagg/ggsql.git |
| 25 | +cd ggsql/ggsql-python |
| 26 | + |
| 27 | +# Create a virtual environment |
| 28 | +python -m venv .venv |
| 29 | +source .venv/bin/activate # or `.venv\Scripts\activate` on Windows |
| 30 | + |
| 31 | +# Install build dependencies |
| 32 | +pip install maturin |
| 33 | + |
| 34 | +# Build and install in development mode |
| 35 | +maturin develop |
| 36 | + |
| 37 | +# Or build a wheel |
| 38 | +maturin build --release |
| 39 | +pip install target/wheels/ggsql-*.whl |
| 40 | +``` |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +```python |
| 45 | +import ggsql |
| 46 | +import polars as pl |
| 47 | + |
| 48 | +# Split a ggSQL query into SQL and VISUALISE portions |
| 49 | +sql, viz = ggsql.split_query(""" |
| 50 | + SELECT date, revenue, region FROM sales |
| 51 | + WHERE year = 2024 |
| 52 | + VISUALISE date AS x, revenue AS y, region AS color |
| 53 | + DRAW line |
| 54 | + LABEL title => 'Sales Trends' |
| 55 | +""") |
| 56 | + |
| 57 | +# Execute SQL with your preferred tool |
| 58 | +import duckdb |
| 59 | +df = duckdb.sql(sql).pl() |
| 60 | + |
| 61 | +# Render DataFrame + VISUALISE spec to Vega-Lite JSON |
| 62 | +vegalite_json = ggsql.render(df, viz) |
| 63 | +``` |
| 64 | + |
| 65 | +### Mapping styles |
| 66 | + |
| 67 | +The `render()` function supports various mapping styles: |
| 68 | + |
| 69 | +```python |
| 70 | +df = pl.DataFrame({"x": [1, 2, 3], "y": [10, 20, 30], "category": ["A", "B", "A"]}) |
| 71 | + |
| 72 | +# Explicit mapping |
| 73 | +ggsql.render(df, "VISUALISE x AS x, y AS y DRAW point") |
| 74 | + |
| 75 | +# Implicit mapping (column name = aesthetic name) |
| 76 | +ggsql.render(df, "VISUALISE x, y DRAW point") |
| 77 | + |
| 78 | +# Wildcard mapping (map all matching columns) |
| 79 | +ggsql.render(df, "VISUALISE * DRAW point") |
| 80 | + |
| 81 | +# With color encoding |
| 82 | +ggsql.render(df, "VISUALISE x, y, category AS color DRAW point") |
| 83 | +``` |
| 84 | + |
| 85 | +## API |
| 86 | + |
| 87 | +### `split_query(query: str) -> tuple[str, str]` |
| 88 | + |
| 89 | +Split a ggSQL query into SQL and VISUALISE portions. |
| 90 | + |
| 91 | +**Parameters:** |
| 92 | +- `query`: The full ggSQL query string |
| 93 | + |
| 94 | +**Returns:** |
| 95 | +- Tuple of `(sql_portion, visualise_portion)` |
| 96 | + |
| 97 | +**Raises:** |
| 98 | +- `ValueError`: If the query cannot be parsed |
| 99 | + |
| 100 | +### `render(df, viz, *, writer="vegalite") -> str` |
| 101 | + |
| 102 | +Render a DataFrame with a VISUALISE specification. |
| 103 | + |
| 104 | +**Parameters:** |
| 105 | +- `df`: A `polars.DataFrame` or `polars.LazyFrame` (LazyFrames are collected automatically) |
| 106 | +- `viz`: The VISUALISE specification string |
| 107 | +- `writer`: Output format, currently only `"vegalite"` is supported |
| 108 | + |
| 109 | +**Returns:** |
| 110 | +- JSON string containing the Vega-Lite specification |
| 111 | + |
| 112 | +**Raises:** |
| 113 | +- `ValueError`: If the spec cannot be parsed or rendered |
| 114 | + |
| 115 | +## Development |
| 116 | + |
| 117 | +### Keeping in sync with the monorepo |
| 118 | + |
| 119 | +The `ggsql-python` package is part of the [ggsql monorepo](https://github.com/georgestagg/ggsql) and depends on the Rust `ggsql` crate via a path dependency. When the Rust crate is updated, you may need to rebuild: |
| 120 | + |
| 121 | +```bash |
| 122 | +cd ggsql-python |
| 123 | + |
| 124 | +# Rebuild after Rust changes |
| 125 | +maturin develop |
| 126 | + |
| 127 | +# If tree-sitter grammar changed, clean and rebuild |
| 128 | +cd .. && cargo clean -p tree-sitter-ggsql && cd ggsql-python |
| 129 | +maturin develop |
| 130 | +``` |
| 131 | + |
| 132 | +### Running tests |
| 133 | + |
| 134 | +```bash |
| 135 | +# Install test dependencies |
| 136 | +pip install pytest altair |
| 137 | + |
| 138 | +# Run unit tests |
| 139 | +pytest tests/test_ggsql.py -v |
| 140 | + |
| 141 | +# Run E2E tests with altair |
| 142 | +pytest tests/test_altair_e2e.py -v |
| 143 | + |
| 144 | +# Run all tests |
| 145 | +pytest tests/ -v |
| 146 | +``` |
| 147 | + |
| 148 | +## Requirements |
| 149 | + |
| 150 | +- Python >= 3.10 |
| 151 | +- polars >= 1.0 |
| 152 | +- pyarrow >= 12 (required for Arrow FFI) |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +MIT |
0 commit comments