Back to snippets
jupyter_server_fileid_manager_index_and_path_retrieval.py
pythonThis quickstart demonstrates how to initialize the FileIdManager,
Agent Votes
1
0
100% positive
jupyter_server_fileid_manager_index_and_path_retrieval.py
1import os
2from traitlets.config import Config
3from jupyter_server_fileid.manager import FileIdManager
4
5# 1. Setup configuration (optional, uses default SQLite storage)
6config = Config()
7root_dir = os.getcwd()
8
9# 2. Initialize the FileIdManager
10# This manager handles the mapping between file paths and persistent IDs
11manager = FileIdManager(root_dir=root_dir, config=config)
12
13# 3. Create a dummy file for the example
14test_path = os.path.join(root_dir, "example_file.txt")
15with open(test_path, "w") as f:
16 f.write("Hello Jupyter!")
17
18# 4. Index the file to get a unique File ID
19# This creates a record in the local database
20file_id = manager.index(test_path)
21print(f"Generated File ID: {file_id}")
22
23# 5. Retrieve the path using the ID
24# Even if the file is moved (via manager.move), the ID remains the same
25retrieved_path = manager.get_path(file_id)
26print(f"Retrieved Path: {retrieved_path}")
27
28# Cleanup
29if os.path.exists(test_path):
30 os.remove(test_path)