Skip to main content
Version: Python

How to create function-generated tables

This guide will show you how to create and use function-generated tables. As the name suggests, function-generated tables are tables generated by a user-defined Python function.

The function_generated_table method allows you to create ticking tables via a Python function. The ticking tables can be triggered by one or more source tables, or by a refresh interval.

Usage

The basic syntax for function_generated_table is as follows:

function_generated_table(table_generator, source_tables, refresh_interval_ms, exec_ctx) -> Table

The workflow for this method is to first define a function that returns a table, then pass it to function_generated_table, which will re-run the functino at pre-defined intervals based on either one or more source tables or a refresh interval.

If one or more source tables are used, the function will be re-run any time any of the tables tick. If a refresh interval is used, the function is re-run once per interval in milliseconds. You must only use one or the other as the trigger.

The user-defined table_generator function can source its data from anywhere - it could be generated with NumPy, fetched from a REST API, or sampled from a distribution. The only limit is the user's imagination, and the requirement that the function return a valid table.

Define a table_generator function

We can't create a function-generated table without a function to generate the table, so creating that function is step one.

You can define your function in the normal Pythonic way. The only requirement is that the function must return a table.

Here's an example:

from deephaven import empty_table


def make_table():
return empty_table(5).update(
["X = randomInt(0, 10)", "Y = randomDouble(-50.0, 50.0)"]
)

Execution context

The function_generated_table method requires an execution context to run in. If you don't specify an execution context, the method will use the systemic execution context. The examples below do not specify one, so the systemic execution context is used.

Generate tables with a Python function

Now, we'll create two function-generated tables: one that re-runs the table_generator whenever the source table updates, and one that re-runs the table_generator function every 2000ms.

from deephaven import time_table, empty_table
from deephaven import function_generated_table


def make_table():
return empty_table(5).update(
["X = randomInt(0, 10)", "Y = randomDouble(-50.0, 50.0)"]
)


tt = time_table("PT1S")

result_from_table = function_generated_table(
table_generator=make_table, source_tables=tt
)

result_from_refresh_interval = function_generated_table(
table_generator=make_table, refresh_interval_ms=2000
)