Component Builder (Custom Code)
Summary of How It Works
You can write custom Python to generate a JSON or HTML response, just like an API would return. Then use the rest of the component builder to turn it into a visualization (table, chart, or HTML iframe).
Your code is passed to an isolated AWS Lambda function equipped with certain packages (listed below), a 10 second execution timeout, and 128mb of RAM.
The code is executed according to the refresh interval you set at the bottom of the builder - the response gets cached for that amount of time, and then it'll rerun when you load the component on a dashboard and the cache expired.
Instructions
-
The Python script needs to set a variable
result
to a JSON object or HTML string. You can write helper functions in this script, but the execution is looking for any setting of a local variable calledresult
. -
You can't import any packages to the script, but it's importing the following:
import json
import requests
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup
import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go
from sqlalchemy import create_engine
from sqlalchemy import select
import psycopg2
import boto3
from datetime import datetime, date, time, timedelta, timezone
import matplotlib.pyplot as plt
- To use custom variables, enter the name and default value in the table on the right of the code editor. They get imported at the top of your script as global variables, so you can use them as such. These are good for API parameters, API Keys, etc. and if you don't mark them as private, it will allow users of the component to input their own custom values of those variables.
Write and Test Your Script in Your Own Editor
Our component builder is a basic code editor that isn't the ideal experience for initially creating the script, as it doesn't print out console results - only runtime errors. We suggest you build and test your code in your preferred Python development tool, and then once confirmed, copy it into the builder and make any necessary adjustments (such as removing the package imports or adding in customizable variables).
Sample Scripts
Example 1: Charting Data From an API
Example 2: Merging data from two APIs, and using a private API key
Updated about 2 years ago