Skip to content

Commit 62f0d78

Browse files
chriswmackeyChris Mackey
authored andcommitted
fix(cli): Support loading HTML and vtkjs output to stdout
1 parent b8ca691 commit 62f0d78

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

honeybee_display/cli/__init__.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import logging
66
import json
77
import pickle
8+
import tempfile
9+
import uuid
810

911
from honeybee.model import Model
1012
from honeybee.cli import main
@@ -116,8 +118,7 @@ def display():
116118
'file and the distinction between the two is only for help in coordinating file '
117119
'extensions (since both .vsf and .json can be acceptable). Also note that '
118120
'ladybug-vtk must be installed in order for the vtkjs or html options to be usable '
119-
'and the html format refers to a web page with the vtkjs file embedded within it. '
120-
'The vtkjs and html options also require an explicit --output-file to be specified.',
121+
'and the html format refers to a web page with the vtkjs file embedded within it.',
121122
type=str, default='vsf', show_default=True)
122123
@click.option(
123124
'--output-file', help='Optional file to output the JSON string of '
@@ -170,14 +171,16 @@ def model_to_vis_set(
170171
else:
171172
output_file.write(pickle.dumps(vis_set.to_dict()))
172173
elif output_format in ('vtkjs', 'html'):
173-
assert output_file.name != '<stdout>', \
174-
'Must specify an --output-file to use --output-format vtkjs.'
175-
out_folder, out_file = os.path.split(output_file.name)
176-
try:
174+
if output_file.name == '<stdout>': # get a temporary file
175+
out_file = str(uuid.uuid4())[:6]
176+
out_folder = tempfile.gettempdir()
177+
else:
178+
out_folder, out_file = os.path.split(output_file.name)
177179
if out_file.endswith('.vtkjs'):
178180
out_file = out_file[:-6]
179181
elif out_file.endswith('.html'):
180182
out_file = out_file[:-5]
183+
try:
181184
if output_format == 'vtkjs':
182185
vis_set.to_vtkjs(output_folder=out_folder, file_name=out_file)
183186
if output_format == 'html':
@@ -186,6 +189,16 @@ def model_to_vis_set(
186189
raise AttributeError(
187190
'Ladybug-vtk must be installed in order to use --output-format '
188191
'vtkjs.\n{}'.format(ae))
192+
if output_file.name == '<stdout>': # load file contents to stdout
193+
out_file_ext = out_file + '.' + output_format
194+
out_file_path = os.path.join(out_folder, out_file_ext)
195+
if output_format == 'html':
196+
with open(out_file_path, encoding='utf-8') as of:
197+
f_contents = of.read()
198+
else: # vtkjs can only be read as binary
199+
with open(out_file_path, 'rb') as of:
200+
f_contents = of.read()
201+
output_file.write(f_contents)
189202
else:
190203
raise ValueError('Unrecognized output-format "{}".'.format(output_format))
191204
except Exception as e:

0 commit comments

Comments
 (0)