|
| 1 | +import os |
| 2 | +import sys |
| 3 | +_all_formats = ("png", "jpg", "jpeg", "webp", "svg", "pdf", "json") |
| 4 | +dirname="./test-results/" |
| 5 | +os.makedirs(dirname, exist_ok=True) |
| 6 | +os.environ["KALEIDO-DEBUG"] = "true" |
| 7 | + |
| 8 | +total = len(_all_formats) * 5 |
| 9 | +x = 0 |
| 10 | +def count(): |
| 11 | + global x |
| 12 | + x = x + 1 |
| 13 | + print(f"\r{x}/{total}", end = "\r") |
| 14 | + |
| 15 | +with open(dirname+"log.log", 'w') as sys.stderr: |
| 16 | + print("scope-old".center(50, "&"), file=sys.stderr) |
| 17 | + debug=True |
| 18 | + import plotly.graph_objects as go |
| 19 | + fig = go.Figure(data=[go.Scatter(y=[1, 3, 2])], layout=dict(title="$$\\text{Test} \\pi$$")) # whole thing needs to be mathjax? |
| 20 | + figgl = go.Figure(data=[go.Scattergl(y=[1, 3, 2])], layout=dict(title="$$\\text{Test} \\pi$$")) # whole thing needs to be mathjax? |
| 21 | + |
| 22 | + from kaleido.scopes.plotly import PlotlyScope |
| 23 | + scope = PlotlyScope(debug=debug, |
| 24 | + # plotlyjs="https://cdn.plot.ly/plotly-latest.min.js", |
| 25 | + # plotlyjs="/path/to/local/plotly.js", |
| 26 | + ) |
| 27 | + for extension in _all_formats: |
| 28 | + try: |
| 29 | + print(f"Trying non-gl: {extension} w/ transform".center(40, "*"), file=sys.stderr) |
| 30 | + with open(dirname+"figure-scope-old."+extension, "wb") as f: |
| 31 | + f.write(scope.transform(fig, format=extension)) |
| 32 | + count() |
| 33 | + print(f"Trying gl: {extension} w/ transform".center(40, "*"), file=sys.stderr) |
| 34 | + with open(dirname+"figure-scope-old-gl."+extension, "wb") as f: |
| 35 | + f.write(scope.transform(figgl, format=extension)) |
| 36 | + count() |
| 37 | + |
| 38 | + except Exception as e: |
| 39 | + print(e, file=sys.stderr) |
| 40 | + |
| 41 | + import asyncio |
| 42 | + print("ASYNC w/ PRETEND BLOCKING".center(50, "&"), file=sys.stderr) |
| 43 | + |
| 44 | + async def test_with_blocking_in_async(): |
| 45 | + for extension in _all_formats: |
| 46 | + try: |
| 47 | + print(f"Trying: {extension} w/ transform".center(40, "*"), file=sys.stderr) |
| 48 | + with open(dirname+"figure-async-block."+extension, "wb") as f: |
| 49 | + f.write(scope.transform(fig, format=extension)) |
| 50 | + count() |
| 51 | + except Exception as e: |
| 52 | + print(e, file=sys.stderr) |
| 53 | + |
| 54 | + asyncio.run(test_with_blocking_in_async()) |
| 55 | + print("ASYNC w/ ASYNC NATIVE".center(50, "&"), file=sys.stderr) |
| 56 | + |
| 57 | + |
| 58 | + import kaleido |
| 59 | + from kaleido.scopes.plotly import PlotlyScope |
| 60 | + async def test_with_async(): |
| 61 | + for extension in _all_formats: |
| 62 | + try: |
| 63 | + print(f"Trying: {extension} w/ transform async".center(40, "*"), file=sys.stderr) |
| 64 | + spec = scope.make_spec(fig, format=extension) |
| 65 | + with open(dirname+"figure-async-native."+extension, "wb") as f: |
| 66 | + f.write(await kaleido.to_image(spec, debug=debug)) |
| 67 | + count() |
| 68 | + except Exception as e: |
| 69 | + print(e, file=sys.stderr) |
| 70 | + asyncio.run(test_with_async()) |
| 71 | + ## Other |
| 72 | + |
| 73 | + print("express-write".center(50, "&"), file=sys.stderr) |
| 74 | + |
| 75 | + import plotly.express as px |
| 76 | + fig = px.scatter(px.data.iris(), x="sepal_length", y="sepal_width", color="species") |
| 77 | + fig.update_layout(dict(title="$$\\text{Test} \\pi$$")) |
| 78 | + for extension in _all_formats: |
| 79 | + try: |
| 80 | + print(f"Trying: {extension} w/ write_image".center(40, "*"), file=sys.stderr) |
| 81 | + fig.write_image(dirname+"figure-express." + extension, engine="kaleido") |
| 82 | + count() |
| 83 | + except Exception as e: |
| 84 | + print(e, file=sys.stderr) |
| 85 | + print("Done!") |
| 86 | + print(f"Please check {dirname}log.log") |
| 87 | + |
0 commit comments