Search code examples
jupyter-notebookjupyterkdbpykx

Is there a way to suppress empty output (`::`) when using the magic %%q (with PyKX, NOT PyQ) in Jupyter notebook?


When using the %%q magic with PyKX in jupyter notebook, it seems that the output from each line of code is printed, including ::. See, e.g., the example on the documentation . Is there a way to suppress empty output (::)?

enter image description here

I tried to add ; to each line of code, but still a :: is printed for each line of code ended with ;. I expect all output of :: to be removed, or only the output of the last line of code in a cell is printed with all results of previous lines removed (even if that line is not ended with ;).


Solution

  • I find an interim solution by defining a customized magic command which mimics the one in PyKX (defined here: https://github.com/KxSystems/pykx/blob/main/src/pykx/nbextension.py). In my usecase, I don't mind adding the definition at the top of each notebook, so I just add the following code to define a new magic command %%qq to do the same stuff. I also don't use any options of the PyKX q magic command, so I remove those code.

    import pykx as kx
    from IPython.core.magic import register_cell_magic
    
    @register_cell_magic
    def qq(instructions, code):
        ld = kx.SymbolAtom('.Q.pykxld')
        _q = kx.q
        code = [kx.CharVector(x) for x in code.split('\n')][:-1]
        ret = _q(
            "{[ld;code;file] value (@';\"q\";enlist[file],/:value(ld;code))}",
            ld,
            code,
            b'jupyter_cell.q'
        )
        if not ret[-1] == kx.q('::'):
            print(_q('{x y}', ret, kx.q.count(ret)-1)
    

    For more details, see https://github.com/KxSystems/pykx/issues/7