MCPcopy Index your code
hub / github.com/ipython/ipython / test_matplotlib_positioning

Function test_matplotlib_positioning

tests/test_display_2.py:218–273  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

216
217@dec.skip_without("matplotlib")
218def test_matplotlib_positioning():
219 _ip = get_ipython()
220
221 prev_active_types = _ip.display_formatter.active_types.copy()
222 prev_execution_count = _ip.execution_count
223 prev_user_ns_underscore = _ip.user_ns.get("_", None)
224
225 _ip.history_manager.reset()
226 _ip.display_formatter.active_types = ["text/plain", "image/png"]
227
228 _ip.run_cell("import matplotlib")
229 prev_mpl_backend = _ip.run_cell("matplotlib.get_backend()").result
230
231 try:
232 _ip.run_line_magic("matplotlib", "inline")
233 _ip.execution_count = 1
234 _ip.run_cell("'no plot here'", store_history=True)
235
236 # Cell 2: No manual flush
237 _ip.run_cell(
238 "import matplotlib.pyplot as plt;plt.plot([0, 1])", store_history=True
239 )
240
241 _ip.run_cell("'no plot here'", store_history=True)
242
243 # Cell 4: Manual flush
244 _ip.run_cell("plt.plot([1, 0])\nplt.show()", store_history=True)
245
246 _ip.run_cell("'no plot here'", store_history=True)
247
248 outputs = _ip.history_manager.outputs
249
250 # Only cells 2 and 4 should have plots
251 for cell_num in [1, 3, 5]:
252 assert not any(
253 "image/png" in out.bundle for out in outputs.get(cell_num, [])
254 ), f"Cell {cell_num} should not have plot"
255
256 cell_2_has_plot = any("image/png" in out.bundle for out in outputs.get(2, []))
257 cell_4_has_plot = any("image/png" in out.bundle for out in outputs.get(4, []))
258
259 assert cell_2_has_plot, "Cell 2 should have plot (auto-flush)"
260 assert cell_4_has_plot, "Cell 4 should have plot (manual flush)"
261
262 finally:
263 _ip.run_cell("plt.close('all')")
264 _ip.run_line_magic("matplotlib", prev_mpl_backend)
265 _ip.history_manager.reset()
266 _ip.display_formatter.active_types = prev_active_types
267 _ip.displayhook.flush()
268
269 _ip.execution_count = prev_execution_count
270 if prev_user_ns_underscore is not None:
271 _ip.user_ns["_"] = prev_user_ns_underscore
272 elif "_" in _ip.user_ns:
273 del _ip.user_ns["_"]
274
275

Callers

nothing calls this directly

Calls 7

get_ipythonFunction · 0.90
getMethod · 0.80
run_line_magicMethod · 0.80
copyMethod · 0.45
resetMethod · 0.45
run_cellMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…