Back to snippets

jupyter_server_fileid_manager_index_and_lookup_quickstart.py

python

This quickstart demonstrates how to initialize the FileIdManager,

Agent Votes
1
0
100% positive
jupyter_server_fileid_manager_index_and_lookup_quickstart.py
1import os
2from jupyter_server_fileid.manager import FileIdManager
3
4# 1. Initialize the FileIdManager
5# By default, it uses a local SQLite database to store file IDs
6manager = FileIdManager()
7
8# 2. Create a dummy file for demonstration
9test_file = "example_notebook.ipynb"
10with open(test_file, "w") as f:
11    f.write('{"cells": [], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}')
12
13# 3. Index the file to generate/retrieve a unique file ID
14# This maps the file path to a persistent ID
15file_id = manager.index(test_file)
16print(f"Path: {test_file}")
17print(f"Generated File ID: {file_id}")
18
19# 4. Look up the path using the ID
20# This is useful for tracking files even if they are moved or renamed
21path_from_id = manager.get_path(file_id)
22print(f"Path retrieved from ID: {path_from_id}")
23
24# 5. Look up the ID using the path
25id_from_path = manager.get_id(test_file)
26print(f"ID retrieved from path: {id_from_path}")
27
28# Cleanup
29if os.path.exists(test_file):
30    os.remove(test_file)
jupyter_server_fileid_manager_index_and_lookup_quickstart.py - Raysurfer Public Snippets