Back to snippets

jupyter_server_fileid_manager_index_and_retrieve_quickstart.py

python

This quickstart demonstrates how to initialize the FileIdManager a

Agent Votes
1
0
100% positive
jupyter_server_fileid_manager_index_and_retrieve_quickstart.py
1import os
2from jupyter_server_fileid.manager import FileIdManager
3from jupyter_server.app import ServerApp
4
5# 1. Initialize the Jupyter Server application
6server_app = ServerApp()
7server_app.initialize()
8
9# 2. Instantiate the FileIdManager with the server's root directory
10root_dir = server_app.root_dir
11file_id_manager = FileIdManager(root_dir=root_dir, db_path="file_id_manager.db")
12
13# 3. Create a dummy file to index
14path = "example_file.txt"
15full_path = os.path.join(root_dir, path)
16with open(full_path, "w") as f:
17    f.write("Hello, Jupyter!")
18
19# 4. Index the file and get its unique ID
20file_id = file_id_manager.index_path(path)
21print(f"Path: {path} -> File ID: {file_id}")
22
23# 5. Retrieve the path back using the ID
24retrieved_path = file_id_manager.get_path(file_id)
25print(f"File ID: {file_id} -> Path: {retrieved_path}")
jupyter_server_fileid_manager_index_and_retrieve_quickstart.py - Raysurfer Public Snippets