Custom Source

Introduction #

SmartChart supports custom data sources, allowing you to write Python connector modules for any data source. Create reusable connectors with dataset() and insert_dataset() functions that can be used like built-in data sources.

Development Pattern #

Function Purpose
dataset(*args, **kwargs) Returns query dataset (2D array or JSON dict)
insert_dataset(*args, **kwargs) Data submission/write implementation

Function Parameters #

def dataset(*args, **kwargs):
    # args[0]: sqlList - Dataset editor input split by semicolons [sql1, sql2...]
    # args[1]: config - Config dict {'host','port','user','password','db'}
    # Write your data query and processing logic here
    result = []
    return result

def insert_dataset(*args, **kwargs):
    # args[0]: contents - Submitted data in 2D array format
    # args[1]: table - Table name from config
    # args[3]: config - Config dict
    # Write your data write logic here
    pass

Usage Workflow #

  1. Write a Python module with dataset() and insert_dataset() functions
  2. Upload the module to a dashboard template via resource management
  3. Create a data source with the module name as the driver
  4. Use the custom data source like any built-in source

Example: HTTP API Connector #

import requests
import json

def dataset(*args, **kwargs):
    sqlList = args[0]
    config = args[1]
    url = config['host']
    headers = {'Authorization': f"Bearer {config['password']}"}
    response = requests.get(url, headers=headers, params={'query': sqlList[0]})
    return json.loads(response.text)

def insert_dataset(*args, **kwargs):
    contents = args[0]
    config = args[3]
    url = config['host']
    headers = {'Authorization': f"Bearer {config['password']}"}
    requests.post(url, json=contents, headers=headers)

Custom data source modules must be uploaded as Python files (.py) to the dashboard’s resource management.