Skip to content

Commit 07bc2bf

Browse files
authored
Merge pull request #5588 from plotly/cam/5562/add-default-kaleido-headers
fix: Add default request headers to Kaleido calls
2 parents dd9d2e5 + 8b7bac4 commit 07bc2bf

6 files changed

Lines changed: 226 additions & 1179 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1111
- Fix issue where user-specified `color_continuous_scale` was ignored when template had `autocolorscale=True` [[#5439](https://github.com/plotly/plotly.py/pull/5439)], with thanks to @antonymilne for the contribution!
1212
- Use presence of `COLAB_NOTEBOOK_ID` env var to enable Colab renderer instead of testing import of `google.colab` [[#5473](https://github.com/plotly/plotly.py/pull/5473)], with thanks to @kevineger for the contribution!
1313
- Update tests to be compatible with numpy 2.4 [[#5522](https://github.com/plotly/plotly.py/pull/5522)], with thanks to @thunze for the contribution!
14+
- Add default headers to be passed in to Kaleido v1.3.0 to avoid blocked Open Street Map tiles [#5588](https://github.com/plotly/plotly.py/pull/5588)]
1415

1516
### Updated
1617
- The `__eq__` method for `graph_objects` classes now returns `NotImplemented` to give the other operand an opportunity to handle the comparison [[#5547](https://github.com/plotly/plotly.py/pull/5547)], with thanks to @RazerM for the contribution!

commands.py

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -149,34 +149,20 @@ def overwrite_plotlyjs_version_file(plotlyjs_version):
149149
)
150150

151151

152-
def request_json(url):
153-
"""Get JSON data from a URL."""
152+
def get_latest_commit_info(repo, branch):
153+
"""Get latest commit info from GitHub API."""
154154

155+
url = "https://api.github.com/repos/{repo}/commits/{branch}".format(
156+
repo=repo, branch=branch
157+
)
155158
req = requests.get(url)
156-
return json.loads(req.content.decode("utf-8"))
157-
158-
159-
def get_latest_publish_build_info(repo, branch):
160-
"""Get build info from Circle CI."""
161-
162-
url = (
163-
r"https://circleci.com/api/v1.1/project/github/"
164-
r"{repo}/tree/{branch}?limit=100&filter=completed"
165-
).format(repo=repo, branch=branch)
166-
167-
branch_jobs = request_json(url)
168-
169-
# Get most recent successful publish build for branch
170-
builds = [
171-
j
172-
for j in branch_jobs
173-
if j.get("workflows", {}).get("job_name", None) == "publish-dist"
174-
and j.get("status", None) == "success"
175-
]
176-
build = builds[0]
159+
assert req.status_code == 200, "Failed to fetch commit info: %s" % req.text
160+
commit = req.json()
177161

178-
# Extract build info
179-
return {p: build[p] for p in ["vcs_revision", "build_num", "committer_date"]}
162+
return {
163+
"vcs_revision": commit["sha"],
164+
"committer_date": commit["commit"]["committer"]["date"],
165+
}
180166

181167

182168
def get_bundle_schema_local(local):
@@ -188,29 +174,15 @@ def get_bundle_schema_local(local):
188174
return plotly_archive, plotly_bundle, plotly_schemas
189175

190176

191-
def get_bundle_schema_urls(build_num):
192-
"""Get URLs for required files."""
193-
194-
url = (
195-
"https://circleci.com/api/v1.1/project/github/"
196-
"plotly/plotly.js/{build_num}/artifacts"
197-
).format(build_num=build_num)
198-
199-
artifacts = request_json(url)
200-
201-
# Find archive
202-
archives = [a for a in artifacts if a.get("path", None) == "plotly.js.tgz"]
203-
archive = archives[0]
204-
205-
# Find bundle
206-
bundles = [a for a in artifacts if a.get("path", None) == "dist/plotly.min.js"]
207-
bundle = bundles[0]
177+
def get_github_urls(repo, revision):
178+
"""Get URLs for required files from GitHub."""
208179

209-
# Find schema
210-
schemas = [a for a in artifacts if a.get("path", None) == "dist/plot-schema.json"]
211-
schema = schemas[0]
180+
raw = f"https://raw.githubusercontent.com/{repo}/{revision}"
181+
archive_url = f"https://github.com/{repo}/tarball/{revision}"
182+
bundle_url = raw + "/dist/plotly.min.js"
183+
schema_url = raw + "/dist/plot-schema.json"
212184

213-
return archive["url"], bundle["url"], schema["url"]
185+
return archive_url, bundle_url, schema_url
214186

215187

216188
def update_schema(plotly_js_version):
@@ -249,9 +221,9 @@ def update_plotlyjs(plotly_js_version, outdir):
249221
def update_schema_bundle_from_master(args):
250222
"""Update the plotly.js schema and bundle from master."""
251223
if args.local is None:
252-
build_info = get_latest_publish_build_info(args.devrepo, args.devbranch)
253-
archive_url, bundle_url, schema_url = get_bundle_schema_urls(
254-
build_info["build_num"]
224+
build_info = get_latest_commit_info(args.devrepo, args.devbranch)
225+
archive_url, bundle_url, schema_url = get_github_urls(
226+
args.devrepo, build_info["vcs_revision"]
255227
)
256228

257229
# Update bundle in package data

plotly/io/_defaults.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def __init__(self):
1414
self.mathjax = None
1515
self.topojson = None
1616
self.plotlyjs = None
17+
# This header is necessary to comply with Open Street Map tile policy:
18+
# https://openstreetmap.github.io/owg-website/policies/tiles/#31-identification
19+
self.headers = {"X-Requested-With": "plotly.py"}
1720

1821

1922
defaults = _Defaults()

plotly/io/_kaleido.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ def to_image(
375375
kopts["plotlyjs"] = defaults.plotlyjs
376376
if defaults.mathjax:
377377
kopts["mathjax"] = defaults.mathjax
378+
if defaults.headers:
379+
kopts["headers"] = defaults.headers
378380

379381
width = (
380382
width
@@ -712,6 +714,8 @@ def write_images(
712714
kopts["plotlyjs"] = defaults.plotlyjs
713715
if defaults.mathjax:
714716
kopts["mathjax"] = defaults.mathjax
717+
if defaults.headers:
718+
kopts["headers"] = defaults.headers
715719
kaleido.write_fig_from_object_sync(
716720
kaleido_specs,
717721
kopts=kopts,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies = [
4141

4242
[project.optional-dependencies]
4343
express = ["numpy>=1.22"]
44-
kaleido = ["kaleido>=1.1.0"]
44+
kaleido = ["kaleido>=1.3.0"]
4545
dev_core = [
4646
"pytest",
4747
"requests",

0 commit comments

Comments
 (0)