Skip to content

Commit cae3dd8

Browse files
committed
Move image env set up to composite action, update Kaleido
1 parent f59d26d commit cae3dd8

File tree

5 files changed

+127
-100
lines changed

5 files changed

+127
-100
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: 'Setup Image Environment'
2+
description: 'Setup Python, uv, and install Kaleido/plotly with required fonts'
3+
4+
inputs:
5+
python-version:
6+
description: 'Python version to use'
7+
default: '3.12'
8+
uv-version:
9+
description: 'uv version to use'
10+
default: '0.11.2'
11+
12+
runs:
13+
using: 'composite'
14+
steps:
15+
- uses: actions/setup-python@v6
16+
with:
17+
python-version: ${{ inputs.python-version }}
18+
19+
- name: Install uv
20+
uses: astral-sh/setup-uv@v7
21+
with:
22+
version: ${{ inputs.uv-version }}
23+
24+
- name: Install Kaleido, plotly.io and required fonts
25+
run: .github/scripts/env_image.sh
26+
shell: bash

.github/scripts/env_image.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ sudo apt install fontconfig
99
sudo fc-cache -f
1010

1111
# install Kaleido & Plotly
12-
sudo python3 -m pip install kaleido==0.2.1 plotly==6.6.0 --progress-bar off
12+
uv pip install kaleido==1.2 plotly==6.6.0 --no-progress
1313

1414
# install numpy i.e. to convert arrays to typed arrays
15-
sudo python3 -m pip install numpy==2.4.3
15+
uv pip install numpy==2.4.3
16+
17+
# verify version of python and versions of installed python packages
18+
python --version
19+
uv pip freeze

.github/scripts/test.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,24 @@ case $1 in
103103
find $ROOT/test/image/mocks/gl* -type f -printf "%f\n"; \
104104
find $ROOT/test/image/mocks/map* -type f -printf "%f\n"; \
105105
} | sed 's/\.json$//1' | sort | node "$SPLIT")
106-
sudo python3 test/image/make_baseline.py virtual-webgl $SUITE || EXIT_STATE=$?
106+
uv run test/image/make_baseline.py virtual-webgl $SUITE || EXIT_STATE=$?
107107
exit $EXIT_STATE
108108
;;
109109

110110
make-baselines-mathjax3)
111-
sudo python3 test/image/make_baseline.py mathjax3 legend_mathjax_title_and_items mathjax parcats_grid_subplots table_latex_multitrace_scatter table_plain_birds table_wrapped_birds ternary-mathjax ternary-mathjax-title-place-subtitle || EXIT_STATE=$?
111+
uv run test/image/make_baseline.py mathjax3 legend_mathjax_title_and_items mathjax parcats_grid_subplots table_latex_multitrace_scatter table_plain_birds table_wrapped_birds ternary-mathjax ternary-mathjax-title-place-subtitle || EXIT_STATE=$?
112112
exit $EXIT_STATE
113113
;;
114114

115115
make-baselines-b64)
116116
SUITE=$(find $ROOT/test/image/mocks/ -type f -printf "%f\n" | sed 's/\.json$//1' | sort | node "$SPLIT")
117-
sudo python3 test/image/make_baseline.py b64 $SUITE || EXIT_STATE=$?
117+
uv run test/image/make_baseline.py b64 $SUITE || EXIT_STATE=$?
118118
exit $EXIT_STATE
119119
;;
120120

121121
make-baselines)
122122
SUITE=$(find $ROOT/test/image/mocks/ -type f -printf "%f\n" | sed 's/\.json$//1' | sort | node "$SPLIT")
123-
sudo python3 test/image/make_baseline.py $SUITE || EXIT_STATE=$?
123+
uv run test/image/make_baseline.py $SUITE || EXIT_STATE=$?
124124
exit $EXIT_STATE
125125
;;
126126

.github/workflows/ci.yml

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,7 @@ jobs:
253253
- uses: actions/checkout@v6
254254
- uses: ./.github/actions/setup-workspace
255255

