Skip to content

Commit 874bbae

Browse files
authored
V4 insitute code formatting standard using Black (plotly#1626)
- Formats the codebase using Black (https://black.readthedocs.io/en/stable/) - Updates codegen logic to use black instead of yapf for code formatting - Adds a pre-commit hook specification for running black prior to commit - Adds instructions to contributing.md for setting up pre-commit. - Adds a circleci test case to check that formatting is valid
1 parent 958a6b7 commit 874bbae

File tree

1,285 files changed

+98673
-116166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,285 files changed

+98673
-116166
lines changed

.circleci/config.yml

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
version: 2
22

33
jobs:
4+
check-code-formatting:
5+
docker:
6+
- image: circleci/python:3.7-stretch-node-browsers
7+
8+
steps:
9+
- checkout
10+
- run:
11+
name: Install black
12+
command: 'sudo pip install black'
13+
- run:
14+
name: Check formatting with black
15+
command: 'black --check .'
16+
417
# Core
518
python-2.7-core:
619
docker:
@@ -297,7 +310,7 @@ jobs:
297310
- checkout
298311
- run:
299312
name: Install tox
300-
command: 'sudo pip install tox requests yapf pytz decorator retrying inflect'
313+
command: 'sudo pip install tox requests black pytz decorator retrying inflect'
301314
- run:
302315
name: Update jupyterlab-plotly version
303316
command: 'cd packages/python/plotly; python setup.py updateplotlywidgetversion'
@@ -341,6 +354,9 @@ jobs:
341354

342355
workflows:
343356
version: 2
357+
code_formatting:
358+
jobs:
359+
- check-code-formatting
344360
dev_build:
345361
jobs:
346362
- plotlyjs_dev_build

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: https://github.com/ambv/black
3+
rev: stable
4+
hooks:
5+
- id: black
6+
language_version: python

contributing.md

+26
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ Here's what you need to know: changes to any files inside the following director
6464
- `packages/python/chart-studio/chart_studio/plotly/chunked_requests`
6565
- `packages/python/plotly/plotly/matplotlylib/mplexporter`
6666

67+
### Configure black code formatting
68+
This repo uses the [Black](https://black.readthedocs.io/en/stable/) code formatter,
69+
and the [pre-commit](https://pre-commit.com/) library to manage a git commit hook to
70+
run Black prior to each commit. Both pre-commit and black are included in the
71+
`packages/python/plotly/optional-requirements.txt` file, so you should have them
72+
installed already if you've been following along.
73+
74+
To enable the Black formatting git hook, run the following from within your virtual
75+
environment.
76+
77+
```bash
78+
(plotly_dev) $ pre-commit install
79+
```
80+
81+
Now, whenever you perform a commit, the Black formatter will run. If the formatter
82+
makes no changes, then the commit will proceed. But if the formatter does make changes,
83+
then the commit will abort. To proceed, stage the files that the formatter
84+
modified and commit again.
85+
86+
If you don't want to use `pre-commit`, then you can run black manually prior to making
87+
a PR as follows.
88+
89+
```bash
90+
(plotly_dev) $ black .
91+
```
92+
6793
### Making a Development Branch
6894

6995
Third, *don't* work in the `master` branch. As soon as you get your master branch ready, run:
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
from __future__ import absolute_import
2-
from chart_studio import (
3-
plotly,
4-
dashboard_objs,
5-
grid_objs,
6-
session,
7-
tools,
8-
)
2+
from chart_studio import plotly, dashboard_objs, grid_objs, session, tools

packages/python/chart-studio/chart_studio/api/utils.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def _to_native_string(string, encoding):
1212

1313

1414
def to_native_utf8_string(string):
15-
return _to_native_string(string, 'utf-8')
15+
return _to_native_string(string, "utf-8")
1616

1717

1818
def to_native_ascii_string(string):
19-
return _to_native_string(string, 'ascii')
19+
return _to_native_string(string, "ascii")
2020

2121

2222
def basic_auth(username, password):
@@ -31,11 +31,11 @@ def basic_auth(username, password):
3131
3232
"""
3333
if isinstance(username, str):
34-
username = username.encode('latin1')
34+
username = username.encode("latin1")
3535

3636
if isinstance(password, str):
37-
password = password.encode('latin1')
37+
password = password.encode("latin1")
3838

39-
return 'Basic ' + to_native_ascii_string(
40-
b64encode(b':'.join((username, password))).strip()
39+
return "Basic " + to_native_ascii_string(
40+
b64encode(b":".join((username, password))).strip()
4141
)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
from __future__ import absolute_import
22

3-
from chart_studio.api.v2 import (dash_apps, dashboards, files, folders, grids,
4-
images, plot_schema, plots,
5-
spectacle_presentations, users)
3+
from chart_studio.api.v2 import (
4+
dash_apps,
5+
dashboards,
6+
files,
7+
folders,
8+
grids,
9+
images,
10+
plot_schema,
11+
plots,
12+
spectacle_presentations,
13+
users,
14+
)

packages/python/chart-studio/chart_studio/api/v2/dash_apps.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55

66
from chart_studio.api.v2.utils import build_url, request
77

8-
RESOURCE = 'dash-apps'
8+
RESOURCE = "dash-apps"
99

1010

1111
def create(body):
1212
"""Create a dash app item."""
1313
url = build_url(RESOURCE)
14-
return request('post', url, json=body)
14+
return request("post", url, json=body)
1515

1616

1717
def retrieve(fid):
1818
"""Retrieve a dash app from Plotly."""
1919
url = build_url(RESOURCE, id=fid)
20-
return request('get', url)
20+
return request("get", url)
2121

2222

2323
def update(fid, content):
2424
"""Completely update the writable."""
2525
url = build_url(RESOURCE, id=fid)
26-
return request('put', url, json=content)
26+
return request("put", url, json=content)

packages/python/chart-studio/chart_studio/api/v2/dashboards.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,34 @@
88

99
from chart_studio.api.v2.utils import build_url, request
1010

11-
RESOURCE = 'dashboards'
11+
RESOURCE = "dashboards"
1212

1313

1414
def create(body):
1515
"""Create a dashboard."""
1616
url = build_url(RESOURCE)
17-
return request('post', url, json=body)
17+
return request("post", url, json=body)
1818

1919

2020
def list():
2121
"""Returns the list of all users' dashboards."""
2222
url = build_url(RESOURCE)
23-
return request('get', url)
23+
return request("get", url)
2424

2525

2626
def retrieve(fid):
2727
"""Retrieve a dashboard from Plotly."""
2828
url = build_url(RESOURCE, id=fid)
29-
return request('get', url)
29+
return request("get", url)
3030

3131

3232
def update(fid, content):
3333
"""Completely update the writable."""
3434
url = build_url(RESOURCE, id=fid)
35-
return request('put', url, json=content)
35+
return request("put", url, json=content)
3636

3737

3838
def schema():
3939
"""Retrieve the dashboard schema."""
40-
url = build_url(RESOURCE, route='schema')
41-
return request('get', url)
40+
url = build_url(RESOURCE, route="schema")
41+
return request("get", url)

packages/python/chart-studio/chart_studio/api/v2/files.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from chart_studio.api.v2.utils import build_url, make_params, request
55

6-
RESOURCE = 'files'
6+
RESOURCE = "files"
77

88

99
def retrieve(fid, share_key=None):
@@ -17,7 +17,7 @@ def retrieve(fid, share_key=None):
1717
"""
1818
url = build_url(RESOURCE, id=fid)
1919
params = make_params(share_key=share_key)
20-
return request('get', url, params=params)
20+
return request("get", url, params=params)
2121

2222

2323
def update(fid, body):
@@ -30,7 +30,7 @@ def update(fid, body):
3030
3131
"""
3232
url = build_url(RESOURCE, id=fid)
33-
return request('put', url, json=body)
33+
return request("put", url, json=body)
3434

3535

3636
def trash(fid):
@@ -41,8 +41,8 @@ def trash(fid):
4141
:returns: (requests.Response) Returns response directly from requests.
4242
4343
"""
44-
url = build_url(RESOURCE, id=fid, route='trash')
45-
return request('post', url)
44+
url = build_url(RESOURCE, id=fid, route="trash")
45+
return request("post", url)
4646

4747

4848
def restore(fid):
@@ -53,8 +53,8 @@ def restore(fid):
5353
:returns: (requests.Response) Returns response directly from requests.
5454
5555
"""
56-
url = build_url(RESOURCE, id=fid, route='restore')
57-
return request('post', url)
56+
url = build_url(RESOURCE, id=fid, route="restore")
57+
return request("post", url)
5858

5959

6060
def permanent_delete(fid):
@@ -65,8 +65,8 @@ def permanent_delete(fid):
6565
:returns: (requests.Response) Returns response directly from requests.
6666
6767
"""
68-
url = build_url(RESOURCE, id=fid, route='permanent_delete')
69-
return request('delete', url)
68+
url = build_url(RESOURCE, id=fid, route="permanent_delete")
69+
return request("delete", url)
7070

7171

7272
def lookup(path, parent=None, user=None, exists=None):
@@ -80,6 +80,6 @@ def lookup(path, parent=None, user=None, exists=None):
8080
:returns: (requests.Response) Returns response directly from requests.
8181
8282
"""
83-
url = build_url(RESOURCE, route='lookup')
83+
url = build_url(RESOURCE, route="lookup")
8484
params = make_params(path=path, parent=parent, user=user, exists=exists)
85-
return request('get', url, params=params)
85+
return request("get", url, params=params)

packages/python/chart-studio/chart_studio/api/v2/folders.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from chart_studio.api.v2.utils import build_url, make_params, request
55

6-
RESOURCE = 'folders'
6+
RESOURCE = "folders"
77

88

99
def create(body):
@@ -15,7 +15,7 @@ def create(body):
1515
1616
"""
1717
url = build_url(RESOURCE)
18-
return request('post', url, json=body)
18+
return request("post", url, json=body)
1919

2020

2121
def retrieve(fid, share_key=None):
@@ -29,7 +29,7 @@ def retrieve(fid, share_key=None):
2929
"""
3030
url = build_url(RESOURCE, id=fid)
3131
params = make_params(share_key=share_key)
32-
return request('get', url, params=params)
32+
return request("get", url, params=params)
3333

3434

3535
def update(fid, body):
@@ -42,7 +42,7 @@ def update(fid, body):
4242
4343
"""
4444
url = build_url(RESOURCE, id=fid)
45-
return request('put', url, json=body)
45+
return request("put", url, json=body)
4646

4747

4848
def trash(fid):
@@ -55,8 +55,8 @@ def trash(fid):
5555
:returns: (requests.Response) Returns response directly from requests.
5656
5757
"""
58-
url = build_url(RESOURCE, id=fid, route='trash')
59-
return request('post', url)
58+
url = build_url(RESOURCE, id=fid, route="trash")
59+
return request("post", url)
6060

6161

6262
def restore(fid):
@@ -69,8 +69,8 @@ def restore(fid):
6969
:returns: (requests.Response) Returns response directly from requests.
7070
7171
"""
72-
url = build_url(RESOURCE, id=fid, route='restore')
73-
return request('post', url)
72+
url = build_url(RESOURCE, id=fid, route="restore")
73+
return request("post", url)
7474

7575

7676
def permanent_delete(fid):
@@ -83,8 +83,8 @@ def permanent_delete(fid):
8383
:returns: (requests.Response) Returns response directly from requests.
8484
8585
"""
86-
url = build_url(RESOURCE, id=fid, route='permanent_delete')
87-
return request('delete', url)
86+
url = build_url(RESOURCE, id=fid, route="permanent_delete")
87+
return request("delete", url)
8888

8989

9090
def lookup(path, parent=None, user=None, exists=None):
@@ -98,6 +98,6 @@ def lookup(path, parent=None, user=None, exists=None):
9898
:returns: (requests.Response) Returns response directly from requests.
9999
100100
"""
101-
url = build_url(RESOURCE, route='lookup')
101+
url = build_url(RESOURCE, route="lookup")
102102
params = make_params(path=path, parent=parent, user=user, exists=exists)
103-
return request('get', url, params=params)
103+
return request("get", url, params=params)

0 commit comments

Comments
 (0)