Skip to content

Commit d4e569e

Browse files
jtpiorichagadgildeclanvk
committed
Add a lab extension to display memory usage
Co-authored-by: RichaGadgil <[email protected]> Co-authored-by: Declan Kelly <[email protected]>
1 parent 1d7b8d4 commit d4e569e

19 files changed

+4705
-58
lines changed

.github/workflows/build.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: '*'
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: Install node
17+
uses: actions/setup-node@v1
18+
with:
19+
node-version: '14.x'
20+
21+
- name: Install Python
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: '3.7'
25+
architecture: 'x64'
26+
27+
- name: Install dependencies
28+
run: python -m pip install jupyterlab --pre
29+
30+
- name: Build the extension
31+
run: |
32+
python -m pip install .
33+
34+
jupyter labextension list 2>&1 | grep -ie "@jupyter-server/resource-usage.*OK"
35+
python -m jupyterlab.browser_check
36+
37+
- name: Lint
38+
run: |
39+
cd packages/labextension/
40+
jlpm
41+
jlpm run lint:check

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ var/
3131
.vscode
3232
*.code-workspace
3333
.history
34+
35+
# Labextension
36+
*.bundle.*
37+
lib/
38+
node_modules/
39+
*.egg-info/
40+
.ipynb_checkpoints
41+
*.tsbuildinfo
42+
nbresuse/labextension
43+
yarn-error.log

CONTRIBUTING.md

