Skip to main content

Python client for XplainDB

Project description

XplainDB-Client - The Python Client for the XplainDB

PyPI version Python Version License: LGPL v3

XplainDB-client is the official Python client for the XplainDB. It provides a simple, intuitive, and powerful interface for interacting with a XplainDB server, handling everything from database creation and security management to complex multi-model queries.

This client abstracts away the complexity of HTTP requests, allowing you to work with your SQL, NoSQL, Graph, and Vector data using clean, Pythonic code.


Account Creation:

Get your account created at XplainDB and in Dashboard, Check for the Tenant Domain This is your BASE_URL.

Installation

Install the client library directly from PyPI (once published) or locally from your project.

# Install using pip
pip install xplaindb-client

📖 Getting Started: A Complete Walkthrough

This guide will walk you through creating a new database, managing its security, and using all four of its powerful data models.

Step 1: Connecting and Creating a Database

The first step is always to connect to the XplainDB server and bootstrap your database. The XplainDBClient.create_db() class method handles this in one simple command. It will create the database if it doesn't exist and retrieve the root admin key.

from xplaindb_client import XplainDBClient

# Configuration
BASE_URL = "http://127.0.0.1:8000"
DB_NAME = "my_new_application"

# Connect to the server. This creates 'my_new_application.XplainDB' and gets the admin key.
try:
    admin_client = XplainDBClient.create_db(base_url=BASE_URL, db_name=DB_NAME)
    #admin_client = XplainDBClient(base_url=BASE_URL, db_name=DB_NAME, api_key=API_KEY) if the database #already exists
    print("✅ Successfully connected as admin!")
    # In a real app, you would now save this key securely.
    my_admin_key = admin_client.api_key
except ConnectionError as e:
    print(f"❌ Error: {e}")

Step 2: Access Management (RBAC)

With your admin_client, you can now create less-privileged keys for different parts of your application. This is a critical security practice.

# Create a 'writer' key for your application's backend
writer_key_info = admin_client.create_api_key(permissions="writer")
writer_key = writer_key_info.get("api_key")
print(f"Created 'writer' key: {writer_key[:8]}...")

# Create a 'reader' key for an analytics dashboard
reader_key_info = admin_client.create_api_key(permissions="reader")
reader_key = reader_key_info.get("api_key")
print(f"Created 'reader' key: {reader_key[:8]}...")

# Now, create new clients to act as these roles
writer_client = XplainDBClient(base_url=BASE_URL, db_name=DB_NAME, api_key=writer_key)
reader_client = XplainDBClient(base_url=BASE_URL, db_name=DB_NAME, api_key=reader_key)

Step 3: Using the Relational (SQL) Model

Use the .sql() method to execute any standard SQL command. This is ideal for your structured, transactional data.

# The writer can create tables and insert data
writer_client.sql("CREATE TABLE IF NOT EXISTS customers (id INT, name TEXT, city TEXT)")
writer_client.sql("INSERT INTO customers VALUES (1, 'Alice', 'New York')")
print("\n--- SQL Operations ---")
print("✅ Writer added a customer.")

# The reader can select data
customers = reader_client.sql("SELECT * FROM customers WHERE city = 'New York'")
print(f"   Reader found customers: {customers}")

# The reader CANNOT write data (this would raise a ConnectionError: 403 Forbidden)
try:
    reader_client.sql("INSERT INTO customers VALUES (2, 'Bob', 'London')")
except ConnectionError as e:
    print("✅ Reader was correctly denied write access.")

Step 4: Using the Document (NoSQL) Model

Use the .nosql() method for flexible, schema-less JSON documents.

# The writer can insert complex documents
writer_client.nosql({
    "type": "insert",
    "collection": "product_reviews",
    "data": {
        "product_id": "laptop-123",
        "user_id": 1,
        "rating": 5,
        "comments": [{"user": "Bob", "text": "Great product!"}]
    }
})
print("\n--- NoSQL Operations ---")
print("✅ Writer added a product review.")

# The reader can search for documents
reviews = reader_client.nosql({
    "type": "search",
    "collection": "product_reviews",
    "query": {"rating": 5}
})
print(f"   Reader found reviews: {reviews}")

Step 5: Using the Graph Model

Use the .graph() method to manage nodes and edges, perfect for modeling relationships.

# The writer can build the graph
writer_client.graph({"type": "add_node", "node_id": "user:1", "properties": {"name": "Alice"}})
writer_client.graph({"type": "add_node", "node_id": "product:laptop-123", "properties": {"price": 1200}})
writer_client.graph({
    "type": "add_edge",
    "source": "user:1",
    "target": "product:laptop-123",
    "label": "PURCHASED"
})
print("\n--- Graph Operations ---")
print("✅ Writer created nodes and an edge.")

# The reader can query the relationships
purchases = reader_client.graph({"type": "get_neighbors", "node_id": "user:1"})
print(f"   Reader found what Alice purchased: {purchases}")

Step 6: Using the Vector (AI) Model

Use the .vector() method to leverage the database's native AI capabilities for semantic search.

# The writer (or admin) can add documents to be embedded by the server
documents_to_embed = [
    {"id": "doc1", "text": "A guide to high-performance computing"},
    {"id": "doc2", "text": "The history of deep learning models"},
    {"id": "doc3", "text": "An introduction to quantum physics"}
]
writer_client.vector({
    "type": "embed_and_add",
    "documents": documents_to_embed
})
print("\n--- Vector Operations ---")
print("✅ Writer sent text to the server for native embedding.")

# The reader can perform a semantic search using just text
# The server will create the query vector automatically
similar_docs = reader_client.vector({
    "type": "find_similar",
    "query_text": "AI and neural networks",
    "k": 1
})
print(f"   Reader searched for 'AI and neural networks' and found: {similar_docs}")

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

xplaindb_client-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

xplaindb_client-0.1.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xplaindb_client-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for xplaindb_client-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7a087114ff25f22807fa54dcdcd4f4f209f732d1fb2676dce76dfef6c9bff3ed
MD5 9736f139b3e713e6571539a90578f1b8
BLAKE2b-256 beacb0ac27e85b38442beadfc6e1defb22a2baab51607e9845ec268fad4de2d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for xplaindb_client-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2b6723ee0bced32e6636b640b892af4dc24a75702b49e8a77ec29f6c82b33db2
MD5 41b0605e1cee4cc0afce1e619695109f
BLAKE2b-256 f8064c3213da7d85a089969192db4a0e67343e79c16dae0ebc22e95dc0710509

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