This repository was archived by the owner on Sep 16, 2024. It is now read-only.
forked from quantopian/pgcontents
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhybrid_manager_example.py
91 lines (76 loc) · 3.25 KB
/
hybrid_manager_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# This example shows how to configure Jupyter/IPython to use the more complex
# HybridContentsManager.
# A HybridContentsManager implements the contents API by delegating requests to
# other contents managers. Each sub-manager is associated with a root
# directory, and all requests for data within that directory are routed to the
# sub-manager.
# A HybridContentsManager needs two pieces of information at configuration time:
# 1. ``manager_classes``, a map from root directory to the type of contents
# manager to use for that root directory.
# 2. ``manager_kwargs``, a map from root directory to a dict of keywords to
# pass to the associated sub-manager.
# Optionally, a HybridContentsManager supports path validation to ensure a
# consistent naming scheme or avoid illegal characters across different managers
# path_validators, a map from root directory to a validation function for new model paths.
import os
from hybridcontents import HybridContentsManager
from pgcontents.pgmanager import PostgresContentsManager
from s3contents import S3ContentsManager, GCSContentsManager
# LargeFileManager is the default Jupyter content manager
# NOTE: LargFileManager only exists in notebook > 5
# If using notebook < 5, use FileContentManager instead
from notebook.services.contents.largefilemanager import LargeFileManager
c = get_config()
# Set main content manager to be a HybridContentsManager
c.NotebookApp.contents_manager_class = HybridContentsManager
c.HybridContentsManager.manager_classes = {
# Associate the root directory with a LargeFileManager,
# This manager will receive all requests that don't fall under any of the
# other managers.
# If you want to make this path un-editable you can configure it to use a read-only filesystem
'': LargeFileManager,
# Associate /directory with a LargeFileManager.
'directory': LargeFileManager,
# Associate the postgres directory with a PostgresContentManager
'postgres': PostgresContentsManager,
# Associate the s3 directory with AWS S3
's3': S3ContentsManager,
# Associate the gcs directory with GCS
'gcs': GCSContentsManager
}
c.HybridContentsManager.manager_kwargs = {
# Args for the LargeFileManager mapped to /directory
'': {
'root_dir': '/tmp/read-only',
},
# Args for the LargeFileManager mapped to /directory
'directory': {
'root_dir': '/home/viaduct/local_directory',
},
# Args for PostgresContentsManager.
'postgres': {
'db_url': 'postgresql://ssanderson@/pgcontents_testing',
'user_id': 'my_awesome_username',
'max_file_size_bytes': 1000000, # Optional
},
# Args for S3ContentManager.
's3': {
"access_key_id": os.environ.get("AWS_ACCESS_KEY_ID"),
"secret_access_key": os.environ.get("AWS_SECRET_ACCESS_KEY"),
"endpoint_url": os.environ.get("AWS_ENDPOINT_URL"),
"bucket": "my-remote-data-bucket",
"prefix": "s3/prefix"
},
# Args for GCSContentManager.
'gcs': {
'project': "<your-project>",
'token': "~/.config/gcloud/application_default_credentials.json",
'bucket': "<bucket-name>"
},
}
def no_spaces(path):
return ' ' not in path
c.HybridContentsManager.path_validators = {
'postgres': no_spaces,
's3': no_spaces
}