256-
- uses: actions/setup-python@v6
257-
with:
258-
python-version: '3.12'
259-
260-
- name: Install Kaleido, plotly.io and required fonts
261-
run: .github/scripts/env_image.sh
256+
- uses: ./.github/actions/setup-image-env
262257

263258
- name: Create all png files
264259
run: .github/scripts/test.sh make-baselines
@@ -320,12 +315,7 @@ jobs:
320315
- uses: actions/checkout@v6
321316
- uses: ./.github/actions/setup-workspace
322317

323-
- uses: actions/setup-python@v6
324-
with:
325-
python-version: '3.12'
326-
327-
- name: Install kaleido, plotly.io and required fonts
328-
run: .github/scripts/env_image.sh
318+
- uses: ./.github/actions/setup-image-env
329319

330320
- name: Create all png files (b64)
331321
run: .github/scripts/test.sh make-baselines-b64
@@ -387,12 +377,7 @@ jobs:
387377
- uses: actions/checkout@v6
388378
- uses: ./.github/actions/setup-workspace
389379

390-
- uses: actions/setup-python@v6
391-
with:
392-
python-version: '3.12'
393-
394-
- name: Install kaleido, plotly.io and required fonts
395-
run: .github/scripts/env_image.sh
380+
- uses: ./.github/actions/setup-image-env
396381

397382
- name: Create png files (virtual-webgl)
398383
run: .github/scripts/test.sh make-baselines-virtual-webgl
@@ -447,12 +432,7 @@ jobs:
447432
- uses: actions/checkout@v6
448433
- uses: ./.github/actions/setup-workspace
449434

450-
- uses: actions/setup-python@v6
451-
with:
452-
python-version: '3.12'
453-
454-
- name: Install kaleido, plotly.io and required fonts
455-
run: .github/scripts/env_image.sh
435+
- uses: ./.github/actions/setup-image-env
456436

457437
- name: Create mathjax v3 png files
458438
run: .github/scripts/test.sh make-baselines-mathjax3
@@ -509,12 +489,7 @@ jobs:
509489
- uses: actions/checkout@v6
510490
- uses: ./.github/actions/setup-workspace
511491

512-
- uses: actions/setup-python@v6
513-
with:
514-
python-version: '3.12'
515-
516-
- name: Install kaleido, plotly.io and required fonts
517-
run: .github/scripts/env_image.sh
492+
- uses: ./.github/actions/setup-image-env
518493

519494
- name: Install poppler-utils for eps export
520495
run: |

test/image/make_baseline.py

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import asyncio
2+
import json
3+
import kaleido
14
import os
25
import sys
3-
import json
4-
import plotly.io as pio
56
from convert_b64 import arraysToB64
67

78
args = []
@@ -20,10 +21,11 @@
2021
root, "build", "plotly_with_virtual-webgl.js"
2122
)
2223

24+
topojson = "file://" + os.path.join(root, "topojson", "dist")
2325
dirIn = os.path.join(root, "test", "image", "mocks")
2426
dirOut = os.path.join(root, "build", "test_images")
2527

26-
# N.B. equal is the falg to write to baselines not test_images
28+
# N.B. equal is the flag to write to baselines not test_images
2729

2830
if "=" in args:
2931
args = args[args.index("=") + 1 :]
@@ -35,13 +37,13 @@
3537
print("output to", dirOut)
3638

3739
mathjax_version = 2
40+
mathjax = None
3841
if "mathjax3" in sys.argv or "mathjax3=" in sys.argv:
3942
# until https://github.com/plotly/Kaleido/issues/124 is addressed
4043
# we are uanble to use local mathjax v3 installed in node_modules
4144
# for now let's download it from the internet:
42-
pio.kaleido.scope.mathjax = (
43-
"https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js"
44-
)
45+
mathjax = "https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js"
46+
4547
mathjax_version = 3
4648
print("Kaleido using MathJax v3")
4749

@@ -58,10 +60,6 @@
5860

5961
plotlyjs = plotlyjs_with_virtual_webgl
6062

61-
pio.kaleido.scope.plotlyjs = plotlyjs
62-
pio.kaleido.scope.topojson = "file://" + os.path.join(root, "topojson", "dist")
63-
pio.templates.default = "none"
64-
6563
ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith(".json")]
6664
ALL_MOCKS.sort()
6765

