Using file save hooks with TLJH

I am trying to use the FileContentsManager.pre_save_hook and FileContentsManager.post_save_hook.

I have followed the example here: File save hooks — Jupyter Server documentation , putting the code into the /etc/jupyter_server_config.py. That did not seem to be getting called, so I put it in a separate file: /opt//opt/tljh/config/jupyterhub_config.d/file_save_configuration.py. I know the file is being read, because if I put a syntax error into the file, it will show up as an exception in the jupyterhub log. However, when I save a file, the hooks do not appear to be called. Should this work in TLJH?

It should work if you put it in /etc/jupyter/jupyter_server_config.py (the path you pasted was missing the /jupyter/ part). This file needs to be found by the Jupyter Server, not JupyterHub.

3 Likes

Thank you for clarifying that. I tried again putting code into jupyter_server_config.py, and this time it worked. It wrote to the log file, and created a text file in a shared directory.

In case this helps anyone else, here is what I did that worked:

  1. added the following code to /etc/jupyter/jupyter_server_config.py
  2. killed any existing running servers: pkill jupyter+
  3. tljh-config reload
  4. journalctl -f
c.ServerApp.log_level = 'DEBUG'

def jody_pre_save(model, path, contents_manager):
    log = contents_manager.log
    log.info("jody_pre_save called")
    with open("/srv/src/scripts/presave.txt", "a") as file:
        file.write("pre-save hook called")

c.FileContentsManager.pre_save_hook = jody_pre_save

def jody_post_save(model, os_path, contents_manager):
    log = contents_manager.log
    log.info("jody_post_save called")
    with open("/srv/src/scripts/postsave.txt", "a") as file:
        file.write("post-save hook called")


c.FileContentsManager.post_save_hook = jody_post_save
```
3 Likes