mirror of
https://github.com/harvard-edge/cs249r_book.git
synced 2026-05-01 01:59:10 -05:00
fix(vscode-ext): force matplotlib Agg backend more aggressively
Use force=True, switch_backend(), and close("all") after execution
to prevent plot windows from appearing during Python value resolution.
This commit is contained in:
@@ -111,12 +111,19 @@ function buildResolverScript(pythonSource: string, varNames: string[]): string {
|
|||||||
// We also suppress plt.show() so it's a no-op.
|
// We also suppress plt.show() so it's a no-op.
|
||||||
const lines = [
|
const lines = [
|
||||||
'import sys, json, os',
|
'import sys, json, os',
|
||||||
|
'# Force non-interactive backend BEFORE any matplotlib import.',
|
||||||
|
'# Setting the env var ensures even lazy imports pick up Agg.',
|
||||||
'os.environ["MPLBACKEND"] = "Agg"',
|
'os.environ["MPLBACKEND"] = "Agg"',
|
||||||
|
'os.environ["MATPLOTLIB_BACKEND"] = "Agg"',
|
||||||
'try:',
|
'try:',
|
||||||
' import matplotlib',
|
' import matplotlib',
|
||||||
' matplotlib.use("Agg")',
|
' matplotlib.use("Agg", force=True)',
|
||||||
' import matplotlib.pyplot as _plt',
|
' import matplotlib.pyplot as _plt',
|
||||||
|
' _plt.switch_backend("Agg")',
|
||||||
' _plt.show = lambda *a, **k: None',
|
' _plt.show = lambda *a, **k: None',
|
||||||
|
' _plt.pause = lambda *a, **k: None',
|
||||||
|
' # Close any figures that might have been created during import',
|
||||||
|
' _plt.close("all")',
|
||||||
'except ImportError:',
|
'except ImportError:',
|
||||||
' pass',
|
' pass',
|
||||||
'',
|
'',
|
||||||
@@ -133,6 +140,13 @@ function buildResolverScript(pythonSource: string, varNames: string[]): string {
|
|||||||
'# Execute the QMD Python blocks',
|
'# Execute the QMD Python blocks',
|
||||||
pythonSource,
|
pythonSource,
|
||||||
'',
|
'',
|
||||||
|
'# Close any figures created during execution',
|
||||||
|
'try:',
|
||||||
|
' import matplotlib.pyplot as _plt2',
|
||||||
|
' _plt2.close("all")',
|
||||||
|
'except Exception:',
|
||||||
|
' pass',
|
||||||
|
'',
|
||||||
'# Collect variable values',
|
'# Collect variable values',
|
||||||
'_results = {}',
|
'_results = {}',
|
||||||
captures,
|
captures,
|
||||||
|
|||||||
Reference in New Issue
Block a user