Skip to content

Commit

Permalink
Merge pull request #106 from Carifio24/fix-image-qt-export-crash
Browse files Browse the repository at this point in the history
Fix technical issues with Qt image exporter
  • Loading branch information
Carifio24 authored Nov 24, 2024
2 parents 23a79c9 + 4763d32 commit 3cefb1e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
17 changes: 16 additions & 1 deletion glue_plotly/common/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def layout_config(viewer):
showlegend=True)


def get_mpl_renderer(figure):
if hasattr(figure.canvas, "get_renderer"):
return figure.canvas.get_renderer()
elif hasattr(figure, "_get_renderer"):
return figure._get_renderer()
return None


def axes_data_from_mpl(viewer):
axes_data = {}

Expand All @@ -58,10 +66,17 @@ def axes_data_from_mpl(viewer):

for helper in axes.coords:
ticks = helper.ticks
ticklabels = helper.ticklabels
if hasattr(helper, '_ticklabels'):
# We need to use the private attribute due to a bug in astropy 7
# See https://github.com/astropy/astropy/pull/17444
ticklabels = helper._ticklabels
else:
ticklabels = helper.ticklabels
for axis in ticklabels.get_visible_axes():
ax = 'x' if axis in ['t', 'b'] else 'y'
ax_idx = 0 if ax == 'x' else 1
if not hasattr(ticks, "ticks_locs"):
ticks.draw(get_mpl_renderer(viewer.figure))
locations = sorted([loc[0][ax_idx] for loc in ticks.ticks_locs[axis]])
labels = ticklabels.text[axis]
if ax == 'y':
Expand Down
22 changes: 12 additions & 10 deletions glue_plotly/html_exporters/qt/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,13 @@ class PlotlyImage2DExport(Tool):
tool_tip = 'Save Plotly HTML page'

@messagebox_on_error(PLOTLY_ERROR_MESSAGE)
def _export_to_plotly(self, filename, checked_dictionary):
def _export_to_plotly(self, filename, checked_dictionary, config):

layers = layers_to_export(self.viewer)
add_data_label = data_count(layers) > 1

config = layout_config(self.viewer)

# TODO: Need to determine how to makes axes from bqplot
ax = axes_data_from_mpl(self.viewer)
config.update(**ax)
secondary_x = 'xaxis2' in ax
secondary_y = 'yaxis2' in ax
config["showlegend"] = len(layers) > 1
secondary_x = 'xaxis2' in config
secondary_y = 'yaxis2' in config

if secondary_x or secondary_y:
fig = make_subplots(specs=[[{"secondary_y": True}]], horizontal_spacing=0, vertical_spacing=0)
Expand Down Expand Up @@ -87,7 +81,15 @@ def activate(self):
if not filename:
return

worker = Worker(self._export_to_plotly, filename, checked_dictionary)
# It would be better to create the layout config in `_export_to_plotly`.
# But we get some of our axis font sizes from matplotlib, some of which seems to cause problems
# when run in a QThread. So for now, we get the layout config here.
config = layout_config(self.viewer)
ax = axes_data_from_mpl(self.viewer)
config.update(**ax)
config["showlegend"] = len(layers) > 1

worker = Worker(self._export_to_plotly, filename, checked_dictionary, config)
exp_dialog = export_dialog.ExportDialog(parent=self.viewer)
worker.result.connect(exp_dialog.close)
worker.error.connect(exp_dialog.close)
Expand Down

0 comments on commit 3cefb1e

Please sign in to comment.