diff --git a/phy/cluster/supervisor.py b/phy/cluster/supervisor.py index b8a8132f..759c823c 100644 --- a/phy/cluster/supervisor.py +++ b/phy/cluster/supervisor.py @@ -728,6 +728,10 @@ def _save_gui_state(self, gui): """Save the GUI state with the cluster view and similarity view.""" gui.state.update_view_state(self.cluster_view, self.cluster_view.state) + # Clear temporary HTML files + self.cluster_view.clear_temporary_files() + self.similarity_view.clear_temporary_files() + def _get_similar_clusters(self, sender, cluster_id): """Return the clusters similar to a given cluster.""" sim = self.similarity(cluster_id) or [] diff --git a/phy/gui/qt.py b/phy/gui/qt.py index b786af19..c189335b 100644 --- a/phy/gui/qt.py +++ b/phy/gui/qt.py @@ -13,7 +13,9 @@ import os import os.path as op from pathlib import Path +import shutil import sys +import tempfile from timeit import default_timer import traceback @@ -466,8 +468,21 @@ def set_html(self, html, callback=None): self._callback = callback self.loadFinished.connect(self._loadFinished) static_dir = str(Path(__file__).parent / 'static') + '/' - base_url = QUrl().fromLocalFile(static_dir) - self.page().setHtml(html, base_url) + + # Create local file from HTML + self.clear_temporary_files() + self._tempdir = Path(tempfile.mkdtemp()) + shutil.copytree(static_dir, self._tempdir / 'html') + file_path = self._tempdir / 'html' / 'page.html' + with open(file_path, 'w') as f: + f.write(html) + file_url = QUrl().fromLocalFile(str(file_path)) + self.page().setUrl(file_url) + + def clear_temporary_files(self): + """Delete the temporary HTML files""" + if hasattr(self, '_tempdir') and self._tempdir.is_dir(): + shutil.rmtree(self._tempdir, ignore_errors=True) def _callable(self, data): self.html = data