Skip to content

Commit f71796a

Browse files
committed
Add the toyplot.hou module making it easier to display Toyplot plots in Houdini (https://sidefx.com).
1 parent 6a5b6b0 commit f71796a

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

docs/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Python API
1919
toyplot.data.rst
2020
toyplot.font.rst
2121
toyplot.format.rst
22+
toyplot.hou.rst
2223
toyplot.html.rst
2324
toyplot.layout.rst
2425
toyplot.locator.rst

docs/toyplot.hou.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
toyplot.hou module
2+
==================
3+
4+
.. automodule:: toyplot.hou
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

toyplot/hou.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)