Skip to content

Commit 1e37285

Browse files
committed
Review
1 parent ec56106 commit 1e37285

File tree

5 files changed

+22
-66
lines changed

5 files changed

+22
-66
lines changed

jupyter_resource_usage/api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
from concurrent.futures import ThreadPoolExecutor
33

4-
import ipykernel
54
import psutil
65
import zmq
76
from jupyter_client.jsonutil import date_default
@@ -18,7 +17,12 @@
1817
from .utils import Callable
1918

2019

21-
USAGE_IS_SUPPORTED = version.parse("6.9.0") <= version.parse(ipykernel.__version__)
20+
try:
21+
import ipykernel
22+
23+
USAGE_IS_SUPPORTED = version.parse("6.9.0") <= version.parse(ipykernel.__version__)
24+
except ImportError:
25+
USAGE_IS_SUPPORTED = False
2226

2327
MAX_RETRIES = 3
2428

packages/labextension/src/format.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
// Taken from https://github.com/jupyter-server/jupyter-resource-usage/blob/e6ec53fa69fdb6de8e878974bcff006310658408/packages/labextension/src/memoryUsage.tsx#L272
2-
3-
type MemoryUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB';
1+
/**
2+
* The type of unit used for reporting memory usage.
3+
*/
4+
export type MemoryUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB';
45

5-
const MEMORY_UNIT_LIMITS: {
6+
/**
7+
* The number of bytes in each memory unit.
8+
*/
9+
export const MEMORY_UNIT_LIMITS: {
610
readonly [U in MemoryUnit]: number;
711
} = {
812
B: 1,
@@ -21,9 +25,8 @@ export function formatForDisplay(numBytes: number | undefined): string {
2125
/**
2226
* Given a number of bytes, convert to the most human-readable
2327
* format, (GB, TB, etc).
24-
* Taken from https://github.com/jupyter-server/jupyter-resource-usage/blob/e6ec53fa69fdb6de8e878974bcff006310658408/packages/labextension/src/memoryUsage.tsx#L272
2528
*/
26-
function convertToLargestUnit(
29+
export function convertToLargestUnit(
2730
numBytes: number | undefined
2831
): [number, MemoryUnit] {
2932
if (!numBytes) {

packages/labextension/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const extension: JupyterFrontEndPlugin<void> = {
3232
palette: ICommandPalette,
3333
notebookTracker: INotebookTracker
3434
) => {
35+
const trans = translator.load('jupyterlab');
3536
const item = new MemoryUsage(translator);
3637

3738
statusBar.registerStatusItem(extension.id, {
@@ -58,8 +59,8 @@ const extension: JupyterFrontEndPlugin<void> = {
5859
}
5960

6061
commands.addCommand(CommandIDs.getKernelUsage, {
61-
label: 'Kernel Usage',
62-
caption: 'Kernel Usage',
62+
label: trans.__('Kernel Usage'),
63+
caption: trans.__('Kernel Usage'),
6364
icon: new LabIcon({
6465
name: 'jupyterlab-kernel-usage:icon',
6566
svgstr: tachometer,

packages/labextension/src/memoryUsage.tsx

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { Poll } from '@lumino/polling';
1919

2020
import React from 'react';
2121

22+
import { MemoryUnit, MEMORY_UNIT_LIMITS, convertToLargestUnit } from './format';
23+
2224
import { resourceItem } from './text';
2325

2426
/**
@@ -188,7 +190,7 @@ export namespace MemoryUsage {
188190
const memoryLimit = value.limits.memory
189191
? value.limits.memory.rss
190192
: null;
191-
const [currentMemory, units] = Private.convertToLargestUnit(numBytes);
193+
const [currentMemory, units] = convertToLargestUnit(numBytes);
192194
const usageWarning = value.limits.memory
193195
? value.limits.memory.warn
194196
: false;
@@ -197,7 +199,7 @@ export namespace MemoryUsage {
197199
this._currentMemory = currentMemory;
198200
this._units = units;
199201
this._memoryLimit = memoryLimit
200-
? memoryLimit / Private.MEMORY_UNIT_LIMITS[units]
202+
? memoryLimit / MEMORY_UNIT_LIMITS[units]
201203
: null;
202204
this._warn = usageWarning;
203205
}
@@ -235,11 +237,6 @@ export namespace MemoryUsage {
235237
refreshRate: number;
236238
}
237239
}
238-
239-
/**
240-
* The type of unit used for reporting memory usage.
241-
*/
242-
export type MemoryUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB';
243240
}
244241

245242
/**
@@ -251,54 +248,6 @@ namespace Private {
251248
*/
252249
export const DECIMAL_PLACES = 2;
253250

254-
/**
255-
* The number of bytes in each memory unit.
256-
*/
257-
export const MEMORY_UNIT_LIMITS: {
258-
readonly [U in MemoryUsage.MemoryUnit]: number;
259-
} = {
260-
B: 1,
261-
KB: 1024,
262-
MB: 1048576,
263-
GB: 1073741824,
264-
TB: 1099511627776,
265-
PB: 1125899906842624,
266-
};
267-
268-
/**
269-
* Given a number of bytes, convert to the most human-readable
270-
* format, (GB, TB, etc).
271-
*/
272-
export function convertToLargestUnit(
273-
numBytes: number
274-
): [number, MemoryUsage.MemoryUnit] {
275-
if (numBytes < MEMORY_UNIT_LIMITS.KB) {
276-
return [numBytes, 'B'];
277-
} else if (
278-
MEMORY_UNIT_LIMITS.KB === numBytes ||
279-
numBytes < MEMORY_UNIT_LIMITS.MB
280-
) {
281-
return [numBytes / MEMORY_UNIT_LIMITS.KB, 'KB'];
282-
} else if (
283-
MEMORY_UNIT_LIMITS.MB === numBytes ||
284-
numBytes < MEMORY_UNIT_LIMITS.GB
285-
) {
286-
return [numBytes / MEMORY_UNIT_LIMITS.MB, 'MB'];
287-
} else if (
288-
MEMORY_UNIT_LIMITS.GB === numBytes ||
289-
numBytes < MEMORY_UNIT_LIMITS.TB
290-
) {
291-
return [numBytes / MEMORY_UNIT_LIMITS.GB, 'GB'];
292-
} else if (
293-
MEMORY_UNIT_LIMITS.TB === numBytes ||
294-
numBytes < MEMORY_UNIT_LIMITS.PB
295-
) {
296-
return [numBytes / MEMORY_UNIT_LIMITS.TB, 'TB'];
297-
} else {
298-
return [numBytes / MEMORY_UNIT_LIMITS.PB, 'PB'];
299-
}
300-
}
301-
302251
/**
303252
* Settings for making requests to the server.
304253
*/

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ include_package_data = True
4949
packages = find:
5050
python_requires = >=3.6
5151
install_requires =
52-
ipykernel >=6.11.0
5352
jupyter_server~=1.0
5453
prometheus_client
5554
psutil~=5.6

0 commit comments

Comments
 (0)