-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
111 lines (84 loc) · 2.92 KB
/
Copy path__init__.py
File metadata and controls
111 lines (84 loc) · 2.92 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
### Example datasets ###
from em_database import data
from em_database.config import settings
from em_database.downloadable_dataset import DownloadableDataset
__all__ = []
def get_data_dir():
"""
Get the directory where example datasets are stored.
Returns
-------
str
Path to the example datasets directory.
"""
from em_database import config
return config.data_dir()
def set_data_dir(path: str, persist: bool = True):
"""
Set the directory where example datasets are stored.
Parameters
----------
path : str
Path to the desired example datasets directory.
persist : bool, optional
If True (the default), remember the choice across sessions by writing it
to the settings file. Pass False for a one-off, in-memory change.
"""
settings["data_dir"] = str(path)
if persist:
settings.save()
def reset_data_dir():
"""
Reset the example datasets directory to the default location, clearing any
saved choice.
"""
settings.reset("data_dir")
def get_setting(key: str, default=None):
"""Read a value from :data:`em_database.settings`."""
return settings.get(key, default)
def set_setting(key: str, value, persist: bool = True):
"""Set a value in :data:`em_database.settings`, persisting it by default."""
settings[key] = value
if persist:
settings.save()
def browse(**kwargs):
"""
Open the interactive dataset browser in Jupyter.
Returns an `anywidget` widget listing every dataset grouped by technique,
showing which are downloaded, revealing full metadata on hover, and
downloading on click with a live progress toast. Requires the optional
`anywidget` dependency (`pip install em-database[widget]`).
``display(em_database)`` renders the same browser.
"""
from em_database.widget import browse as _browse
return _browse(**kwargs)
__all__ = [
"get_data_dir",
"set_data_dir",
"reset_data_dir",
"get_setting",
"set_setting",
"settings",
"browse",
"data",
"DownloadableDataset",
]
# Let ``display(em_database)`` render the browser. Reassigning the module's
# __class__ to a ModuleType subclass is a supported pattern (see PEP 562) and is
# what lets the package itself carry a rich Jupyter repr.
import sys as _sys # noqa: E402
from types import ModuleType as _ModuleType # noqa: E402
class _EmDatabaseModule(_ModuleType):
def _repr_mimebundle_(self, include=None, exclude=None, **kwargs):
try:
widget = browse()
except Exception:
return {
"text/plain": (
"em_database — install the interactive browser with "
"`pip install em-database[widget]`, then call "
"em_database.browse()."
)
}
return widget._repr_mimebundle_(**kwargs)
_sys.modules[__name__].__class__ = _EmDatabaseModule