from pydomo import Domo
from pydomo.datasets import Sorting, DataSetRequest, Schema, Column, ColumnType
import logging
import json
import subprocess
# read the secret, client_id, and scope
client_secret = None
scope = None
client_id = None
api_host = 'api.domo.com'
with open("secret.txt", 'r') as secret_file:
client_secret = secret_file.read().strip()
with open("client.txt", 'r') as client_file:
client_id = client_file.read().strip()
with open("scope.txt", 'r') as scope_file:
scope = scope_file.read().strip()
# link to domo instance
domo = Domo(client_id, client_secret, logger_name="Eric", log_level=logging.INFO, api_host=api_host)
(takes 30 seconds to run)
# get the input and output datasets
to_find = ['BASE|ERIC|Billboard Top 10','BASE|ERIC|Billboard Top 10 OUTPUT']
def find_datasets_by_name(dataset_names, domo):
datasets = list(domo.datasets.list())
results = dict()
for dataset in datasets:
if dataset['name'] in dataset_names:
results[dataset['name']] = dataset
return results
datasets = find_datasets_by_name(to_find, domo)
(Export it to 'input.csv')
include_csv_header = True
csv_file_path = './input.csv'
csv_download = domo.datasets.data_export_to_file(
datasets['BASE|ERIC|Billboard Top 10']['id'], csv_file_path, include_csv_header)
r_script_path = './script_for_domo.R'
command = 'Rscript'
working_dir = '.'
outfile_name = 'output.csv'
args = [working_dir, csv_file_path, outfile_name] # could place several command arguments here
cmd = [command, r_script_path] + args
# run the command and store result
result = subprocess.check_output(cmd, universal_newlines=True)
# universal newlines tells python to interpret returned output as a string
# and handle both windows and linux newline characters
domo.datasets.data_import_from_file(
datasets['BASE|ERIC|Billboard Top 10 OUTPUT']['id'], outfile_name, update_method='REPLACE')
print(domo.datasets.data_export(
datasets['BASE|ERIC|Billboard Top 10 OUTPUT']['id'], include_csv_header))
datasets