Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
seeM committed Apr 12, 2024
1 parent ccd0637 commit 77ee954
Showing 1 changed file with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@ def __call__(self, msg: Dict[str, Any]) -> Optional[Dict[str, Any]]:
# can restore the context for future renderings. We construct
# a new plot comm to advise the client of the new figure.
pickled = self._pickle_current_figure()
if pickled is not None:
id = str(uuid.uuid4())
self.figures[id] = pickled
if pickled is None:
logger.warning("No figure ")
return msg

id = str(uuid.uuid4())
self.figures[id] = pickled

# Creating a comm per plot figure allows the client
# to request new renderings of each plot at a later time,
# e.g. on resizing the plots view
self._create_comm(id)
# Creating a comm per plot figure allows the client
# to request new renderings of each plot at a later time,
# e.g. on resizing the plots view
self._create_comm(id)

# Returning None implies our hook has processed the message
# and it stops the parent from sending the display_data via
# the standard iopub channel
return None
# Returning None implies our hook has processed the message
# and it stops the parent from sending the display_data via
# the standard iopub channel
return None

def _create_comm(self, comm_id: str) -> None:
"""
Expand Down Expand Up @@ -134,34 +137,32 @@ def shutdown(self) -> None:
# -- Private Methods --

def _pickle_current_figure(self) -> Optional[str]:
pickled = None
figure = None

# Delay importing matplotlib until the kernel and shell has been initialized
# otherwise the graphics backend will be reset to the gui
import matplotlib.pyplot as plt

# We turn off interactive mode before accessing the plot context
was_interactive = plt.isinteractive()
plt.ioff()
with plt.ioff():
# Check to see if there are any figures left in stack to display
# If not, get the number of figures to display from matplotlib
if len(self.fignums) == 0:
self.fignums = plt.get_fignums()

# Check to see if there are any figures left in stack to display
# If not, get the number of figures to display from matplotlib
if len(self.fignums) == 0:
self.fignums = plt.get_fignums()
if len(self.fignums) == 0:
logger.warning("Hook called without a figure to display")
return None

# Get the current figure, remove from it from being called next hook
if len(self.fignums) > 0:
# Get the current figure, remove it from displayed in the next call
figure = plt.figure(self.fignums.pop(0))

# Pickle the current figure
if figure is not None and not self._is_figure_empty(figure):
pickled = codecs.encode(pickle.dumps(figure), "base64").decode()
if self._is_figure_empty(figure):
logger.warning("Figure is empty")
return None

if was_interactive:
plt.ion()
# Pickle the current figure
pickled = codecs.encode(pickle.dumps(figure), "base64").decode()

return pickled
return pickled

def _resize_pickled_figure(
self,
Expand Down

0 comments on commit 77ee954

Please sign in to comment.