Use the JS API

In this guide, you'll learn how to create a basic web page and use the JS API to create a table in Deephaven and display it. The Deephaven JS API is used to connect to a Deephaven Community Core instance from a browser or node.js application. It manages all your server connections, table tracking, and server communications. The Deephaven Console experience is created using our JS API.

Note

This guide assumes you have Deephaven running locally with Python. You can find the completed example available at http://localhost:10000/jsapi/table_basic.html. There are other examples available at http://localhost:10000/jsapi, but we focus specifically on creating the basic table example step-by-step.

Create a simple web page

Create a new folder to work out of, add in index.html, and start up a server to host the file:

Tip

This example uses a basic Python server to host the index.html file, but you can substitute another server.

You should now be able to navigate to your server (located at at http://0.0.0.0:8000/ by default) to view our index.html. Initially, it will be a blank page - nothing is in the index.html yet!

Open up the index.html file for editing, and add the following:

Now, if you refresh the page, you should see "Table data" output. We're also loading the JS API scripts, which creates the global dh object (our main entry point into the JS API). If you do not include these scripts, or the path is incorrect, you will get errors trying to use the dh object.

The inline script doesn't have any actual code yet. Let's add some code to make the page do something. Add all the code below into the inline script in the body element of the page.

First, we need to open a connection to the server:

After a connection is established, we need to start a session. For this example, we will start a python session (the default):

Next, we need to run the code that will create the table. This code creates a static table called remoteTable with 10 rows and three columns: I, J, and K. Because it's Python code, be careful with indentation when creating the template string.

Retrieve the JS Table object:

Create a table header element (thead) with the table columns:

Set the viewport and extract the data to build the body of the table. Since the bottom of the specified viewport is inclusive and the table is ten rows, we only need 0,9:

Retrieve the table element in our document and append the table to it:

Now refresh the page. After the table loads, you should see the table data output in the browser.

Congratulations! You've successfully used the JS API to create a table and read it.

Final product

The final product of the HTML page is: