Skip to content

Commit

Permalink
Allow plotly.scope to pass debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ayjayt committed Oct 24, 2024
1 parent f9964d2 commit 677c271
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/py/kaleido/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@

_scope_flags_ = ("plotlyjs", "mathjax", "topojson", "mapbox_access_token")

def to_image_block(spec, f=None, topojson=None, mapbox_token=None):
def to_image_block(spec, f=None, topojson=None, mapbox_token=None, debug=False):
try:
_ = asyncio.get_running_loop()
from threading import Thread
image = None
def get_image():
nonlocal image
image = asyncio.run(to_image(spec, f, topojson, mapbox_token))
image = asyncio.run(to_image(spec, f, topojson, mapbox_token, debug=debug))
t = Thread(target=get_image)
t.start()
t.join()
return image
except RuntimeError:
pass
return asyncio.run(to_image(spec, f, topojson, mapbox_token))
return asyncio.run(to_image(spec, f, topojson, mapbox_token, debug=debug))

async def to_image(spec, f=None, topojson=None, mapbox_token=None):
async with Browser(headless=True) as browser:
async def to_image(spec, f=None, topojson=None, mapbox_token=None, debug=False):
async with Browser(headless=True, debug=debug, debug_browser=debug) as browser:
if not f:
f = script_path.absolute()
tab = await browser.create_tab(f.as_uri())
Expand Down
9 changes: 6 additions & 3 deletions src/py/kaleido/scopes/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class PlotlyScope():
_scope_flags = kaleido._scope_flags_


def __init__(self, plotlyjs=None, mathjax=None, topojson=None, mapbox_access_token=None, **kwargs):
def __init__(self, plotlyjs=None, mathjax=None, topojson=None, mapbox_access_token=None, debug=False, **kwargs):
self.debug=debug
# TODO: #2 This is deprecated, this whole FILE is deprecated
self._plotlyjs = plotlyjs
self._topojson = topojson
Expand Down Expand Up @@ -159,7 +160,7 @@ def make_spec(self, figure, format=None, width=None, height=None, scale=None):
js_args = dict(format=format, width=width, height=height, scale=scale)
return dict(js_args, data = figure)

def transform(self, figure, format=None, width=None, height=None, scale=None):
def transform(self, figure, format=None, width=None, height=None, scale=None, debug=None):
"""
Convert a Plotly figure into a static image
Expand Down Expand Up @@ -189,12 +190,14 @@ def transform(self, figure, format=None, width=None, height=None, scale=None):
If not specified, will default to the `scope.default_scale` property
:return: image bytes
"""
if not debug:
debug=self.debug
spec = self.make_spec(figure, format=format, width=width, height=height, scale=scale)

# Write to process and read result within a lock so that can be
# sure we're reading the response to our request
with _proc_lock:
img = kaleido.to_image_block(spec, Path(self._tempfile.name).absolute(), self._topojson, self._mapbox_access_token)
img = kaleido.to_image_block(spec, Path(self._tempfile.name).absolute(), self._topojson, self._mapbox_access_token, debug=debug)

return img

Expand Down

0 comments on commit 677c271

Please sign in to comment.