Lines changed: 118 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,142 @@ you can follow the [Jupyter contributor guide](https://jupyter.readthedocs.io/en
66
Make sure to also follow [Project Jupyter's Code of Conduct](https://github.com/jupyter/governance/blob/master/conduct/code_of_conduct.md)
77
for a friendly and welcoming collaborative environment.
88

9-
## Development set up
9+
## Setting up a development environment
1010

1111
We recommend using [pipenv](https://docs.pipenv.org/) to make development easier.
1212

13-
1. Clone the git repository:
13+
Alternatively, you can also use `conda` or `mamba` to create new virtual environments.
1414

15-
```bash
16-
git clone https://github.com/yuvipanda/nbresuse
17-
```
15+
Clone the git repository:
1816

19-
2. Create an environment that will hold our dependencies.
17+
```bash
18+
git clone https://github.com/yuvipanda/nbresuse
19+
```
20+
21+
Create an environment that will hold our dependencies:
22+
23+
```bash
24+
cd nbresuse
25+
pipenv --python 3.6
26+
```
2027

21-
```bash
22-
cd nbresuse
23-
pipenv --python 3.6
24-
```
28+
With conda:
2529

26-
3. Activate the virtual environment that pipenv created for us
30+
```bash
31+
conda create -n nbresuse -c conda-forge python
32+
```
2733

28-
```bash
29-
pipenv shell
30-
```
34+
Activate the virtual environment that pipenv created for us
3135

32-
4. Do a dev install of nbresuse and its dependencies
36+
```bash
37+
pipenv shell
38+
```
3339

34-
```bash
35-
pip install --editable .[dev]
36-
```
40+
With conda:
3741

38-
5. Install and enable the nbextension for use with Jupyter Classic Notebook.
42+
```bash
43+
conda activate nbresuse
44+
```
3945

40-
```bash
41-
jupyter nbextension install --py nbresuse --symlink --sys-prefix
42-
jupyter serverextension enable --py nbresuse --sys-prefix
43-
jupyter nbextension enable --py nbresuse --sys-prefix
44-
```
46+
Do a dev install of nbresuse and its dependencies
4547

46-
6. Start a Jupyter Notebook instance, open a new notebook and check out the memory usage
47-
in the top right!
48+
```bash
49+
pip install --editable .[dev]
50+
```
4851

49-
```bash
50-
jupyter notebook
51-
```
52+
Enable the server extension:
5253

53-
7. If you want to test the memory limit display functionality, you can do so by setting
54-
the `MEM_LIMIT` environment variable (in bytes) when starting `jupyter notebook`.
54+
```bash
55+
jupyter serverextension enable --py nbresuse --sys-prefix
56+
```
5557

56-
```bash
57-
MEM_LIMIT=$(expr 128 \* 1024 \* 1024) jupyter notebook
58-
```
58+
*Note: if you're using Jupyter Server:*
5959

60-
8. NBResuse has adopted automatic code formatting so you shouldn't
61-
need to worry too much about your code style.
60+
```bash
61+
jupyter server extension enable --py nbresuse --sys-prefix
62+
```
63+
64+
## Classic notebook extension
65+
66+
Install and enable the nbextension for use with Jupyter Classic Notebook.
67+
68+
```bash
69+
jupyter nbextension install --py nbresuse --symlink --sys-prefix
70+
jupyter nbextension enable --py nbresuse --sys-prefix
71+
```
72+
73+
Start a Jupyter Notebook instance, open a new notebook and check out the memory usage in the top right!
74+
75+
```bash
76+
jupyter notebook
77+
```
78+
79+
If you want to test the memory limit display functionality, you can do so by setting the `MEM_LIMIT` environment variable (in bytes) when starting `jupyter notebook`.
80+
81+
```bash
82+
MEM_LIMIT=$(expr 128 \* 1024 \* 1024) jupyter notebook
83+
```
84+
85+
## JupyterLab extension
86+
87+
The JupyterLab extension for `nbresuse` was bootstrapped from the [extension cookiecutter](https://github.com/jupyterlab/extension-cookiecutter-ts), and follows the common patterns and tooling for developing extensions.
88+
89+
```bash
90+
# activate the environment (conda, pipenv)
91+
92+
# install the package in development mode
93+
python -m pip install -e ".[dev]"
94+
95+
# link your development version of the extension with JupyterLab
96+
jupyter labextension develop . --overwrite
97+
98+
# go to the labextension directory
99+
cd labextension/
100+
101+
# Rebuild extension Typescript source after making changes
102+
jlpm run build
103+
```
104+
105+
You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.
106+
107+
```bash
108+
# Watch the source directory in one terminal, automatically rebuilding when needed
109+
jlpm run watch
110+
# Run JupyterLab in another terminal
111+
jupyter lab
112+
```
113+
114+
With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).
115+
116+
To check the extension is correctly installed, run:
117+
118+
```bash
119+
jupyter labextension list
120+
```
121+
122+
It should show something like the following:
123+
124+
```bash
125+
JupyterLab v3.0.0
126+
/path/to/env/share/jupyter/labextensions
127+
nbresuse v0.1.0 enabled OK
128+
```
129+
130+
## pre-commit
131+
132+
`nbresuse` has adopted automatic code formatting so you shouldn't need to worry too much about your code style.
62133
As long as your code is valid,
63134
the pre-commit hook should take care of how it should look. Here is how to set up pre-commit hooks for automatic code formatting, etc.
64135

65-
```bash
66-
pre-commit install
67-
```
136+
```bash
137+
pre-commit install
138+
```
68139

69-
You can also invoke the pre-commit hook manually at any time with
140+
You can also invoke the pre-commit hook manually at any time with
70141

71-
```bash
72-
pre-commit run
73-
```
142+
```bash
143+
pre-commit run
144+
```
74145

75146
which should run any autoformatting on your code
76147
and tell you about any errors it couldn't fix automatically.
@@ -82,11 +153,11 @@ hook with `pre-commit install`, you can fix everything up using
82153
`pre-commit run --all-files`. You need to make the fixing commit
83154
yourself after that.
84155

85-
9. It's a good idea to write tests to exercise any new features,
86-
or that trigger any bugs that you have fixed to catch regressions. `pytest` is used to run the test suite. You can run the tests with:
156+
## Tests
157+
158+
It's a good idea to write tests to exercise any new features,
159+
or that trigger any bugs that you have fixed to catch regressions. `pytest` is used to run the test suite. You can run the tests with in the repo directory:
87160

88161
```bash
89162
python -m pytest -vvv nbresuse
90163
```
91-
92-
in the repo directory.

MANIFEST.in

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
11
include LICENSE
2+
3+
include README.md
4+
include pyproject.toml
5+
include package.json
6+
include install.json
7+
include ts*.json
8+
9+
graft nbresuse/labextension
10+
11+
# Javascript files
12+
graft src
13+
graft style
14+
prune **/node_modules
15+
prune lib
16+
17+
# Patterns to exclude from any directory
18+
global-exclude *~
19+
global-exclude *.pyc
20+
global-exclude *.pyo
21+
global-exclude .git
22+
global-exclude .ipynb_checkpoints

install.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"packageManager": "python",
3+
"packageName": "nbresuse",
4+
"uninstallInstructions": "Use your Python package manager (pip, conda, etc.) to uninstall the package nbresuse"
5+
}

nbresuse/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import json
2+
import os.path as osp
3+
14
from notebook.utils import url_path_join
25
from tornado import ioloop
36

@@ -6,6 +9,15 @@
69
from nbresuse.metrics import PSUtilMetricsLoader
710
from nbresuse.prometheus import PrometheusHandler
811

12+
HERE = osp.abspath(osp.dirname(__file__))
13+
14+
with open(osp.join(HERE, "labextension", "package.json")) as fid:
15+
data = json.load(fid)
16+
17+
18+
def _jupyter_labextension_paths():
19+
return [{"src": "labextension", "dest": data["name"]}]
20+
921

1022
def _jupyter_server_extension_paths():
1123
"""

packages/labextension/.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
dist
3+
coverage
4+
**/*.d.ts
5+
tests

packages/labextension/.eslintrc.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = {
2+
extends: [
3+
'eslint:recommended',
4+
'plugin:@typescript-eslint/eslint-recommended',
5+
'plugin:@typescript-eslint/recommended',
6+
'plugin:prettier/recommended',
7+
],
8+
parser: '@typescript-eslint/parser',
9+
plugins: ['@typescript-eslint'],
10+
rules: {
11+
'@typescript-eslint/naming-convention': [
12+
'error',
13+
{
14+
selector: 'interface',
15+
format: ['PascalCase'],
16+
custom: {
17+
regex: '^I[A-Z]',
18+
match: true,
19+
},
20+
},
21+
],
22+
'@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }],
23+
'@typescript-eslint/no-explicit-any': 'off',
24+
'@typescript-eslint/no-namespace': 'off',
25+
'@typescript-eslint/no-use-before-define': 'off',
26+
'@typescript-eslint/quotes': [
27+
'error',
28+
'single',
29+
{ avoidEscape: true, allowTemplateLiterals: false },
30+
],
31+
curly: ['error', 'all'],
32+
eqeqeq: 'error',
33+
'prefer-arrow-callback': 'error',
34+
},
35+
};

packages/labextension/.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
**/node_modules
3+
**/lib
4+
**/package.json

packages/labextension/.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

0 commit comments

Comments
 (0)