Loading

Python

Hello Warp 10 from Python

The first way to interact between Python and Warp 10 is through the egress endpoint. For example:

# pip install requests
import requests
mc2 = "'Hello Warp 10!'"

requests.post('https://sandbox.senx.io/api/v0/exec', mc2).json()
# ['Hello Warp 10!']

Inter-operating WarpScript and Python

Using a Py4J gateway, you can access a WarpScript execution environment (a stack) directly from Python.

There is two ways to launch a gateway that gives access to a stack:

  • either it is launched by the Warp 10 platform via the Py4J plugin (available here);
  • or it can be launched manually from a Python script, but in this case no Warp 10 instance is connected to it (see this blog post, section 4, or this one)

Conversions

WarpScript supports the binary Pickle format with the functions ->PICKLE and PICKLE->.

The binary Apache Arrow columnar format is also supported through the Warp 10 Arrow extension.

Note that if you are using the egress endpoint, keep in mind that binary data will be encoded with charset ISO-8859-1 in the JSON output.

If you are using a Py4J gateway, common types are already converted (see Py4J automatic conversions), and binary data need not an additional encoding round.

GTS to DataFrame

We provide two macros to help convert GTS into DataFrame.

The macro @senx/python/GTStoPickledDict takes a GTS and converts it to pickled dict.

The macro @senx/python/ListGTStoPickledDict converts a list of GTS into a pickled dict, and also joins the timestamps into a single field.

They also take a boolean as second parameter indicating whether the classname (false) or the full selector (true) is used as the field keys of the dict.

Then after unpickling, Pandas can read the data into a dataframe with the method pandas.DataFrame.from_dict().

With an extension, it is also possible to use the Arrow format, see for example this blog post.

Description of the Py4J plugin for Warp 10

The Py4J plugin launches a gateway in same JVM running Warp 10. Instances of Java objects are accessible from Python through this gateway. By default, the gateway address is 127.0.0.1 and its port is 25333.

Using the Py4J plugin has multiple advantages:

  • you don't need to use POST requests to execute your code
  • you can access WarpScript objects from Python without them being serialized in JSON format
  • you can go back and forth between Python and the WarpScript stack that is in the JVM
  • you can process with Python server-side close to Warp 10 data

However, the Py4J gateway gives access to all classes loaded in the JVM of the WarpScript execution environment. So it is recommended not to use it in a standalone multi-tenant installation, or at least to restrict its access to priviledged users.

Py4j configuration parameters

The Py4j plugin depends on these parameters (default value is given if parameter is not set):

# register the Py4J plugin
warp10.plugin.py4j = io.warp10.plugins.py4j.Py4JWarp10Plugin

# Address and port of the gateway
py4j.host=127.0.0.1
py4j.port=25333

# Timeout specifications
py4j.timeout.read=0
py4j.timeout.connect=0

# Whether to use imposed limits on the stack or not
py4j.stack.nolimits=false

# If set, GatewayParameters must set the argument auth_token to this value to authorize the connection
#py4j.authtoken=

# Allowing to fetch data from Python (or other external access method) with FETCH, FIND or FINDSTATS.
egress.clients.expose=false

# set to true if the jar version of the plugin is not an uberjar and need an external dependency
plugin.defaultcl.io.warp10.plugins.py4j.Py4JWarp10Plugin = true

Short example with WarpScript

Once the plugin is installed, restart Warp 10. Then, you can set a connection with the gateway from Python:

# pip install py4j
from py4j.java_gateway import (JavaGateway, GatewayParameters)

# if you need to launch a gateway manually, you can use py4j.launch_gateway() function

# Connect to the gateway
params = GatewayParameters('127.0.0.1', 25333, auto_convert=True)
gateway = JavaGateway(gateway_parameters=params)

Then, you can create a WarpScript stack and start querying the Warp 10 platform.

# Instanciates a WarpScript stack
stack = gateway.entry_point.newStack()
stack = stack.execMulti('<My_WarpScript_code_here>')

# Extract top of the stack and store it in a Python variable
# @see https://www.py4j.org/advanced_topics.html#collections-conversion
my_var = stack.pop()

The following methods are useful:

  • execMulti(str): executes a multiline WarpScript code on the stack
  • pop(): extract the top of the stack
  • get(int): get the object at a certain level of the stack
  • clear(): reset the stack
  • load(str): get the object that was stored under a certain variable in WarpScript
  • getStackAttribute(key): get an attribute
  • setStackAttribute(key,value): set an attribute

Note that only basic conversions are supported automatically (primitive types, lists, sets, maps, iterators, byte arrays). Therefore, data of GTS need to be stored beforehand in a list, a map, or byte array using WarpScript functions. For that purpose, the functions VALUES, TICKLIST and ->PICKLE (since pickled representation are byte arrays) come in handy.

Py4j with a Jupyter Server

The python package warp10-jupyter contains the cell magic called %%warscript that executes WarpScript code through a gateway of the Py4J plugin. You can install it with pip install warp10-jupyter, or from the source code available here.

For example, you can load it in a Jupyter notebook with

%load_ext warpscript

Then you can start executing WarpScript code in following cells:

%%warpscript --stack s
<My_WarpScript_code_here>

The newly created WarpScript stack is stored under the Python variable s. Therefore, its objects can be accessed with the aforementioned methods.

After the cell has been executed, the stack is still in the JVM so subsequent cells starting with %%warpscript --stack s will execute WarpScript code on the same stack.

Recall that if needed, the docstring of this cell magic can be invoked within a notebook with %%warpscript?.

Read more

We wrote a blog post on the Py4J plugin, where we also explained how to use WarpScript in Python without a running Warp 10 platform.

In another blog post on the warp10-jupyter package, we show examples of notebooks.

In a blog post on WarpScript for Pythonists, we explain why and when Python users benefit from using WarpScript.