Skip to main content

Natural Language to SQL with MySQL/PostgreSQL export - Supports CSV, Excel, JSON, SQL, PDF, DOCX, TXT

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.2.tar.gz (41.8 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.2-py3-none-any.whl (44.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: querysutra-0.1.2.tar.gz
  • Upload date:
  • Size: 41.8 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.2.tar.gz
Algorithm Hash digest
SHA256 c68fe4e696bad5ff182b1c278df30afd05cac2539cf3fea4e4cb2f760f1bdf51
MD5 3cf283e59c34292c9c085efd7b3be0bc
BLAKE2b-256 29bb3909fbbe5ef139a327087928785516b75d537eab4a716d37ea978b482be5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: querysutra-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 44.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e8e30128db4eada39c0b70f081bc85e6c896e5f858de0d682ec8e9343c035f2a
MD5 4a096443e4fb143eee40030bc2f4d5ee
BLAKE2b-256 4cd9fc11770f33f4a6a013642980357dc4976792ae0ebca973ce25fd7e1a37c2

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