Skip to content

Commit 2a7780d

Browse files
authored
Merge pull request #33 from krinsman/master
Autoformatting and documentation
2 parents c10f2ea + 1d8685a commit 2a7780d

14 files changed

+219
-97
lines changed

.flake8

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[flake8]
2+
# Ignore style and complexity
3+
# E: style errors
4+
# W: style warnings
5+
# F401: module imported but unused
6+
# F811: redefinition of unused `name` from line `N`
7+
# F841: local variable assigned but never used
8+
ignore = E, C, W, F401, F403, F811, F841, E402, I100, I101, D400
9+
exclude =
10+
helm-chart,
11+
hooks,
12+
setup.py,
13+
statuspage,
14+
versioneer.py

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@ var/
2323
*.egg-info/
2424
.installed.cfg
2525
*.egg
26-

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
repos:
2+
- repo: https://github.com/asottile/reorder_python_imports
3+
rev: v1.3.5
4+
hooks:
5+
- id: reorder-python-imports
6+
language_version: python3.6
7+
- repo: https://github.com/ambv/black
8+
rev: 18.9b0
9+
hooks:
10+
- id: black
11+
- repo: https://github.com/pre-commit/pre-commit-hooks
12+
rev: v2.1.0
13+
hooks:
14+
- id: end-of-file-fixer
15+
- id: check-json
16+
- id: check-yaml
17+
exclude: ^helm-chart/nbviewer/templates/
18+
- id: check-case-conflict
19+
- id: check-executables-have-shebangs
20+
- id: requirements-txt-fixer
21+
- id: flake8

.travis.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
language: python
2+
3+
python:
4+
- 3.7
5+
- 3.8
6+
7+
before_install:
8+
- pip install --upgrade setuptools pip
9+
10+
install:
11+
- pip install --editable .
12+
13+
script:
14+
- python -m pytest -vvv nbresuse
15+
16+
17+
jobs:
18+
include:
19+
- name: autoformatting check
20+
python: 3.6
21+
# NOTE: It does not suffice to override to: null, [], or [""]. Travis will
22+
# fall back to the default if we do.
23+
before_install: echo "Do nothing before install."
24+
install: pip install pre-commit
25+
script:
26+
- pre-commit run --all-files
27+
after_success: echo "Do nothing after success."
28+
after_failure:
29+
- |
30+
echo "You can install pre-commit hooks to automatically run formatting"
31+
echo "on each commit with:"
32+
echo " pre-commit install"
33+
echo "or you can run by hand on staged files with"
34+
echo " pre-commit run"
35+
echo "or after-the-fact on already committed files with"
36+
echo " pre-commit run --all-files"

CONTRIBUTING.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Contributing
22

