Jupyter q Magic Command
import pykx as kx
Create the external q process¶
To run this example, the Python code in the following cell will do the equivalent to executing the following in a terminal:
$ q -p 5001
import subprocess
import time
proc = subprocess.Popen(
('q', '-p', '5001')
)
time.sleep(5)
Executing against Embedded q¶
A cell beginning with %%q
will execute q within PyKX
's EmbeddedQ
module.
%%q
til 10
0 1 2 3 4 5 6 7 8 9
Execution options¶
Execution options can also be included after %%q
.
Here is the list of currently supported execution options.
--debug: prints the q backtrace before raising a QError
if the cell errors
--display: calls display rather than the default print
on returned objects
%%q
([] a: 1 2 3)
a - 1 2 3
%%q --display
([] a: 1 2 3)
a | |
---|---|
0 | 1 |
1 | 2 |
2 | 3 |
Executing against an external q process over IPC¶
Connection information can also be included after the %%q
to connect to a remote q
process over
IPC.
Here is the list of currently supported connection parameters. If they specify a type a second value is expected to follow them to be used as the parameter. If no type follows them they can be used as a stand alone flag.
--host: A string object denoting the host to connect to
--port: An int object denoting the port to connect over
--user: A str object denoting the username to use when connecting
--password: A str object denoting the password to use when connecting
--timeout: A float object denoting the time in seconds before the query
times out, defaults to no timeout
--nolarge: Disable messages over 2GB being sent / received
--tls: Use a tls connection
--unix: Use a unix connection
--reconnection_attempts: An int object denoting how many
reconnection attempts to make
--noctx: Disable the context interface
Connect to a q server running on localhost
at port 5001
as user
using password password
and disable the context interface.
%%q --host localhost --port 5001 --user user --pass password --noctx
til 10
0 1 2 3 4 5 6 7 8 9
All connection arguments are optional with the exception of the --port
argument. If --host
is not provided localhost
will be used as the default host.
%%q --port 5001
tab:([]a:1000?1000; b:1000?500.0; c:1000?`AAPL`MSFT`GOOG);
It is possible to execute q
code spanning multiple lines.
%%q --port 5001
afunc: {[x; y]
x + y
};
afunc[0; 1]
afunc[2; 3]
1 5
Using the SQL interface¶
The s)
syntax to run SQL queries against local tables within the q
process.
Note: The s.k_
library must be loaded first to use the SQL interface
%%q --port 5001
\l s.k_
s) select * from tab where a>500 and b<250.0 limit 5
a b c ----------------- 916 149.4238 AAPL 898 97.74763 MSFT 900 45.35602 MSFT 677 150.6862 MSFT 741 151.1293 MSFT
Using namespaces¶
You can also use q
namespaces, and switch between them using \d
.
Note: The namespace is reset back to the base namespace .
between cells.
%%q --port 5001
\d .example
f: {[x] til x};
%%q --port 5001
\d
.example.f[10]
. 0 1 2 3 4 5 6 7 8 9
# Shutdown the q process we were connected to for the IPC demo
proc.kill()