CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-shillelagh

Making it easy to query APIs via SQL

Pending
Overview
Eval results
Files

cli-interface.mddocs/

CLI Interface

Interactive SQL shell with syntax highlighting, completion, and configuration support for querying data sources from the command line. Provides a user-friendly interface for exploring and analyzing data using SQL.

Capabilities

Command Line Application

Interactive SQL shell accessible via the shillelagh command with rich terminal features.

def main():
    """
    Main CLI entry point.
    
    Starts interactive SQL shell with:
    - Syntax highlighting for SQL queries
    - Auto-completion for SQL keywords and functions
    - Command history with persistent storage
    - Configuration file support
    - Tabular result formatting
    """

Configuration Management

YAML-based configuration system for adapter settings and connection parameters.

# Configuration file: shillelagh.yaml
# Location: User config directory (varies by OS)
#   - Linux: ~/.config/shillelagh/shillelagh.yaml
#   - macOS: ~/Library/Application Support/shillelagh/shillelagh.yaml  
#   - Windows: %APPDATA%\shillelagh\shillelagh.yaml

# Configuration structure:
# adapter_name:
#   setting1: value1
#   setting2: value2
#   catalog:
#     alias1: url1
#     alias2: url2

Interactive Features

Rich terminal interface with advanced features for SQL query development and data exploration.

# Interactive features:
# - SQL syntax highlighting using Pygments
# - Keyword auto-completion  
# - Command history with search
# - Multi-line query support
# - Result pagination and formatting
# - Error handling and display

Usage Examples

Basic CLI Usage

# Start interactive shell
$ shillelagh

# Shell prompt with SQL highlighting
shillelagh> SELECT * FROM 'data.csv' LIMIT 5;

# Results displayed in table format
┌────┬─────────┬─────┬────────┐
│ id │ name    │ age │ city   │
├────┼─────────┼─────┼────────┤
│ 1  │ Alice   │ 25  │ NYC    │
│ 2  │ Bob     │ 30  │ LA     │
│ 3  │ Charlie │ 35  │ Chicago│
└────┴─────────┴─────┴────────┘

shillelagh>

Configuration File Setup

Create configuration file for adapter settings:

# ~/.config/shillelagh/shillelagh.yaml

# Google Sheets API configuration
gsheetsapi:
  service_account_file: /path/to/credentials.json
  subject: user@example.com
  catalog:
    # Define aliases for commonly used sheets
    sales_data: https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
    inventory: https://docs.google.com/spreadsheets/d/1AbCdEfGhIjKlMnOpQrStUvWxYz/edit

# GitHub API configuration  
githubapi:
  token: ghp_your_github_token_here
  catalog:
    my_repos: https://api.github.com/users/myusername/repos
    org_repos: https://api.github.com/orgs/myorg/repos

# Weather API configuration
weatherapi:
  api_key: your_weather_api_key_here

# Generic JSON API configuration
genericjsonapi:
  headers:
    User-Agent: "ShillelaghCLI/1.0"
    Accept: "application/json"

Using Aliases

# With configuration file aliases, use short names instead of full URLs
shillelagh> SELECT * FROM sales_data WHERE amount > 1000;

shillelagh> SELECT name, stargazers_count FROM my_repos ORDER BY stargazers_count DESC;

shillelagh> SELECT city, temperature FROM weather WHERE temperature > 30;

Advanced Query Examples

# Multi-source join query
shillelagh> SELECT 
    u.name,
    d.department_name,
    s.total_sales
FROM 'https://api.company.com/users' u
JOIN './departments.csv' d ON u.dept_id = d.id  
JOIN sales_data s ON u.id = s.user_id
WHERE s.total_sales > 10000
ORDER BY s.total_sales DESC;

# Aggregate analysis
shillelagh> SELECT 
    DATE(created_at) as date,
    COUNT(*) as issues_created,
    AVG(CASE WHEN state = 'closed' THEN 1 ELSE 0 END) as close_rate
FROM 'https://api.github.com/repos/owner/repo/issues'
WHERE created_at >= '2023-01-01'
GROUP BY DATE(created_at)
ORDER BY date;

# Data export to CSV
shillelagh> SELECT * FROM 'https://api.example.com/data' 
WHERE category = 'important'
-- Results can be piped to file: shillelagh < query.sql > output.csv

Error Handling and Help

# SQL syntax errors are highlighted
shillelagh> SELCT * FROM data.csv;
Error: SQL syntax error near 'SELCT'

# Adapter-specific errors
shillelagh> SELECT * FROM 'https://invalid-url.com/data.json';
Error: Failed to connect to data source
Details: Connection timeout after 30 seconds

# Built-in help and information
shillelagh> .help
Available commands:
  .help     - Show this help message
  .exit     - Exit the shell
  .version  - Show version information
  .adapters - List available adapters

shillelagh> .adapters
Available adapters:
  - csvfile: CSV file adapter
  - gsheetsapi: Google Sheets API adapter  
  - githubapi: GitHub API adapter
  - genericjsonapi: Generic JSON API adapter
  - htmltableapi: HTML table scraper
  [... more adapters ...]

Command History and Navigation

# Command history persisted between sessions
shillelagh> SELECT COUNT(*) FROM data.csv;
# Press Up arrow to recall previous commands
# Press Ctrl+R to search command history

# Multi-line query support
shillelagh> SELECT 
         >   name,
         >   age,
         >   city
         > FROM users
         > WHERE age > 25;

Performance and Debugging

# Query timing information
shillelagh> SELECT COUNT(*) FROM 'https://large-api.com/data';
┌──────────┐
│ COUNT(*) │
├──────────┤
│ 150000   │
└──────────┘
Query executed in 2.34 seconds

# Verbose mode for debugging
shillelagh> .verbose on
Verbose mode enabled

shillelagh> SELECT * FROM data.csv LIMIT 1;
[DEBUG] Loading adapter: CSVFile
[DEBUG] Parsing URI: data.csv  
[DEBUG] Executing query: SELECT * FROM data.csv LIMIT 1
[DEBUG] Adapter returned 1 rows in 0.05 seconds
┌────┬──────┬─────┐
│ id │ name │ age │
├────┼──────┼─────┤
│ 1  │ John │ 30  │
└────┴──────┴─────┘

Installation and Setup

Installing CLI Dependencies

# Install shillelagh with CLI support
pip install shillelagh[console]

# Or install all optional dependencies
pip install shillelagh[all]

First Run Setup

# Create initial configuration
$ shillelagh --setup

# This creates:
# - Configuration directory
# - Default configuration file
# - Command history file
# - Cache directory for adapter data

Configuration directory created at: ~/.config/shillelagh/
Edit shillelagh.yaml to configure adapters.

# Start using the CLI
$ shillelagh
Welcome to Shillelagh SQL shell
Type .help for help, .exit to quit

shillelagh>

Environment Variables

# Override configuration file location
export SHILLELAGH_CONFIG=/path/to/custom/config.yaml

# Set cache directory
export SHILLELAGH_CACHE_DIR=/path/to/cache

# Enable debug logging
export SHILLELAGH_LOG_LEVEL=DEBUG

# Run with environment settings
shillelagh

Install with Tessl CLI

npx tessl i tessl/pypi-shillelagh

docs

adapters.md

cli-interface.md

database-api.md

index.md

sqlalchemy-integration.md

type-system.md

tile.json