3-
Contributions to nbresuse are highly welcome!
3+
Contributions to NBResuse are highly welcome! As a [Jupyter](https://jupyter.org) project,
4+
you can follow the [Jupyter contributor guide](https://jupyter.readthedocs.io/en/latest/contributor/content-contributor.html).
5+
6+
Make sure to also follow [Project Jupyter's Code of Conduct](https://github.com/jupyter/governance/blob/master/conduct/code_of_conduct.md)
7+
for a friendly and welcoming collaborative environment.
48

59
## Development set up
610

@@ -51,4 +55,38 @@ We recommend using [pipenv](https://docs.pipenv.org/) to make development easier
5155

5256
```bash
5357
MEM_LIMIT=$(expr 128 \* 1024 \* 1024) jupyter notebook
54-
```
58+
```
59+
60+
8. NBResuse has adopted automatic code formatting so you shouldn't
61+
need to worry too much about your code style.
62+
As long as your code is valid,
63+
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.
64+
65+
```bash
66+
pre-commit install
67+
```
68+
69+
You can also invoke the pre-commit hook manually at any time with
70+
71+
```bash
72+
pre-commit run
73+
```
74+
75+
which should run any autoformatting on your code
76+
and tell you about any errors it couldn't fix automatically.
77+
You may also install [black integration](https://github.com/ambv/black#editor-integration)
78+
into your text editor to format code automatically.
79+
80+
If you have already committed files before setting up the pre-commit
81+
hook with `pre-commit install`, you can fix everything up using
82+
`pre-commit run --all-files`. You need to make the fixing commit
83+
yourself after that.
84+
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:
87+
88+
```bash
89+
python -m pytest -vvv nbresuse
90+
```
91+
92+
in the repo directory.

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
# nbresuse
1+
**[Installation](#installation)** |
2+
**[Configuration](#configuration)** |
3+
**[Resources Displayed](#resources-displayed)** |
4+
**[Contributing](#contributing)**
5+
6+
# NBResuse
7+
8+
[![PyPI](https://img.shields.io/pypi/v/nbresuse.svg)](https://pypi.python.org/pypi/nbresuse)
9+
[![PyPI](https://img.shields.io/pypi/l/nbresuse.svg)](https://pypi.python.org/pypi/nbresuse)
10+
[![GitHub](https://img.shields.io/badge/issue_tracking-github-blue?logo=github)](https://github.com/yuvipanda/nbresuse/issues)
11+
[![TravisCI build status](https://img.shields.io/travis/jupyter/nbviewer/master?logo=travis)](https://travis-ci.org/yuvipanda/nbresuse)
212
[![Build Status](https://dev.azure.com/tpaine154/jupyter/_apis/build/status/timkpaine.nbresuse?branchName=master)](https://dev.azure.com/tpaine154/jupyter/_build/latest?definitionId=17&branchName=master)
313
[![Coverage](https://img.shields.io/azure-devops/coverage/tpaine154/jupyter/17)](https://dev.azure.com/tpaine154/jupyter/_build?definitionId=17&_a=summary)
4-
[![PyPI](https://img.shields.io/pypi/l/nbresuse.svg)](https://pypi.python.org/pypi/nbresuse)
5-
[![PyPI](https://img.shields.io/pypi/v/nbresuse.svg)](https://pypi.python.org/pypi/nbresuse)
6-
714

815

916
![Screenshot with memory limit](screenshot.png)
1017

11-
NB Resource Usage (nbresuse) is a small extension for Jupyter Notebooks that
18+
NB Resource Usage (NBResuse) is a small extension for Jupyter Notebooks that
1219
displays an indication of how much resources your current notebook server and
1320
its children (kernels, terminals, etc) are using. This is displayed in the
1421
main toolbar in the notebook itself, refreshing every 5s.
@@ -80,9 +87,14 @@ As a command line argument:
8087
jupyter notebook --ResourceUseDisplay.track_cpu_percent=True
8188
```
8289

83-
## Resources displayed
90+
## Resources Displayed
8491

85-
Currently it only displays Memory usage (just RSS). Other metrics will be
92+
Currently the server extension only reports memory usage (just RSS) and CPU usage. Other metrics will be
8693
added in the future as needed.
8794

88-
The notebook extension currently doesn't show CPU usage.
95+
The notebook extension currently doesn't show CPU usage, only memory usage.
96+
97+
## Contributing
98+
99+
If you would like to contribute to the project, please read the [`CONTRIBUTING.md`](CONTRIBUTING.md). The `CONTRIBUTING.md` file
100+
explains how to set up a development installation and how to run the test suite.

nbresuse/__init__.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import os
22

33
from tornado import ioloop
4-
from traitlets import Bool, Float, Int, Union, default
4+
from traitlets import Bool
5+
from traitlets import default
6+
from traitlets import Float
7+
from traitlets import Int
8+
from traitlets import Union
59
from traitlets.config import Configurable
610

711
from nbresuse.prometheus import PrometheusHandler
@@ -17,21 +21,21 @@ def _jupyter_server_extension_paths():
1721
"""
1822
Set up the server extension for collecting metrics
1923
"""
20-
return [{
21-
'module': 'nbresuse',
22-
}]
24+
return [{"module": "nbresuse"}]
2325

2426

2527
def _jupyter_nbextension_paths():
2628
"""
2729
Set up the notebook extension for displaying metrics
2830
"""
29-
return [{
30-
"section": "notebook",
31-
"dest": "nbresuse",
32-
"src": "static",
33-
"require": "nbresuse/main"
34-
}]
31+
return [
32+
{
33+
"section": "notebook",
34+
"dest": "nbresuse",
35+
"src": "static",
36+
"require": "nbresuse/main",
37+
}
38+
]
3539

3640

3741
class ResourceUseDisplay(Configurable):
@@ -49,7 +53,7 @@ class ResourceUseDisplay(Configurable):
4953
we will start warning the user when they use (128 - (128 * 0.1)) MB.
5054
5155
Set to 0 to disable warning.
52-
"""
56+
""",
5357
).tag(config=True)
5458

5559
mem_limit = Union(
@@ -62,18 +66,18 @@ class ResourceUseDisplay(Configurable):
6266
6367
Defaults to reading from the `MEM_LIMIT` environment variable. If
6468
set to 0, the max memory available is displayed.
65-
"""
69+
""",
6670
).tag(config=True)
6771

68-
@default('mem_limit')
72+
@default("mem_limit")
6973
def _mem_limit_default(self):
70-
return int(os.environ.get('MEM_LIMIT', 0))
74+
return int(os.environ.get("MEM_LIMIT", 0))
7175

7276
track_cpu_percent = Bool(
7377
default_value=False,
7478
help="""
7579
Set to True in order to enable reporting of CPU usage statistics.
76-
"""
80+
""",
7781
).tag(config=True)
7882

7983
cpu_warning_threshold = Float(
@@ -86,7 +90,7 @@ def _mem_limit_default(self):
8690
we will start warning the user when they use (150 - (150 * 0.1)) %.
8791
8892
Set to 0 to disable warning.
89-
"""
93+
""",
9094
).tag(config=True)
9195

9296
cpu_limit = Float(
@@ -98,19 +102,19 @@ def _mem_limit_default(self):
98102
99103
Defaults to reading from the `CPU_LIMIT` environment variable. If
100104
set to 0, the total CPU count available is displayed.
101-
"""
105+
""",
102106
).tag(config=True)
103107

104-
@default('cpu_limit')
108+
@default("cpu_limit")
105109
def _cpu_limit_default(self):
106-
return float(os.environ.get('CPU_LIMIT', 0))
110+
return float(os.environ.get("CPU_LIMIT", 0))
107111

108112

109113
def load_jupyter_server_extension(nbapp):
110114
"""
111115
Called during notebook start
112116
"""
113117
resuseconfig = ResourceUseDisplay(parent=nbapp)
114-
nbapp.web_app.settings['nbresuse_display_config'] = resuseconfig
118+
nbapp.web_app.settings["nbresuse_display_config"] = resuseconfig
115119
callback = ioloop.PeriodicCallback(PrometheusHandler(nbapp), 1000)
116120
callback.start()

nbresuse/etc/nbextension.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
"nbresuse/main": true
44
}
55
}
6-

nbresuse/etc/serverextension.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55
}
66
}
77
}
8-

nbresuse/metrics.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ def memory_metrics() -> MemoryMetrics:
2020
rss = sum([p.memory_info().rss for p in all_processes])
2121
virtual_memory = psutil.virtual_memory()
2222

23-
return MemoryMetrics(
24-
rss,
25-
virtual_memory.total
26-
)
23+
return MemoryMetrics(rss, virtual_memory.total)
2724

2825

2926
def cpu_metrics() -> CPUMetrics:
@@ -39,9 +36,7 @@ def get_cpu_percent(p):
3936
# about dead processes having no CPU usage
4037
except BaseException:
4138
return 0
39+
4240
cpu_percent = sum([get_cpu_percent(p) for p in all_processes])
4341

44-
return CPUMetrics(
45-
cpu_count * 100.0,
46-
cpu_percent
47-
)
42+
return CPUMetrics(cpu_count * 100.0, cpu_percent)

nbresuse/prometheus.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,30 @@
22
from prometheus_client import Gauge
33
from tornado import gen
44

5-
from nbresuse.metrics import CPUMetrics, MemoryMetrics, cpu_metrics, memory_metrics
5+
from nbresuse.metrics import cpu_metrics
6+
from nbresuse.metrics import CPUMetrics
7+
from nbresuse.metrics import memory_metrics
8+
from nbresuse.metrics import MemoryMetrics
69

710
try:
811
# Traitlets >= 4.3.3
912
from traitlets import Callable
1013
except ImportError:
1114
from .utils import Callable
1215

13-
TOTAL_MEMORY_USAGE = Gauge(
14-
'total_memory_usage',
15-
'counter for total memory usage',
16-
[]
17-
)
16+
TOTAL_MEMORY_USAGE = Gauge("total_memory_usage", "counter for total memory usage", [])
1817

19-
MAX_MEMORY_USAGE = Gauge(
20-
'max_memory_usage',
21-
'counter for max memory usage',
22-
[]
23-
)
18+
MAX_MEMORY_USAGE = Gauge("max_memory_usage", "counter for max memory usage", [])
2419

25-
TOTAL_CPU_USAGE = Gauge(
26-
'total_cpu_usage',
27-
'counter for total cpu usage',
28-
[]
29-
)
20+
TOTAL_CPU_USAGE = Gauge("total_cpu_usage", "counter for total cpu usage", [])
3021

31-
MAX_CPU_USAGE = Gauge(
32-
'max_cpu_usage',
33-
'counter for max cpu usage',
34-
[]
35-
)
22+
MAX_CPU_USAGE = Gauge("max_cpu_usage", "counter for max cpu usage", [])
3623

3724

3825
class PrometheusHandler(Callable):
3926
def __init__(self, nbapp: NotebookApp):
4027
super().__init__()
41-
self.config = nbapp.web_app.settings['nbresuse_display_config']
28+
self.config = nbapp.web_app.settings["nbresuse_display_config"]
4229
self.session_manager = nbapp.session_manager
4330

4431
@gen.coroutine

0 commit comments

Comments
 (0)