Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for optionally returning the Plotly JSON string #328

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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