GitHub user agoodm opened a pull request:

    [ZEPPELIN-1423] Allow users to specify pre/post-execute callbacks for 

    ### What is this PR for?
    One feature built-in to Jupyter's ipykernel that is currently missing in 
Zeppelin are pre/post-execute "callbacks". This PR allows for the possibility 
of entering code that is set to be executed by the interpreter before  the code 
entered into the paragraph (pre-execute) and after (post-execute). In addition 
to saving some users the effort of writing code that they want executed in 
every paragraph, this will help pave the way for more advanced features coming 
soon, like automatically displaying matplotlib figures without an explicit call 
to a `show()` function. See the JIRA issue for more details on this.
    The core implementation for this is primarily contained in a new class 
called `InterpreterCallbackRegistry` which heavily mimics the Angular Display 
system (eg, `AngularObjectRegistry`). While this interface alone should suffice 
for Interpreter maintainers on the Java/JVM side, I also thought it might be 
useful to expose the system directly to the users in their notebooks. Hence, an 
implementation that works with the spark and pyspark interpreters (via the 
`ZeppelinContext` class) is also included here.
    ### What type of PR is it?
    New Feature
    ### Todos
    * [ ] - Add unit tests
    * [ ] - Add new documentation
    * [ ] - Find a better way to check for errors in pre-execute as this can 
brick the REPL (see more info below)
    ### What is the Jira issue?
    ### How should this be tested?
    In a new note, add the following lines of code to a paragraph:
    z.registerCallback("post_exec", "print 'This code should be executed before 
the parapgraph code!'")
    z.registerCallback("pre_exec", "print 'This code should be executed after 
the paragraph code!'")
    Then run any other paragraph in the note containing some other code, eg
    print "This code should be entered into the paragraph by the user!"
    The output should be:
    This code should be executed after the paragraph code!
    This code should be entered into the paragraph by the user!
    This code should be executed before the parapgraph code!
    You should also test out the other two methods (`getCallback()` and 
`unregisterCallback()`) specified in ``.
    One final caveat that should be mentioned: If there are errors in the code 
you specify for a pre-execute event, it will render the interpreter useless 
since the current implementation prepends the the code specified in `pre_exec` 
directly to the paragraph entered code before calling `interpret()`. The 
current workaround for this would be to either restart the interpreter group or 
call `unregisterCallback()` via a different REPL within the interpreter group 
(eg, `z.unregisterCallback("pyspark", "pre_exec")` from the spark interpreter). 
I would appreciate if anyone here would be willing to share any better 
approaches here.
    ### Questions:
    * Does the licenses files need update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? Yes

You can merge this pull request into a Git repository by running:

    $ git pull ZEPPELIN-1423

Alternatively you can review and apply these changes as the patch at:

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1439
commit 2c4756d4d6861ba6192e52f0ca1c4c23b21a8070
Author: Alex Goodman <>
Date:   2016-09-13T20:32:36Z

    First attempt at adding pre/post-execute callbacks
    Added support for pre/post-execute callback hooks to interpreters

commit e4e4ef3d8b4e3da1937d43892e88f030a7bd42b7
Author: Alex Goodman <>
Date:   2016-09-19T22:48:34Z

    Added support for user defined callbacks in spark/pyspark interpreters


If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at or file a JIRA ticket
with INFRA.

Reply via email to