Skip to content

Streamlit Integration

PyKXConnection

Bases: BaseConnection[SyncQConnection]

A connection to q/kdb+ processes from streamlit. Initialize using:

st.connection("<name>", type = pykx.streamlit.PyKXConnection, *args)

PyKX Connection supports the application of queries using Syncronous IPC connections to q/kdb+ processes or Python processes running PyKX as a server.

This is supported through the query() method, this method allows users to run sql, qsql or q queries against these processes returning PyKX data.

Warning

Streamlit integration is not presently supported for Windows as for full utilization it requires use of PYKX_THREADING functionality

Parameters:

Name Type Description Default
host

The host name to which a connection is to be established.

required
port

The port to which a connection is to be established.

required
username

Username for q connection authorization.

required
password

Password for q connection authorization.

required
timeout

Timeout for blocking socket operations in seconds. If set to 0, the socket will be non-blocking.

required
large_messages

Whether support for messages >2GB should be enabled.

required
tls

Whether TLS should be used.

required
unix

Whether a Unix domain socket should be used instead of TCP. If set to True, the host parameter is ignored. Does not work on Windows.

required
wait

Whether the q server should send a response to the query (which this connection will wait to receive). Can be overridden on a per-call basis. If True, Python will wait for the q server to execute the query, and respond with the results. If False, the q server will respond immediately to every query with generic null (::), then execute them at some point in the future.

required
The username and password parameters are not required.

The username and password parameters are only required if the q server requires authorization. Refer to ssl documentation for more information.

The timeout argument may not always be enforced when making succesive querys.

When making successive queries if one query times out the next query will wait until a response has been recieved from the previous query before starting the timer for its own timeout. This can be avioded by using a seperate SyncQConnection instance for each query.

Examples:

Connect to a q process at localhost on port 5050 as a streamlit connection, querying using q

>>> import streamlit as st
>>> import pykx as kx
>>> conn = st.connection('pykx', type=kx.streamlit.PyKXConnection,
...                      host = 'localhost', port = 5050)
>>> df = conn.query('select from tab').pd()
>>> st.dataframe(df)

reset

reset(**kwargs)

Reset an existing Streamlit Connection object, this can be used to manually reconnect to a datasource which was disconnected. This will use the connection details provided at initialisation of the original class.

Example:

Reset a connection if deemed to no longer be valid

>>> import streamlit as st
>>> import pykx as kx
>>> conn = st.connection('pykx', type=kx.streamlit.PyKXConnection,
...                      host = 'localhost', port = 5050)
>>> if not conn.is_healthy():
...     conn.reset()
>>>

is_healthy

is_healthy()

Check if an existing streamlit connection is 'healthy' and available for query.

Returns:

Type Description
bool

A boolean indicating if the connection being used is in a 'healthy' state

>>> import streamlit as st
>>> import pykx as kx
>>> conn = st.connection('pykx', type=kx.streamlit.PyKXConnection,
...                      host = 'localhost', port = 5050)
>>> conn.is_healthy()
True

query

query(query, *args, format='q', **kwargs)

Evaluate a query on the connected q process over IPC.

Parameters:

Name Type Description Default
query str

A q expression to be evaluated.

required
*args

Arguments to the q query. Each argument will be converted into a pykx.K object. Up to 8 arguments can be provided, as that is the maximum supported by q.

()
format

What execution format is to be used, should the function use the qsql interface, execute a sql query or run q code.

'q'

Raises:

Type Description
RuntimeError

A closed IPC connection was used.

QError

Query timed out, may be raised if the time taken to make or receive a query goes over the timeout limit.

TypeError

Too many arguments were provided - q queries cannot have more than 8 parameters.

ValueError

Attempted to send a Python function over IPC.

Examples:

Connect to a q process at localhost on port 5050 as a streamlit connection, querying using q

>>> import streamlit as st
>>> import pykx as kx
>>> conn = st.connection('pykx', type=kx.streamlit.PyKXConnection,
...                      host = 'localhost', port = 5050)
>>> df = conn.query('select from tab').pd()
>>> st.dataframe(df)

Connect to a q process at localhost on port 5050 as a streamlit connection, querying using qsql

>>> import streamlit as st
>>> import pykx as kx
>>> conn = st.connection('pykx', type=kx.streamlit.PyKXConnection,
...                      host = 'localhost', port = 5050)
>>> df = conn.query('tab', where='x>0.5', format='qsql').pd()
>>> st.dataframe(df)

Connect to a q process at localhost on port 5050 as a streamlit connection, querying using sql

>>> import streamlit as st
>>> import pykx as kx
>>> conn = st.connection('pykx', type=kx.streamlit.PyKXConnection,
...                      host = 'localhost', port = 5050)
>>> df = conn.query('select * from tab where x>0.5', format='sql').pd()
>>> st.dataframe(df)