[Python] Error running a magic programmatically via IPython's run_cell_magic

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following program, which I wrote in two Jupyter Notebook cells.

    Cell 1:

    import rumbledb as rmbl
    %load_ext rumbledb
    %env RUMBLEDB_SERVER=http://public.rumbledb.org:9090/jsoniq


    Cell 2:

    %%jsoniq
    parse-json("{\"x\":3}}").x


    After executing

    spark-submit rumbledb-1.21.0-for-spark-3.5.jar serve -p 9090


    in a Git Bash console, when I run these two cells in order, the output of the second cell is

    Took: 0.2607388496398926 ms
    3


    I'd like to rewrite cell 2 so as not to use the cell magic literally (%% syntax) but programatically, via a function. The reason I'd like to avoid using it literally is so that I can encapsulate it in a function, as I described in this post.

    I tried the advice in the end of this answer and rewrote cell 2 as follows:

    Cell 3:

    from IPython import get_ipython
    ipython = get_ipython()
    ipython.run_cell_magic('jsoniq', '', 'parse-json("{\"x\":3}}").x')


    However, when I ran this cell, I got the following error message:

    Took: 2.2189698219299316 ms
    There was an error on line 2 in file:/home/ubuntu/:


    Code: [XPST0003]
    Message: Parser failed.

    Metadata: file:/home/ubuntu/:LINE:2:COLUMN:0:
    This code can also be looked up in the documentation and specifications for more information.

    1. Why did I get the error message, and how can I get rid of it?
    2. Why did I not get 3 in the output, and how can I get it?

    Continue reading...

Compartilhe esta Página