Skip to content

Commit 6514cd2

Browse files
authored
Merge pull request #328 from European-XFEL/no-plotly
Add support for optionally returning the Plotly JSON string
2 parents 976dd51 + 3aaed41 commit 6514cd2

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

damnit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Prototype for extracting and showing metadata (AMORE project)"""
22

3-
__version__ = '0.1.3'
3+
__version__ = '0.1.4'
44

55
from .api import Damnit, RunVariables, VariableData

damnit/api.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ def _read_netcdf(self, one_array=False):
113113
if not k.startswith('_damnit_')}
114114
return obj
115115

116-
def read(self):
117-
"""Read the data for the variable."""
116+
def read(self, deserialize_plotly=True):
117+
"""Read the data for the variable.
118+
119+
Args:
120+
deserialize_plotly (bool): Whether to deserialize Plotly figures
121+
into `Figure` objects. If this is `False` the JSON string will be returned.
122+
"""
118123
if self._db_only:
119124
return self.summary()
120125

@@ -130,7 +135,8 @@ def read(self):
130135
import plotly.io as pio
131136
# plotly figures are json serialized and saved as uint8 arrays
132137
# to enable compression in HDF5
133-
return pio.from_json(dset[()].tobytes())
138+
byte_array = dset[()].tobytes()
139+
return pio.from_json(byte_array) if deserialize_plotly else byte_array.decode()
134140
elif h5py.check_string_dtype(dset.dtype) is not None:
135141
# Strings. Scalar/non-scalar strings need to be read differently.
136142
if dset.ndim == 0:

tests/test_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def dataset(run):
130130
assert isinstance(fig, PlotlyFigure)
131131
assert fig == px.bar(x=["a", "b", "c"], y=[1, 3, 2])
132132

133+
json_str = rv["plotly_mc_plotface"].read(deserialize_plotly=False)
134+
assert isinstance(json_str, str)
135+
133136
def test_api_dependencies(venv):
134137
package_path = Path(__file__).parent.parent
135138
venv.install(package_path)

0 commit comments

Comments
 (0)