Skip to content

Commit

Permalink
Merge pull request #328 from European-XFEL/no-plotly
Browse files Browse the repository at this point in the history
Add support for optionally returning the Plotly JSON string
  • Loading branch information
JamesWrigley authored Sep 3, 2024
2 parents 976dd51 + 3aaed41 commit 6514cd2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion damnit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Prototype for extracting and showing metadata (AMORE project)"""

__version__ = '0.1.3'
__version__ = '0.1.4'

from .api import Damnit, RunVariables, VariableData
12 changes: 9 additions & 3 deletions damnit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ def _read_netcdf(self, one_array=False):
if not k.startswith('_damnit_')}
return obj

def read(self):
"""Read the data for the variable."""
def read(self, deserialize_plotly=True):
"""Read the data for the variable.
Args:
deserialize_plotly (bool): Whether to deserialize Plotly figures
into `Figure` objects. If this is `False` the JSON string will be returned.
"""
if self._db_only:
return self.summary()

Expand All @@ -130,7 +135,8 @@ def read(self):
import plotly.io as pio
# plotly figures are json serialized and saved as uint8 arrays
# to enable compression in HDF5
return pio.from_json(dset[()].tobytes())
byte_array = dset[()].tobytes()
return pio.from_json(byte_array) if deserialize_plotly else byte_array.decode()
elif h5py.check_string_dtype(dset.dtype) is not None:
# Strings. Scalar/non-scalar strings need to be read differently.
if dset.ndim == 0:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def dataset(run):
assert isinstance(fig, PlotlyFigure)
assert fig == px.bar(x=["a", "b", "c"], y=[1, 3, 2])

json_str = rv["plotly_mc_plotface"].read(deserialize_plotly=False)
assert isinstance(json_str, str)

def test_api_dependencies(venv):
package_path = Path(__file__).parent.parent
venv.install(package_path)
Expand Down

0 comments on commit 6514cd2

Please sign in to comment.