Interpreter Execution Hooks (Experimental)


Apache Zeppelin allows for users to specify additional code to be executed by an interpreter at pre and post-paragraph code execution. This is primarily useful if you need to run the same set of code for all of the paragraphs within your notebook at specific times. Currently, this feature is only available for the spark and pyspark interpreters. To specify your hook code, you may use z.registerHook(). For example, enter the following into one paragraph:

z.registerHook("post_exec", "print 'This code should be executed before the parapgraph code!'")
z.registerHook("pre_exec", "print 'This code should be executed after the paragraph code!'")

These calls will not take into effect until the next time you run a paragraph.

In another paragraph, enter

print "This code should be entered into the paragraph by the user!"

The output should be:

This code should be executed before the paragraph code!
This code should be entered into the paragraph by the user!
This code should be executed after the paragraph code!

If you ever need to know the hook code, use z.getHook():

print z.getHook("post_exec")

print 'This code should be executed after the paragraph code!'

Any call to z.registerHook() will automatically overwrite what was previously registered. To completely unregister a hook event, use z.unregisterHook(eventCode). Currently only "post_exec" and "pre_exec" are valid event codes for the Zeppelin Hook Registry system.

Finally, the hook registry is internally shared by other interpreters in the same group. This would allow for hook code for one interpreter REPL to be set by another as follows:

z.unregisterHook("post_exec", "pyspark")

The API is identical for both the spark (scala) and pyspark (python) implementations.


Calls to z.registerHook("pre_exec", ...) should be made with care. If there are errors in your specified hook code, this will cause the interpreter REPL to become unable to execute any code pass the pre-execute stage making it impossible for direct calls to z.unregisterHook() to take into effect. Current workarounds include calling z.unregisterHook() from a different interpreter REPL in the same interpreter group (see above) or manually restarting the interpreter group in the UI.