Skip to content

Commit

Permalink
Handle empty string case for plotly json loading (bugfix) (#209)
Browse files Browse the repository at this point in the history
* Load json with try/catch, for malformed json

* Handle empty string input for plotly
  • Loading branch information
chungmin99 authored May 8, 2024
1 parent 3f38cee commit 7e5073d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/viser/_gui_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ def add_gui_plotly(
self._setup_plotly_js = True

# After plotly.min.js has been sent, we can send the plotly figure.
# Empty string for `plotly_json_str` is a signal to the client to render nothing.
self._get_api()._queue(
_messages.GuiAddPlotlyMessage(
order=handle._order,
Expand Down
3 changes: 3 additions & 0 deletions src/viser/client/src/components/PlotlyComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const PlotWithAspect = React.memo(function PlotWithAspect({
aspectRatio: number;
staticPlot: boolean;
}) {
// Catch if the jsonStr is empty; if so, render an empty div.
if (jsonStr === "") return <div></div>;

// Parse json string, to construct plotly object.
// Note that only the JSON string is kept as state, not the json object.
const plotJson = JSON.parse(jsonStr);
Expand Down

0 comments on commit 7e5073d

Please sign in to comment.