Skip to content

Commit

Permalink
Update repr code
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jun 26, 2022
1 parent 1d21f07 commit dc17b89
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions holoviews/pyodide.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
import asyncio
import sys

from panel.io.pyodide import write
from js import document

from bokeh.embed.elements import script_for_render_items
from bokeh.embed.util import standalone_docs_json_and_render_items
from bokeh.embed.wrappers import wrap_in_script_tag
from panel.io.pyodide import _link_docs
from panel.pane import panel as as_panel

from ..core.dimension import LabelledData
from ..core.options import Store
from ..util import extension as _extension

def write_html(obj):

#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------

async def _link(ref, doc):
from js import Bokeh
rendered = Bokeh.index.object_keys()
if ref not in rendered:
await asyncio.sleep(0.1)
await _link(ref, doc)
return
views = Bokeh.index.object_values()
view = views[rendered.indexOf(ref)]
_link_docs(doc, view.model.document)

def render_html(obj):
if hasattr(sys.stdout, '_out'):
out = sys.stdout._out # type: ignore
else:
raise ValueError("Could not determine target node to write to.")
asyncio.create_task(write(out, obj))
return {'text/plain': ''}, {}
doc = Document()
as_panel(obj).server_doc(doc, location=False)
docs_json, [render_item,] = standalone_docs_json_and_render_items(
doc.roots, suppress_callback_warning=True
)
for root in doc.roots:
render_item.roots._roots[root] = target
document.getElementById(target).classList.add('bk-root')
script = script_for_render_items(docs_json, [render_item])
asyncio.create_task(_link(doc.roots[0].ref['id'], doc))
return {'text/html': wrap_in_script_tag(script)}, {}

def write_image(element, fmt):
def render_image(element, fmt):
"""
Used to render elements to an image format (svg or png) if requested
in the display formats.
Expand All @@ -36,11 +67,16 @@ def write_image(element, fmt):
data, info = renderer(plot, fmt=fmt)
return {info['mime_type']: data}, {}

def write_png(obj):
return write_image(element, 'png')
def render_png(obj):
return render_image(element, 'png')

def render_svg(obj):
return render_image(element, 'svg')


def write_svg(obj):
return write_image(element, 'svg')
#-----------------------------------------------------------------------------
# Public API
#-----------------------------------------------------------------------------

class pyodide_extension(_extension):

Expand All @@ -50,7 +86,7 @@ def __call__(self, *args, **params):
super().__call__(*args, **params)
if not self._loaded:
Store.output_settings.initialize(list(Store.renderers.keys()))
Store.set_display_hook('html+js', LabelledData, write_output)
Store.set_display_hook('png', LabelledData, write_png)
Store.set_display_hook('svg', LabelledData, write_svg)
Store.set_display_hook('html+js', LabelledData, render_html)
Store.set_display_hook('png', LabelledData, render_png)
Store.set_display_hook('svg', LabelledData, render_svg)
pyodide_extension._loaded = True

0 comments on commit dc17b89

Please sign in to comment.