Interpreter Execution Hooks (Experimental)

Overview

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:

%pyspark
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

%pyspark
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():

%pyspark
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:

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

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

Caveats

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.