I am configuring a shared JupyterHub environment and would like to add a pre-save hook to clear notebook outputs before saving. I found a code snippet for this purpose and added it to the hub.extraConfig
section of my values.yaml
file:
hub:
extraConfig:
jupyterhub_config_custom.py: |
# -------------------------------
# ✅ PREVENT OUTPUT STORAGE (Avoid Conflicts)
# -------------------------------
def clear_outputs_pre_save(model, path, contents_manager):
"""
Clears outputs from cells before saving notebooks to prevent shared file conflicts.
"""
if model['type'] != 'notebook':
return
if model['content']['nbformat'] != 4:
return
for cell in model['content']['cells']:
if cell['cell_type'] != 'code':
continue
cell['outputs'] = []
cell['execution_count'] = None
if 'collapsed' in cell['metadata']:
cell['metadata'].pop('collapsed', 0)
c.FileContentsManager.pre_save_hook = clear_outputs_pre_save # Hook to clear outputs before saving
# -------------------------------
# ✅ FILE STORAGE SETTINGS (Avoid Data Loss)
# -------------------------------
c.FileContentsManager.always_save_metadata = False # Avoid saving unnecessary metadata
c.FileContentsManager.save_script = False # Prevent `.py` script generation
When I add this configuration under the hub.extraConfig
section and deploy it, I can see in the hub logs that my configuration is being loaded correctly. However, once I start a user session, the outputs are not being cleared as expected. However, when I start a user session and save a notebook, the outputs are still visible when I view the saved notebook file from the terminal (using cat filename.ipynb
).
Could someone help me understand why the configuration is not applied in the user session, and suggest any possible fixes?