Skip to main content

Natural Language to SQL Query System with Visualization

Project description

🚀 SUTRA - Simple Unified Tool for Relational Analysis

Natural Language to SQL Query System with Auto-Visualization

A single-file Python library that converts your questions into SQL queries and visualizes the results automatically.


✨ What You Get

One file - Everything in sutra.py
Natural language - Ask questions in plain English
Multiple formats - CSV, Excel, JSON, SQL, DataFrames
Direct SQL - No API cost option
Auto visualization - Charts with one parameter
Interactive mode - Ask user for viz choice
Jupyter ready - Perfect for notebooks


📦 Installation

pip install sutra

🎯 Complete Workflow (9 Steps)

Step 1: Install

pip install sutra

Step 2: Import

from sutra import SUTRA

Step 3: Enter API Key

sutra = SUTRA(api_key="your-openai-api-key")

Step 4: Upload Data (Any Format!)

# CSV
sutra.upload("data.csv")

# Excel
sutra.upload("data.xlsx")

# DataFrame
import pandas as pd
df = pd.DataFrame({'sales': [100, 200], 'region': ['North', 'South']})
sutra.upload(df, name="sales_data")

# JSON
sutra.upload("data.json")

Step 5: Database Auto-Created

Database is automatically created and managed!

Step 6: Direct SQL (No API Cost!)

result = sutra.sql("SELECT * FROM sales_data WHERE sales > 150")
print(result.data)

Step 7: Natural Language Queries

result = sutra.ask("What are total sales by region?")
print(result.data)

Step 8: With Visualization

# Set viz=True for automatic charts
result = sutra.ask("Show sales by region", viz=True)

# Or ask user interactively
result = sutra.interactive("Show sales by region")
# Prompts: "Do you want visualization? (yes/no):"

Step 9: Works in Jupyter Notebooks!

See SUTRA_Complete_Guide.ipynb for step-by-step examples.


📖 Quick Examples

Basic Usage

from sutra import SUTRA

# Initialize
sutra = SUTRA(api_key="sk-...")

# Upload data
sutra.upload("sales.csv")

# Ask questions
result = sutra.ask("What are total sales?")
print(result.data)

With Visualization

result = sutra.ask("Show top 10 products by revenue", viz=True)
# Chart appears automatically!
print(result.data)

Interactive Mode

result = sutra.interactive("What are sales trends?")
# Asks: "Do you want visualization? (yes/no):"

Direct SQL (No API Cost)

result = sutra.sql("SELECT region, SUM(sales) FROM data GROUP BY region")
print(result.data)

Export Results

result = sutra.ask("Show all data")
sutra.export(result.data, "output.csv")
sutra.export(result.data, "output.xlsx", format="excel")

📊 Jupyter Notebook Guide

Open SUTRA_Complete_Guide.ipynb for a complete step-by-step tutorial with:

  • All 9 steps explained
  • Multiple examples
  • Interactive queries
  • Visualization options
  • Export examples

🛠️ API Reference

Main Methods

# Initialize
sutra = SUTRA(api_key="...", db="mydb.db")

# Upload data
sutra.upload(data, name="table_name")  # data = file path or DataFrame

# View database
sutra.tables()           # List all tables
sutra.schema()           # Show schema
sutra.peek(n=10)        # Preview data

# Query with SQL
result = sutra.sql("SELECT ...", viz=False)

# Query with natural language
result = sutra.ask("question", viz=False, table="table_name")

# Interactive query
result = sutra.interactive("question")  # Prompts for viz choice

# Export
sutra.export(data, "file.csv", format="csv")  # or "excel", "json"

# Close
sutra.close()

QueryResult Object

result.success  # bool
result.sql      # Generated SQL query
result.data     # pandas DataFrame
result.viz      # Visualization object
result.error    # Error message (if failed)

🎨 Features

