Created
May 15, 2026 02:42
-
-
Save biranchi2018/a434105ba6f69f9b662b152ff35e5975 to your computer and use it in GitHub Desktop.
Remove 'metadata.widgets' entry from Google Colab notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %%writefile remove_widgets.py | |
| # pip install nbformat | |
| import nbformat | |
| from nbformat import v4 | |
| import glob | |
| def remove_widgets_metadata(notebook_path): | |
| """Removes the 'metadata.widgets' entry from the notebook, preserving cell outputs.""" | |
| try: | |
| with open(notebook_path, 'r', encoding='utf-8') as f: | |
| notebook = nbformat.read(f, as_version=4) | |
| if 'widgets' in notebook.metadata: | |
| del notebook.metadata['widgets'] | |
| print(f"'metadata.widgets' removed from '{notebook_path}'.") | |
| else: | |
| print(f"'metadata.widgets' not found in '{notebook_path}'. No action needed.") | |
| with open(notebook_path, 'w', encoding='utf-8') as f: | |
| nbformat.write(notebook, f) | |
| print(f"Notebook '{notebook_path}' saved with updated metadata.") | |
| except FileNotFoundError: | |
| print(f"Error: Notebook '{notebook_path}' not found. Please ensure the notebook is saved and provide the correct path.") | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| # Dynamically find the current notebook's filename | |
| notebook_files = glob.glob('*.ipynb') | |
| if notebook_files: | |
| # Assuming the first .ipynb file found is the current one. | |
| # If you have multiple .ipynb files in your Colab environment | |
| # and this is not the correct one, you might need to adjust this manually. | |
| current_notebook_path = notebook_files[0] | |
| print(f"Detected notebook: {current_notebook_path}") | |
| remove_widgets_metadata(notebook_path=current_notebook_path) | |
| else: | |
| print("No .ipynb files found in the current directory. Please save your notebook first.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment