Skip to content

Commit 0589e43

Browse files
authored
Merge pull request #4526 from geopozo/feature/pikul-local-updateplotlyjsdev
Add support for local `plotly.js/` builds to `setup.py`
2 parents 3488799 + 406c744 commit 0589e43

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

contributing.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,30 @@ the `plotly/plotly.js` GitHub repository (and place them in
224224
`plotly/package_data`). It will then regenerate all of the `graph_objs`
225225
classes based on the new schema.
226226

227-
For dev branches, it is also possible to use `updateplotlyjsdev --devrepo reponame --devbranch branchname` to update to development versions of `plotly.js`. This will fetch the `plotly.js` in the CircleCI artifact of the branch `branchname` of the repo `reponame`. If `--devrepo` or `--devbranch` are omitted, `updateplotlyjsdev` defaults using `plotly/plotly.js` and `master` respectively.
227+
For dev branches, it is also possible to use `updateplotlyjsdev` in two configurations:
228+
229+
### CircleCI Release
230+
231+
If your devbranch is part of the official plotly.js repository, you can use
232+
```bash
233+
python setup.py updateplotlyjsdev --devrepo reponame --devbranch branchname
234+
```
235+
to update to development versions of `plotly.js`. This will fetch the `plotly.js` in the CircleCI artifact of the branch `branchname` of the repo `reponame`. If `--devrepo` or `--devbranch` are omitted, `updateplotlyjsdev` defaults using `plotly/plotly.js` and `master` respectively.
236+
237+
### Local Repository
238+
239+
If you have a local repository of `plotly.js` you'd like to try, you can run:
240+
241+
```bash
242+
# In your plotly.js/ directory, prepare the package:
243+
244+
$ npm run build
245+
$ npm pack
246+
$ mv plotly.js-*.tgz plotly.js.tgz
247+
248+
# In your plotly.py/packages/python/plotly/ directory:
249+
$ python setup.py updateplotlyjsdev --local /path/to/your/plotly.js/
250+
```
228251

229252
## Testing
230253

packages/python/plotly/setup.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import sys
3+
import time
34
import platform
45
import json
6+
import shutil
57

68
from setuptools import setup, Command
79
from setuptools.command.egg_info import egg_info
@@ -137,10 +139,10 @@ class NPM(Command):
137139
]
138140

139141
def initialize_options(self):
140-
pass
142+
self.local = None
141143

142144
def finalize_options(self):
143-
pass
145+
self.set_undefined_options("updateplotlyjsdev", ("local", "local"))
144146

145147
def get_npm_name(self):
146148
npmName = "npm"
@@ -187,6 +189,14 @@ def run(self):
187189
stdout=sys.stdout,
188190
stderr=sys.stderr,
189191
)
192+
if self.local is not None:
193+
plotly_archive = os.path.join(self.local, "plotly.js.tgz")
194+
check_call(
195+
[npmName, "install", plotly_archive],
196+
cwd=node_root,
197+
stdout=sys.stdout,
198+
stderr=sys.stderr,
199+
)
190200
check_call(
191201
[npmName, "run", "build:prod"],
192202
cwd=node_root,
@@ -225,6 +235,11 @@ def run(self):
225235
perform_codegen()
226236

227237

238+
def overwrite_schema_local(uri):
239+
path = os.path.join(here, "codegen", "resources", "plot-schema.json")
240+
shutil.copyfile(uri, path)
241+
242+
228243
def overwrite_schema(url):
229244
import requests
230245

@@ -235,6 +250,11 @@ def overwrite_schema(url):
235250
f.write(req.content)
236251

237252

253+
def overwrite_bundle_local(uri):
254+
path = os.path.join(here, "plotly", "package_data", "plotly.min.js")
255+
shutil.copyfile(uri, path)
256+
257+
238258
def overwrite_bundle(url):
239259
import requests
240260

@@ -304,6 +324,13 @@ def get_latest_publish_build_info(repo, branch):
304324
return {p: build[p] for p in ["vcs_revision", "build_num", "committer_date"]}
305325

306326

327+
def get_bundle_schema_local(local):
328+
plotly_archive = os.path.join(local, "plotly.js.tgz")
329+
plotly_bundle = os.path.join(local, "dist/plotly.min.js")
330+
plotly_schemas = os.path.join(local, "dist/plot-schema.json")
331+
return plotly_archive, plotly_bundle, plotly_schemas
332+
333+
307334
def get_bundle_schema_urls(build_num):
308335
url = (
309336
"https://circleci.com/api/v1.1/project/github/"
@@ -390,31 +417,47 @@ class UpdateBundleSchemaDevCommand(Command):
390417
def initialize_options(self):
391418
self.devrepo = None
392419
self.devbranch = None
420+
self.local = None
393421

394422
def finalize_options(self):
395423
self.set_undefined_options("updateplotlyjsdev", ("devrepo", "devrepo"))
396424
self.set_undefined_options("updateplotlyjsdev", ("devbranch", "devbranch"))
425+
self.set_undefined_options("updateplotlyjsdev", ("local", "local"))
397426

398427
def run(self):
399-
build_info = get_latest_publish_build_info(self.devrepo, self.devbranch)
428+
if self.local is None:
429+
build_info = get_latest_publish_build_info(self.devrepo, self.devbranch)
400430

401-
archive_url, bundle_url, schema_url = get_bundle_schema_urls(
402-
build_info["build_num"]
403-
)
431+
archive_url, bundle_url, schema_url = get_bundle_schema_urls(
432+
build_info["build_num"]
433+
)
434+
435+
# Update bundle in package data
436+
overwrite_bundle(bundle_url)
404437

405-
# Update bundle in package data
406-
overwrite_bundle(bundle_url)
438+
# Update schema in package data
439+
overwrite_schema(schema_url)
440+
else:
441+
# this info could be more informative but
442+
# it doesn't seem as useful in a local context
443+
# and requires dependencies and programming.
444+
build_info = {"vcs_revision": "local", "committer_date": str(time.time())}
445+
self.devrepo = self.local
446+
self.devbranch = ""
407447

408-
# Update schema in package data
409-
overwrite_schema(schema_url)
448+
archive_uri, bundle_uri, schema_uri = get_bundle_schema_local(self.local)
449+
overwrite_bundle_local(bundle_uri)
450+
overwrite_schema_local(schema_uri)
410451

411452
# Update plotly.js url in package.json
412453
package_json_path = os.path.join(node_root, "package.json")
413454
with open(package_json_path, "r") as f:
414455
package_json = json.load(f)
415456

416457
# Replace version with bundle url
417-
package_json["dependencies"]["plotly.js"] = archive_url
458+
package_json["dependencies"]["plotly.js"] = (
459+
archive_url if self.local is None else archive_uri
460+
)
418461
with open(package_json_path, "w") as f:
419462
json.dump(package_json, f, indent=2)
420463

@@ -430,11 +473,13 @@ class UpdatePlotlyJsDevCommand(Command):
430473
user_options = [
431474
("devrepo=", None, "Repository name"),
432475
("devbranch=", None, "branch or pull/number"),
476+
("local=", None, "local copy of repo, used by itself"),
433477
]
434478

435479
def initialize_options(self):
436480
self.devrepo = "plotly/plotly.js"
437481
self.devbranch = "master"
482+
self.local = None
438483

439484
def finalize_options(self):
440485
pass

0 commit comments

Comments
 (0)