Skip to content

Commit ae88473

Browse files
authored
Merge pull request #61 from e2b-dev/skip-blank-figure
Omit blank figure
2 parents 371da53 + 6262c4d commit ae88473

File tree

13 files changed

+24
-1
lines changed

13 files changed

+24
-1
lines changed

.changeset/red-onions-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@e2b/data-extractor': patch
3+
---
4+
5+
Omit blank figures

chart_data_extractor/e2b_charts/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,22 @@ def get_chart_from_ax(
8888
return chart
8989

9090

91+
def is_figure_blank(axes: List[Axes]) -> bool:
92+
"""Check if a Matplotlib figure is blank (has no user-added artists)."""
93+
for ax in axes:
94+
if ax.has_data():
95+
return False # The figure contains user-added data
96+
return True # No data found, figure is blank
97+
98+
9199
def chart_figure_to_chart(figure: Figure) -> Optional[Chart]:
92100
"""
93101
This method is used to extract data from the figure object to a dictionary
94102
"""
95103
# Get all Axes objects from the Figure
96104
axes = figure.get_axes()
97105

98-
if not axes:
106+
if not axes or is_figure_blank(axes):
99107
return
100108
elif len(axes) > 1:
101109
return SuperChart(figure=figure)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import matplotlib.pyplot as plt
2+
3+
from e2b_charts import chart_figure_to_chart
4+
from e2b_charts.charts import BarChart, ChartType
5+
6+
7+
def test_blank_chart():
8+
figure, _ = plt.subplots()
9+
chart = chart_figure_to_chart(figure)
10+
assert chart is None

0 commit comments

Comments
 (0)