Skip to content

Commit 22eb568

Browse files
committed
Fixed memory leak in plotly plot
1 parent 325816f commit 22eb568

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/tdamapper/_app.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from dataclasses import asdict, dataclass
44

55
import pandas as pd
6+
import plotly.graph_objects as go
67
from nicegui import app, run, ui
78
from sklearn.cluster import DBSCAN, AgglomerativeClustering, KMeans
89
from sklearn.datasets import load_digits, load_iris
@@ -83,6 +84,18 @@ class MapperConfig:
8384
clustering_agglomerative_n_clusters: int = CLUSTERING_AGGLOMERATIVE_N_CLUSTERS
8485

8586

87+
def empty_figure():
88+
fig = go.Figure()
89+
fig.update_layout(
90+
xaxis=dict(visible=False),
91+
yaxis=dict(visible=False),
92+
plot_bgcolor="rgba(0,0,0,0)",
93+
paper_bgcolor="rgba(0,0,0,0)",
94+
margin=dict(l=0, r=0, t=0, b=0),
95+
)
96+
return fig
97+
98+
8699
def fix_data(data):
87100
df = pd.DataFrame(data)
88101
df = df.select_dtypes(include="number")
@@ -453,6 +466,9 @@ def _init_clustering_settings(self):
453466

454467
def _init_plot(self):
455468
self.plot_container = ui.element("div").classes("w-full h-full")
469+
with self.plot_container:
470+
fig = empty_figure()
471+
self.draw_area = ui.plotly(fig).classes("w-full h-full")
456472

457473
def get_mapper_config(self):
458474
return MapperConfig(
@@ -575,10 +591,11 @@ async def async_run_mapper(self):
575591
mapper_fig.layout.autosize = True
576592
notification.message = "Done!"
577593
notification.spinner = False
594+
self.draw_area.clear()
578595
self.plot_container.clear()
579596
with self.plot_container:
580597
logger.info("Displaying Mapper plot.")
581-
ui.plotly(mapper_fig).classes("w-full h-full")
598+
self.draw_area = ui.plotly(mapper_fig).classes("w-full h-full")
582599
notification.dismiss()
583600

584601

0 commit comments

Comments
 (0)