|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example usage of the new tree functionality in MindsDB Python SDK. |
| 4 | +
|
| 5 | +This demonstrates how to use the tree() methods to explore database structures. |
| 6 | +""" |
| 7 | + |
| 8 | +import mindsdb_sdk |
| 9 | + |
| 10 | + |
| 11 | +def main(): |
| 12 | + """Example usage of tree functionality.""" |
| 13 | + |
| 14 | + # Connect to MindsDB (adjust URL as needed) |
| 15 | + server = mindsdb_sdk.connect('http://127.0.0.1:47334') |
| 16 | + |
| 17 | + print("=== MindsDB Tree Exploration Example ===\n") |
| 18 | + |
| 19 | + # Get the tree of all databases |
| 20 | + print("1. Getting databases tree:") |
| 21 | + try: |
| 22 | + databases_tree = server.tree() |
| 23 | + |
| 24 | + for db_node in databases_tree: |
| 25 | + print(f" 📁 Database: {db_node.name}") |
| 26 | + print(f" Type: {db_node.type}") |
| 27 | + print(f" Engine: {db_node.engine}") |
| 28 | + print(f" Deletable: {db_node.deletable}") |
| 29 | + print(f" Visible: {db_node.visible}") |
| 30 | + print() |
| 31 | + |
| 32 | + except Exception as e: |
| 33 | + print(f"Error getting databases tree: {e}") |
| 34 | + return |
| 35 | + |
| 36 | + # Explore a specific database |
| 37 | + print("2. Exploring a specific database:") |
| 38 | + if databases_tree: |
| 39 | + # Use the first database as an example |
| 40 | + example_db_name = databases_tree[0].name |
| 41 | + print(f"Exploring database: {example_db_name}") |
| 42 | + |
| 43 | + try: |
| 44 | + database = server.databases.get(example_db_name) |
| 45 | + |
| 46 | + # Get tables without schema info |
| 47 | + print(f"\n Tables in {example_db_name} (basic):") |
| 48 | + basic_tree = database.tree(with_schemas=False) |
| 49 | + for item in basic_tree: |
| 50 | + print(f" 📋 {item.name} ({item.class_}) - Type: {item.type}") |
| 51 | + |
| 52 | + # Get tables with schema info (if applicable) |
| 53 | + print(f"\n Tables in {example_db_name} (with schemas):") |
| 54 | + detailed_tree = database.tree(with_schemas=True) |
| 55 | + for item in detailed_tree: |
| 56 | + if item.class_ == 'schema': |
| 57 | + print(f" 📁 Schema: {item.name}") |
| 58 | + if item.children: |
| 59 | + for child in item.children: |
| 60 | + print(f" 📋 {child.name} ({child.type})") |
| 61 | + else: |
| 62 | + print(f" 📋 {item.name} ({item.class_}) - Type: {item.type}") |
| 63 | + if hasattr(item, 'schema') and item.schema: |
| 64 | + print(f" Schema: {item.schema}") |
| 65 | + |
| 66 | + except Exception as e: |
| 67 | + print(f"Error exploring database {example_db_name}: {e}") |
| 68 | + |
| 69 | + print("\n=== Tree exploration complete ===") |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
0 commit comments