@@ -80,57 +78,81 @@
8078
sys.exit(1)
8179

8280
failed = []
83-
for name in allNames:
84-
outName = name
85-
if mathjax_version == 3:
86-
outName = "mathjax3___" + name
87-
88-
print(outName)
89-
90-
created = False
91-
92-
MAX_RETRY = 2 # 1 means retry once
93-
for attempt in range(0, MAX_RETRY + 1):
94-
with open(os.path.join(dirIn, name + ".json"), "r") as _in:
95-
fig = json.load(_in)
96-
97-
width = 700
98-
height = 500
99-
if "layout" in fig:
100-
layout = fig["layout"]
101-
if "autosize" not in layout or layout["autosize"] != True:
102-
if "width" in layout:
103-
width = layout["width"]
104-
if "height" in layout:
105-
height = layout["height"]
106-
107-
if "b64" in sys.argv or "b64=" in sys.argv or "b64-json" in sys.argv:
108-
newFig = dict()
109-
arraysToB64(fig, newFig)
110-
fig = newFig
111-
if "b64-json" in sys.argv and attempt == 0:
112-
print(json.dumps(fig, indent=2))
113-
114-
try:
115-
pio.write_image(
116-
fig=fig,
117-
file=os.path.join(dirOut, outName + ".png"),
118-
width=width,
119-
height=height,
120-
validate=False,
121-
)
122-
created = True
123-
except Exception as e:
124-
print(e)
125-
if attempt < MAX_RETRY:
126-
print("retry", attempt + 1, "/", MAX_RETRY)
127-
else:
128-
failed.append(outName)
129-
130-
if created:
131-
break
132-
133-
if len(failed) > 0:
134-
print("Failed at :")
135-
print(failed)
136-
sys.exit(1)
81+
82+
83+
async def make_baselines_async():
84+
85+
kopts = dict(
86+
plotlyjs=plotlyjs,
87+
)
88+
if mathjax is not None:
89+
kopts["mathjax"] = mathjax
90+
91+
async with kaleido.Kaleido(n=1, **kopts) as k:
92+
for name in allNames:
93+
outName = name
94+
if mathjax_version == 3:
95+
outName = "mathjax3___" + name
96+
97+
print(outName)
98+
99+
created = False
100+
101+
MAX_RETRY = 2 # 1 means retry once
102+
for attempt in range(0, MAX_RETRY + 1):
103+
with open(os.path.join(dirIn, name + ".json"), "r") as _in:
104+
fig = json.load(_in)
105+
106+
width = 700
107+
height = 500
108+
if "layout" in fig:
109+
layout = fig["layout"]
110+
if "autosize" not in layout or layout["autosize"] != True:
111+
if "width" in layout:
112+
width = layout["width"]
113+
if "height" in layout:
114+
height = layout["height"]
115+
116+
if (
117+
"b64" in sys.argv
118+
or "b64=" in sys.argv
119+
or "b64-json" in sys.argv
120+
):
121+
newFig = dict()
122+
arraysToB64(fig, newFig)
123+
fig = newFig
124+
if "b64-json" in sys.argv and attempt == 0:
125+
print(json.dumps(fig, indent=2))
126+
127+
try:
128+
bytes = await k.calc_fig(
129+
fig,
130+
opts=dict(
131+
format="png",
132+
width=width,
133+
height=height,
134+
),
135+
topojson=topojson,
136+
)
137+
filename = os.path.join(dirOut, outName + ".png")
138+
with open(filename, "wb") as f:
139+
f.write(bytes)
140+
created = True
141+
except Exception as e:
142+
print(e)
143+
if attempt < MAX_RETRY:
144+
print("retry", attempt + 1, "/", MAX_RETRY)
145+
else:
146+
failed.append(outName)
147+
148+
if created:
149+
break
150+
151+
if len(failed) > 0:
152+
print("Failed at :")
153+
print(failed)
154+
sys.exit(1)
155+
156+
157+
if __name__ == "__main__":
158+
asyncio.run(make_baselines_async())

0 commit comments

Comments
 (0)