|
| 1 | +# Copyright 2014, Sandia Corporation. Under the terms of Contract |
| 2 | +# DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain |
| 3 | +# rights in this software. |
| 4 | + |
| 5 | +"""Functionality for displaying a Toyplot canvas in a Houdini help browser.""" |
| 6 | + |
| 7 | + |
| 8 | +import collections |
| 9 | + |
| 10 | +def show(canvases, title="Toyplot Figure"): |
| 11 | + """Display one or more canvases in a Houdini (https://sidefx.com) help browser. |
| 12 | +
|
| 13 | + Uses Toyplot's preferred HTML+SVG+Javascript backend to display one-or-more |
| 14 | + interactive canvases in a floating help browser in Houdini (https://sidefx.com). |
| 15 | +
|
| 16 | + Because the canvases are displayed in a separate web browser process, this |
| 17 | + function returns immediately. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + canvases: :class:`toyplot.canvas.Canvas` instance or sequence of :class:`toyplot.canvas.Canvas` instances. |
| 22 | + The canvases to be displayed. |
| 23 | +
|
| 24 | + title: string, optional |
| 25 | + Optional page title to be displayed by the browser. |
| 26 | + """ |
| 27 | + |
| 28 | + import os |
| 29 | + import tempfile |
| 30 | + import xml.etree.ElementTree as xml |
| 31 | + |
| 32 | + import hou |
| 33 | + |
| 34 | + import toyplot.canvas |
| 35 | + import toyplot.html |
| 36 | + |
| 37 | + if not isinstance(canvases, (toyplot.canvas.Canvas, collections.abc.Iterable)): |
| 38 | + raise ValueError("Expected one or more instances of %s, received %s." % (toyplot.canvas.Canvas, type(canvases))) # pragma: no cover |
| 39 | + |
| 40 | + if isinstance(canvases, toyplot.canvas.Canvas): |
| 41 | + canvases = [canvases] |
| 42 | + |
| 43 | + html = xml.Element("html") |
| 44 | + head = xml.SubElement(html, "head") |
| 45 | + xml.SubElement(head, "title").text = title |
| 46 | + body = xml.SubElement(html, "body") |
| 47 | + for canvas in canvases: |
| 48 | + body.append(toyplot.html.render(canvas)) |
| 49 | + |
| 50 | + fd, path = tempfile.mkstemp(suffix=".html") |
| 51 | + with os.fdopen(fd, "wb") as stream: |
| 52 | + stream.write(xml.tostring(html, method="html")) |
| 53 | + |
| 54 | + browser = hou.ui.curDesktop().createFloatingPaneTab(hou.paneTabType.HelpBrowser) |
| 55 | + browser.setUrl(f"file://{path}") |
| 56 | + |
| 57 | + return browser |
0 commit comments