Guides
Visit our websiteDeveloper Docs
  • Getting Started with Databrain
    • What is Databrain?
  • ❄️Onboarding & Configuration
    • πŸ“Sign-Up
    • ✍️Sign-In
    • ✏️Google Sign In Setup for Self-hosted app
    • πŸ€”Forgot password? Recover your Databrain Account
    • 🌟Onboarding
    • πŸ’ΎAdd a Data Source
    • πŸ§‘Configure Tenants
    • πŸ†•Create a Workspace
    • πŸ”“Create a Private Workspace
    • πŸ†•Create a Dashboard
    • πŸ’ Create a Metric
      • Create Custom Columns
      • Create a Metric using Chat Mode
      • Create a Metric using Custom SQL
    • Workspace Settings
      • General Settings
      • Access Control Settings
      • Cache Settings
      • Download Settings
    • πŸ—„οΈExplore Data
  • πŸ›’οΈDatasources
    • Connecting Data Sources to Databrain
      • Amazon Redshift
      • Snowflake
      • BigQuery
      • MySQL
      • Postgres
      • MongoDB
      • ElasticSearch
      • DataBricks
      • ClickHouse
      • MSSQL
      • Amazon S3
      • CSV
      • Firebolt
      • SingleStore
      • Athena
    • Allow Access to our IP
    • Add a Data Source
    • Configure Tenants
    • How to Sync a Data Source
    • Edit Tenancy
    • Create a Datamart
    • Semantic Layer
    • Create a Data App
    • Creating a Custom Dataset/View in a Multi-Datasource Environment
  • Workspace
    • Multi Datasource Workspace
  • πŸ”DASHBOARDS
    • Edit a Dashboard
    • Share Dashboard
    • Dashboard Settings
    • Create/Modify Dashboard Filter
      • Dashboard Filter - Variable Apply On
      • Add LHS and RHS custom sql support for dashboard filter
    • Customize Layout
    • Adding Elements to Dashboard
    • Import/Export Dashboard
    • Report Scheduler
  • πŸ“‰METRIC
    • Edit a Metric
    • Joins , Filter, Sort, Group By
    • Complex Filter
    • Apply Metric Filter
      • Metric Filter - Variable
      • Metric Filter - Custom
    • Switch X axis and Switch Y axis
    • Group By
    • Footnote and Long Description
    • Dynamic Property
    • Archive/Unarchive Metric Card
    • Download Metric Card
    • Download Underlying Data
    • Metric Summary
    • Metric Expression for Single Value Card
    • AI Summary
    • Merge Metrics
    • Section Filters
    • View Unpublished Metrics
  • πŸ“ŠVISUALIZATIONS - ACTIONS & APPEARANCE
    • Chart Actions
      • Chart Click Action
      • Chart Click Action with Metric
      • Card Click Action
      • Drill Down
      • Cross Dashboard Drill Down
    • Chart Appearance
      • Chart-Specific Appearance Options
  • πŸ›’οΈPREVIEW OF DASHBOARDS
    • Email Settings for Scheduled Reports
    • Scheduled Reports for End User
  • πŸ”FILTERS
    • Dashboard Filter
    • Metric Filter
    • App filter
  • πŸ’‘Features
    • Python Editor Console
    • Custom SQL Console
    • Custom SQL Query Guidelines
  • 🏒Integrating Plugin
    • ✳️Get an auth token
    • πŸ™Get a guest token
  • πŸ›ƒTHEMEING & CUSTOMIZATION
    • 🎨Creating a theme
    • πŸ–ΌοΈView the theme in action
    • βš™οΈReset a saved theme
  • πŸ“ŠMetric Component (upto version v0.11.15)
    • ✨Quick start
  • πŸ•ΈοΈWeb Components
    • ✨Quick start
    • βš›οΈFramework Specific Guide
  • πŸš€Product Changelog
  • 🀳Self Hosted Changelog
Powered by GitBook
On this page
  • Introduction
  • Where is our Python Editor Console ?
  1. Features

Python Editor Console

PreviousApp filterNextCustom SQL Console

Last updated 5 months ago

Introduction

This document provides detailed guidelines on using the integrated Python editor console within our platform. The editor is pre-configured with essential libraries and functionalities to assist in executing Python scripts effectively, particularly for data querying and handling secrets.

Where is our Python Editor Console ?

  • In the Create Metric page, click on the "Custom Query" option located at the top left side of the page.

  • Then, select "</> Write Code" and choose "</> Python" from the Custom Query window to access our Python Editor Console.

Pre-installed Libraries

  • Requests Module: The requests library is readily available and pre-imported for HTTP requests. You do not need to import it again in your scripts.

import requests  # This module is already available in the scope

Working with Variables

To integrate dynamic data within your scripts, you can utilize predefined variables and data structures:

  • Client ID: Use the variable client_id which should be a string representing the client identifier.

  • Metric Filters: This variable metric_filters allows for the filtering of data based on various criteria. The structure of metric_filters is defined as follows:

    from typing import Dict, Optional, Union
    import datetime
    
    class DateDict:
        startDate: datetime.date
        endDate: datetime.date
    
    # MetricFilters type annotation
    MetricFilters = Dict[str, Union[DateDict, str, int]]
    

    The metric_filters variable can include filters of types STRING, DATE, and NUMBER, uniquely identified by a filter name:

    • STRING: A plain string value.

    • DATE: A dictionary containing startDate and endDate, both of datetime.date type.

    • NUMBER: An integer value.

Handling Secrets

Secrets are stored and accessed via the secrets dictionary. This allows secure storage and retrieval of sensitive data such as API keys or database credentials: You can set up your secrets in the Home Page β†’ Settings Tab β†’ Secrets.

url = secrets['BASE_URL']  # Accessing the base URL stored in secrets
res = requests.get(url).json()  # Making a GET request using the retrieved URL

Storing Results

To store or output data from your script, assign your data to the result variable. This variable should be an array of objects (or a list of dictionaries in Python terms):

result = [{'key': 'value'}, ...]  # An example assignment to the result variable
result = requests.get('SOME URL').json()  # Storing JSON response from a GET request

Usage Example

Here’s are some quick examples demonstrating how to utilize these capabilities:

country_population = {
    'USA': 331000000,
    'India': 1380000000,
    'China': 1430000000,
    'Brazil': 212600000,
    'Nigeria': 206000000
}


result = [{'country': country, 'population': population} for country, population in country_population.items()]
# Assuming client_id and metric_filters are predefined
client_id = '12345'
metric_filters = {'salesDate': {'startDate': datetime.date(2021, 1, 1), 'endDate': datetime.date(2021, 12, 31)}}

# Accessing a secret
api_url = secrets['API_URL']

# Making a GET request
response = requests.get(f"{api_url}/data?client_id={client_id}").json()

# Assigning response to result
result = response

This guide should help you effectively utilize the Python editor console for your data handling needs. The capabilities outlined here are designed to enhance the security, flexibility, and efficiency of your data manipulation tasks.

πŸ’‘