forked from jupyterlab/jupyterlab
/
pageconfig.ts
199 lines (181 loc) · 5.46 KB
/
pageconfig.ts
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { JSONExt } from '@phosphor/coreutils';
import minimist from 'minimist';
import { URLExt } from './url';
/**
* Declare stubs for the node variables.
*/
declare var process: any;
declare var require: any;
/**
* The namespace for Page Config functions.
*/
export namespace PageConfig {
/**
* Get global configuration data for the Jupyter application.
*
* @param name - The name of the configuration option.
*
* @returns The config value or an empty string if not found.
*
* #### Notes
* All values are treated as strings.
* For browser based applications, it is assumed that the page HTML
* includes a script tag with the id `jupyter-config-data` containing the
* configuration as valid JSON. In order to support the classic Notebook,
* we fall back on checking for `body` data of the given `name`.
*
* For node applications, it is assumed that the process was launched
* with a `--jupyter-config-data` option pointing to a JSON settings
* file.
*/
export function getOption(name: string): string {
if (configData) {
return configData[name] || Private.getBodyData(name);
}
configData = Object.create(null);
let found = false;
// Use script tag if available.
if (typeof document !== 'undefined' && document) {
const el = document.getElementById('jupyter-config-data');
if (el) {
configData = JSON.parse(el.textContent || '') as {
[key: string]: string;
};
found = true;
}
}
// Otherwise use CLI if given.
if (!found && typeof process !== 'undefined') {
try {
const cli = minimist(process.argv.slice(2));
const path: any = require('path');
let fullPath = '';
if ('jupyter-config-data' in cli) {
fullPath = path.resolve(cli['jupyter-config-data']);
} else if ('JUPYTER_CONFIG_DATA' in process.env) {
fullPath = path.resolve(process.env['JUPYTER_CONFIG_DATA']);
}
if (fullPath) {
/* tslint:disable */
// Force Webpack to ignore this require.
configData = eval('require')(fullPath) as { [key: string]: string };
/* tslint:enable */
}
} catch (e) {
console.error(e);
}
}
if (!JSONExt.isObject(configData)) {
configData = Object.create(null);
} else {
for (let key in configData) {
// PageConfig expects strings
if (typeof configData[key] !== 'string') {
configData[key] = JSON.stringify(configData[key]);
}
}
}
return configData![name] || Private.getBodyData(name);
}
/**
* Set global configuration data for the Jupyter application.
*
* @param name - The name of the configuration option.
* @param value - The value to set the option to.
*
* @returns The last config value or an empty string if it doesn't exist.
*/
export function setOption(name: string, value: string): string {
const last = getOption(name);
configData![name] = value;
return last;
}
/**
* Get the base url for a Jupyter application, or the base url of the page.
*/
export function getBaseUrl(): string {
return URLExt.normalize(getOption('baseUrl') || '/');
}
/**
* Get the tree url for a JupyterLab application.
*/
export function getTreeUrl(): string {
return URLExt.join(getBaseUrl(), getOption('treeUrl'));
}
/**
* Get the base websocket url for a Jupyter application, or an empty string.
*/
export function getWsUrl(baseUrl?: string): string {
let wsUrl = getOption('wsUrl');
if (!wsUrl) {
baseUrl = baseUrl ? URLExt.normalize(baseUrl) : getBaseUrl();
if (baseUrl.indexOf('http') !== 0) {
return '';
}
wsUrl = 'ws' + baseUrl.slice(4);
}
return URLExt.normalize(wsUrl);
}
/**
* Returns the URL converting this notebook to a certain
* format with nbconvert.
*/
export function getNBConvertURL({
path,
format,
download
}: {
path: string;
format: string;
download: boolean;
}): string {
const notebookPath = URLExt.encodeParts(path);
const url = URLExt.join(getBaseUrl(), 'nbconvert', format, notebookPath);
if (download) {
return url + '?download=true';
}
return url;
}
/**
* Get the authorization token for a Jupyter application.
*/
export function getToken(): string {
return getOption('token') || Private.getBodyData('jupyterApiToken');
}
/**
* Get the Notebook version info [major, minor, patch].
*/
export function getNotebookVersion(): [number, number, number] {
const notebookVersion = getOption('notebookVersion');
if (notebookVersion === '') {
return [0, 0, 0];
}
return JSON.parse(notebookVersion);
}
/**
* Private page config data for the Jupyter application.
*/
let configData: { [key: string]: string } | null = null;
}
/**
* A namespace for module private data.
*/
namespace Private {
/**
* Get a url-encoded item from `body.data` and decode it
* We should never have any encoded URLs anywhere else in code
* until we are building an actual request.
*/
export function getBodyData(key: string): string {
if (typeof document === 'undefined' || !document.body) {
return '';
}
let val = document.body.dataset[key];
if (typeof val === 'undefined') {
return '';
}
return decodeURIComponent(val);
}
}