Feature Description
Single File Everything in one sutra.py
Multi-Format CSV, Excel, JSON, SQL, DataFrame
No API Mode Use .sql() for free queries
NLP Mode Use .ask() with OpenAI
Auto Viz Set viz=True for charts
Interactive .interactive() asks user
Caching Saves API costs
Export CSV, Excel, JSON
Jupyter Perfect for notebooks

💰 Cost Saving

Use direct SQL to save API costs:

# FREE - No API call
result = sutra.sql("SELECT * FROM data WHERE amount > 1000")

# USES API - Costs ~$0.001 per query
result = sutra.ask("Show data where amount is greater than 1000")

📝 Publishing to PyPI

See PUBLISHING_GUIDE.md for complete instructions.

Quick version:

# Install tools
pip install build twine

# Build
python -m build

# Upload
python -m twine upload dist/*

🧪 Testing

Run the test script:

python test_package.py

📚 Files Included

  • sutra/sutra.py - Main single-file library
  • sutra/__init__.py - Package initialization
  • SUTRA_Complete_Guide.ipynb - Jupyter notebook tutorial
  • setup.py - Package configuration
  • pyproject.toml - Modern Python packaging
  • README.md - This file
  • PUBLISHING_GUIDE.md - How to publish
  • LICENSE - MIT License

🤝 Contributing

Contributions welcome! This is a single-file library, so it's easy to modify and extend.


📄 License

MIT License - feel free to use in your projects!


🎓 Examples

Example 1: Quick Analysis

from sutra import SUTRA

sutra = SUTRA(api_key="sk-...")
sutra.upload("sales.csv")
result = sutra.ask("What region has highest sales?", viz=True)
print(result.data)

Example 2: Multiple Queries

questions = [
    "What are total sales?",
    "Show top 5 products",
    "Which region performs best?"
]

for q in questions:
    result = sutra.interactive(q)  # Asks for viz each time
    if result.success:
        print(result.data)

Example 3: Context Manager

with SUTRA(api_key="sk-...") as sutra:
    sutra.upload(df)
    result = sutra.ask("Show all", viz=True)
# Automatically closes

❓ FAQ

Q: Do I need an API key?
A: Only for natural language queries (.ask()). Direct SQL (.sql()) works without API key.

Q: What formats are supported?
A: CSV, Excel (.xlsx, .xls), JSON, SQL files, and pandas DataFrames.

Q: How do I visualize?
A: Set viz=True in .ask() or .sql() methods, or use .interactive() to prompt user.

Q: Can I use without Jupyter?
A: Yes! Works in any Python environment - scripts, notebooks, REPL.

Q: Is it a single file?
A: Yes! The core is in sutra/sutra.py - one file with everything.


🚀 Get Started Now!

pip install sutra
from sutra import SUTRA

sutra = SUTRA(api_key="your-key")
sutra.upload("data.csv")
result = sutra.ask("Show me insights", viz=True)

That's it! You're analyzing data with natural language! 🎉


Made with ❤️ by Aditya Batta

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

querysutra-0.1.0.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

querysutra-0.1.0-py3-none-any.whl (43.1 kB view details)

Uploaded Python 3

File details

Details for the file querysutra-0.1.0.tar.gz.

File metadata

  • Download URL: querysutra-0.1.0.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for querysutra-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d6ecbf5e1fd190d07e8014e5081aa260abb6a4c45a7f52186407847b94a7baa4
MD5 05b6a5cfa01d75a50e00a2d059a57117
BLAKE2b-256 9b4446931e19ecd80c7e80091c7adadb4b3d30c3376ac06644f4409983266de6

See more details on using hashes here.

File details

Details for the file querysutra-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: querysutra-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 43.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for querysutra-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68afafa43e500e399e47e284cade2e676f6a3f012b61f1dde69737c7f654e9e5
MD5 14dc8179c238957456c59095b4a293e1
BLAKE2b-256 860cd36f7918996aabfe5d107b048ecd08a7ae34bd85feb90c472b38582f1403

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page