Skip to content

Commit 8adf354

Browse files
Copilotjnumainville
andcommitted
Apply black formatting to subplots code and tests
Co-authored-by: jnumainville <[email protected]>
1 parent 74d3ae4 commit 8adf354

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

plugins/plotly-express/src/deephaven/plot/express/plots/subplots.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ def extract_title_from_figure(fig: Figure | DeephavenFigure) -> str | None:
226226
if plotly_fig is None:
227227
return None
228228
fig = plotly_fig
229-
229+
230230
layout = fig.to_dict().get("layout", {})
231231
title = layout.get("title")
232-
232+
233233
if title is None:
234234
return None
235-
235+
236236
# Title can be either a string or a dict with a 'text' key
237237
if isinstance(title, dict):
238238
return title.get("text")
@@ -264,21 +264,21 @@ def create_subplot_annotations(
264264
265265
"""
266266
annotations = []
267-
267+
268268
for idx, title in enumerate(titles):
269269
if not title: # Skip empty titles
270270
continue
271-
271+
272272
# Calculate row and col from index (row-major order, but reversed since grid is reversed)
273273
row = idx // cols
274274
col = idx % cols
275-
275+
276276
# Calculate x position (center of column)
277277
x = (col_starts[col] + col_ends[col]) / 2
278-
278+
279279
# Calculate y position (top of row with small offset)
280280
y = row_ends[row]
281-
281+
282282
annotation = {
283283
"text": title,
284284
"showarrow": False,
@@ -288,11 +288,11 @@ def create_subplot_annotations(
288288
"y": y,
289289
"xanchor": "center",
290290
"yanchor": "bottom",
291-
"font": {"size": 16}
291+
"font": {"size": 16},
292292
}
293-
293+
294294
annotations.append(annotation)
295-
295+
296296
return annotations
297297

298298

@@ -380,10 +380,10 @@ def atomic_make_subplots(
380380

381381
# Handle subplot titles
382382
final_subplot_titles: list[str] = []
383-
383+
384384
if use_existing_titles and subplot_titles is not None:
385385
raise ValueError("Cannot use both use_existing_titles and subplot_titles")
386-
386+
387387
if use_existing_titles:
388388
# Extract titles from existing figures
389389
for fig_row in grid:
@@ -392,16 +392,20 @@ def atomic_make_subplots(
392392
final_subplot_titles.append("")
393393
else:
394394
extracted_title = extract_title_from_figure(fig)
395-
final_subplot_titles.append(extracted_title if extracted_title else "")
395+
final_subplot_titles.append(
396+
extracted_title if extracted_title else ""
397+
)
396398
elif subplot_titles is not None:
397399
# Convert to list if tuple
398400
final_subplot_titles = list(subplot_titles)
399-
401+
400402
# Pad with empty strings if needed
401403
total_subplots = rows * cols
402404
if len(final_subplot_titles) < total_subplots:
403-
final_subplot_titles.extend([""] * (total_subplots - len(final_subplot_titles)))
404-
405+
final_subplot_titles.extend(
406+
[""] * (total_subplots - len(final_subplot_titles))
407+
)
408+
405409
# Create the custom update function to add annotations and title
406410
def custom_update_figure(fig: Figure) -> Figure:
407411
# Add subplot title annotations if any
@@ -415,15 +419,17 @@ def custom_update_figure(fig: Figure) -> Figure:
415419
rows,
416420
cols,
417421
)
418-
422+
419423
# Get existing annotations if any
420-
existing_annotations = list(fig.layout.annotations) if fig.layout.annotations else []
424+
existing_annotations = (
425+
list(fig.layout.annotations) if fig.layout.annotations else []
426+
)
421427
fig.update_layout(annotations=existing_annotations + annotations)
422-
428+
423429
# Add overall title if provided
424430
if title:
425431
fig.update_layout(title=title)
426-
432+
427433
# Apply user's unsafe_update_figure if provided
428434
result = unsafe_update_figure(fig)
429435
return result if result is not None else fig

plugins/plotly-express/test/deephaven/plot/express/plots/test_make_subplots.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -584,16 +584,16 @@ def test_make_subplots_with_subplot_titles(self):
584584
layout = charts["plotly"]["layout"]
585585
self.assertIn("annotations", layout)
586586
annotations = layout["annotations"]
587-
587+
588588
# Should have 2 annotations for 2 subplot titles
589589
self.assertEqual(len(annotations), 2)
590-
590+
591591
# Check first annotation
592592
self.assertEqual(annotations[0]["text"], "Plot 1")
593593
self.assertEqual(annotations[0]["xref"], "paper")
594594
self.assertEqual(annotations[0]["yref"], "paper")
595595
self.assertFalse(annotations[0]["showarrow"])
596-
596+
597597
# Check second annotation
598598
self.assertEqual(annotations[1]["text"], "Plot 2")
599599

@@ -609,7 +609,7 @@ def test_make_subplots_with_empty_subplot_titles(self):
609609
layout = charts["plotly"]["layout"]
610610
self.assertIn("annotations", layout)
611611
annotations = layout["annotations"]
612-
612+
613613
# Should have 2 annotations (empty string skipped)
614614
self.assertEqual(len(annotations), 2)
615615
self.assertEqual(annotations[0]["text"], "Plot 1")
@@ -628,7 +628,7 @@ def test_make_subplots_with_use_existing_titles(self):
628628
layout = charts["plotly"]["layout"]
629629
self.assertIn("annotations", layout)
630630
annotations = layout["annotations"]
631-
631+
632632
# Should have 2 annotations
633633
self.assertEqual(len(annotations), 2)
634634
self.assertEqual(annotations[0]["text"], "First Chart")
@@ -638,9 +638,9 @@ def test_make_subplots_with_overall_title(self):
638638
import src.deephaven.plot.express as dx
639639

640640
chart = dx.scatter(self.source, x="X", y="Y")
641-
charts = dx.make_subplots(
642-
chart, chart, rows=2, title="Overall Title"
643-
).to_dict(self.exporter)
641+
charts = dx.make_subplots(chart, chart, rows=2, title="Overall Title").to_dict(
642+
self.exporter
643+
)
644644

645645
# Check that the overall title was added
646646
layout = charts["plotly"]["layout"]
@@ -652,18 +652,20 @@ def test_make_subplots_with_subplot_titles_and_overall_title(self):
652652

653653
chart = dx.scatter(self.source, x="X", y="Y")
654654
charts = dx.make_subplots(
655-
chart, chart, rows=2,
655+
chart,
656+
chart,
657+
rows=2,
656658
subplot_titles=["Plot 1", "Plot 2"],
657-
title="Combined Plot"
659+
title="Combined Plot",
658660
).to_dict(self.exporter)
659661

660662
# Check that both subplot titles and overall title were added
661663
layout = charts["plotly"]["layout"]
662-
664+
663665
# Check overall title
664666
self.assertIn("title", layout)
665667
self.assertEqual(layout["title"], "Combined Plot")
666-
668+
667669
# Check subplot titles
668670
self.assertIn("annotations", layout)
669671
annotations = layout["annotations"]
@@ -678,17 +680,17 @@ def test_make_subplots_use_existing_titles_with_grid(self):
678680
chart2 = dx.scatter(self.source, x="X", y="Y", title="Chart B")
679681
chart3 = dx.scatter(self.source, x="X", y="Y", title="Chart C")
680682
chart4 = dx.scatter(self.source, x="X", y="Y", title="Chart D")
681-
683+
682684
grid = [[chart1, chart2], [chart3, chart4]]
683-
charts = dx.make_subplots(
684-
grid=grid, use_existing_titles=True
685-
).to_dict(self.exporter)
685+
charts = dx.make_subplots(grid=grid, use_existing_titles=True).to_dict(
686+
self.exporter
687+
)
686688

687689
# Check that annotations were added with extracted titles
688690
layout = charts["plotly"]["layout"]
689691
self.assertIn("annotations", layout)
690692
annotations = layout["annotations"]
691-
693+
692694
# Should have 4 annotations
693695
self.assertEqual(len(annotations), 4)
694696
self.assertEqual(annotations[0]["text"], "Chart A")
@@ -700,15 +702,17 @@ def test_make_subplots_conflicting_title_options(self):
700702
import src.deephaven.plot.express as dx
701703

702704
chart = dx.scatter(self.source, x="X", y="Y")
703-
705+
704706
# Should raise error when both use_existing_titles and subplot_titles are provided
705707
with self.assertRaises(ValueError) as context:
706708
dx.make_subplots(
707-
chart, chart, rows=2,
709+
chart,
710+
chart,
711+
rows=2,
708712
subplot_titles=["Plot 1", "Plot 2"],
709-
use_existing_titles=True
713+
use_existing_titles=True,
710714
)
711-
715+
712716
self.assertIn("Cannot use both", str(context.exception))
713717

714718

0 commit comments

Comments
 (0)