Skip to content

Instantly share code, notes, and snippets.

@komsit37
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save komsit37/1763ecb0547356058c24 to your computer and use it in GitHub Desktop.

Select an option

Save komsit37/1763ecb0547356058c24 to your computer and use it in GitHub Desktop.
ipython magic function to call q cell
from IPython.core.magic import (register_cell_magic)
from qpython import qconnection
@register_cell_magic
def q(con, cell=None):
"""
execute q statements in jupyter cell. Required qpython https://github.com/exxeleron/qPython
:param con: connection string (default 'localhost:5555')
:type con: str
:returns: result is saved to global 'qdat' or can be accessed with _
:rtype: dataframe
"""
global qdat
if con == "":
con = "localhost:5555"
tokens = con.split(':')
f = lambda x: tokens[x] if len(tokens)>x else None
h = qconnection.QConnection(host=f(0), port=int(f(1)), username=f(2), password=f(3))
h.open()
s = cell.encode('ascii')
qdat = h(s, pandas = True)
h.close()
print con + '| returns', qdat.shape
return qdat
@komsit37

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment