Skip to main content

JavaScript API

Datagrok JS API allows to control all aspects of the Datagrok platform. The API can be used either from ad-hoc scripts (Functions | Scripts | New JavaScript Script) or from packages.

This document covers the following areas:

API structure

There are three entry points to the API:

  • grok for easy discoverability of the functionality,
  • ui for building user interfaces, and
  • DG for instantiating classes directly.


grok is the main entry point for the JS API. It helps you discover the functionality using IntelliSense.

For example, to import a dataframe from a CSV file:

  1. Type grok.. The following options appear: [shell, chem, data, data, ml].
  2. Select the best fitting option,, and press .. The following options appear: [demo, query, compareTables, parseCsv, upload].
  3. Select and press Enter. Use IntelliSense to provide the parameters.



When you need more control, use the DG namespace.


Building a UI is a special form of programming, and many languages were invented for that purpose only (HTML, XAML, JSX) . We have prioritized the following aspects when choosing our approach: simplicity, discoverability, readability.

.add(ui.span(['People of Earth, your attention, please… ']))
.onOK(() => {'OK!'); })


Check out JS API Class Reference


Datagrok visual shell is used to get access to top-level views, tables, methods, and platform states.

Learn more here: Datagrok Shell reference

Data manipulation


Use DataFrame, Column , ColumnList, and Row classes for table manipulation.

demog = grok.testData('demog', 5000);
foo = demog.cols.addNew('foo', 'int');
demog.rows.removeAt(1, 3);
demog.rows.insertAt(2, 2);
demog.rows.addNew(['Spiderman', 'studyX', 'NYC', 32, 'Spider', 'Net', new Date(2020), 180, 80, 666]);
demog.rows.addNew().subj = 'Iron Man';

// alternative ways of setting values
foo.set(1, 777);
demog.set('age', 1, 44);


Each DataFrame is associated with two bitsets: selection and filter.

// bit set (same applies to filter)
demog.selection.set(5, false);
demog.selection.findNext(0, false);

DataFrame code snippets:


Control views via the following methods:;

Dock an arbitrary visual element in a platform:

let e = document.createElement('DIV');
e.innerText = 'This element has been created in JavaScript';, 'JS', 'left', 0.5);

Registering functions

Pretty much anything in Datagrok is a function, it is a concept that connects together scripts written in different languages, predictive models, statistical functions, query transformations, data flows, and many other features.

The following code registers a "jsConcat" function that becomes a first-class citizen in the platform (i.e., it can be used from console, gets registered in help, there could be an optional audit trail associated with the invocations, etc)

To test the newly registered function, enter "jsConcat(42, 33)" in the Console.

signature: 'String jsConcat(int foo, int bar)',
run: (foo, bar) => `${foo}_${bar}`});

The code below registers two functions, "jsWidget" and "jsSuggestCountryName". To test jsWidget, create a new Dashboard, and click on "Widget" under "Widgets".

signature: 'Widget jsWidget()',
tags: 'Widgets',
run: function() {
let e = document.createElement('DIV');
function update() {
let date = new Date();
e.innerText = date.toTimeString();
window.setTimeout(update, 1000);

return new ui.Widget(e);

signature: 'List<String> jsSuggestCountryName(String text)',
isAsync: true,
run: async function(text) {
let response = await fetch('' + text);
return response.status === 200 ? (await response.json()).map(country => country['name']) : [];

Internally, JavaScript-based applications are functions that are annotated accordingly.

Code snippets:


We are exposing events coming out of the platform as a stream via the Rx.JS library that makes it easy to compose asynchronous or callback-based code. The API makes easy to subscribe to either global, or instance-related events:

   // global event when user changes the current project =>`Current project changed: ${}`));

// subscribing to DataFrame events
demog ='demog', 5000);
demog.onValuesChanged.subscribe((_) =>'values changed'));

Event-related code snippets:

To figure out what events are coming out of the platform, use the Inspector tool. Open it (Alt+I), go to the "Client Log" tab, and perform the action that you want to intercept. In the panel, you will see one or more of the events, click on them to inspect event parameters. To simplify the development process, we also generate JavaScript code for handling this particular event, copy-paste it from the context panel into your code if needed.


User-defined types

Define your own classes, and integrate them easily by providing a meta-class that extends DG.EntityMeta. This will provide native support for context actions, rendering, drag-and drop, tooltips, and favorites.

Code snippets:


The platform provides full support for docking windows.'first'), DG.DOCK_TYPE.RIGHT, null, 'First');

Docking code snippets:


Use grok.dapi entry point for managing server-based objects, such as datasets, connection, users, credentials, jobs, packages, etc.

See also HttpDataSource subclasses.

Code snippets:

Machine learning

Use entry point for machine learning-related routines.

Code snippets:


Use grok.chem entry point for cheminformatics-related routines.

Code snippets:



See also: