60
60
changed using the ``plot_html_show_source_link`` variable in
61
61
:file:`conf.py` (which itself defaults to True).
62
62
63
- ``:encoding:`` : str
64
- If this source file is in a non-UTF8 or non-ASCII encoding, the
65
- encoding must be specified using the ``:encoding:`` option. The
66
- encoding will not be inferred using the ``-*- coding -*-`` metacomment.
67
-
68
63
``:context:`` : bool or str
69
64
If provided, the code will be run in the context of all previous plot
70
65
directives for which the ``:context:`` option was specified. This only
166
161
import matplotlib
167
162
from matplotlib .backend_bases import FigureManagerBase
168
163
import matplotlib .pyplot as plt
169
- from matplotlib import _api , _pylab_helpers , cbook
164
+ from matplotlib import _pylab_helpers , cbook
170
165
171
166
matplotlib .use ("agg" )
172
167
@@ -200,11 +195,6 @@ def _option_format(arg):
200
195
return directives .choice (arg , ('python' , 'doctest' ))
201
196
202
197
203
- def _deprecated_option_encoding (arg ):
204
- _api .warn_deprecated ("3.5" , name = "encoding" , obj_type = "option" )
205
- return directives .encoding (arg )
206
-
207
-
208
198
def mark_plot_labels (app , document ):
209
199
"""
210
200
To make plots referenceable, we need to move the reference from the
@@ -254,7 +244,6 @@ class PlotDirective(Directive):
254
244
'format' : _option_format ,
255
245
'context' : _option_context ,
256
246
'nofigs' : directives .flag ,
257
- 'encoding' : _deprecated_option_encoding ,
258
247
'caption' : directives .unchanged ,
259
248
}
260
249
@@ -316,47 +305,25 @@ def contains_doctest(text):
316
305
return bool (m )
317
306
318
307
319
- @_api .deprecated ("3.5" , alternative = "doctest.script_from_examples" )
320
- def unescape_doctest (text ):
321
- """
322
- Extract code from a piece of text, which contains either Python code
323
- or doctests.
324
- """
325
- if not contains_doctest (text ):
326
- return text
327
- code = ""
328
- for line in text .split ("\n " ):
329
- m = re .match (r'^\s*(>>>|\.\.\.) (.*)$' , line )
330
- if m :
331
- code += m .group (2 ) + "\n "
332
- elif line .strip ():
333
- code += "# " + line .strip () + "\n "
334
- else :
335
- code += "\n "
336
- return code
337
-
338
-
339
- @_api .deprecated ("3.5" )
340
- def split_code_at_show (text ):
308
+ def _split_code_at_show (text , function_name ):
341
309
"""Split code at plt.show()."""
342
- return _split_code_at_show (text )[1 ]
343
-
344
310
345
- def _split_code_at_show (text ):
346
- """Split code at plt.show()."""
347
- parts = []
348
311
is_doctest = contains_doctest (text )
349
- part = []
350
- for line in text .split ("\n " ):
351
- if (not is_doctest and line .strip () == 'plt.show()' ) or \
352
- (is_doctest and line .strip () == '>>> plt.show()' ):
353
- part .append (line )
312
+ if function_name is None :
313
+ parts = []
314
+ part = []
315
+ for line in text .split ("\n " ):
316
+ if ((not is_doctest and line .startswith ('plt.show(' )) or
317
+ (is_doctest and line .strip () == '>>> plt.show()' )):
318
+ part .append (line )
319
+ parts .append ("\n " .join (part ))
320
+ part = []
321
+ else :
322
+ part .append (line )
323
+ if "\n " .join (part ).strip ():
354
324
parts .append ("\n " .join (part ))
355
- part = []
356
- else :
357
- part .append (line )
358
- if "\n " .join (part ).strip ():
359
- parts .append ("\n " .join (part ))
325
+ else :
326
+ parts = [text ]
360
327
return is_doctest , parts
361
328
362
329
@@ -469,15 +436,6 @@ class PlotError(RuntimeError):
469
436
pass
470
437
471
438
472
- @_api .deprecated ("3.5" )
473
- def run_code (code , code_path , ns = None , function_name = None ):
474
- """
475
- Import a Python module from a path, and run the function given by
476
- name, if function_name is not None.
477
- """
478
- _run_code (unescape_doctest (code ), code_path , ns , function_name )
479
-
480
-
481
439
def _run_code (code , code_path , ns = None , function_name = None ):
482
440
"""
483
441
Import a Python module from a path, and run the function given by
@@ -566,28 +524,30 @@ def render_figures(code, code_path, output_dir, output_base, context,
566
524
Save the images under *output_dir* with file names derived from
567
525
*output_base*
568
526
"""
527
+ if function_name is not None :
528
+ output_base = f'{ output_base } _{ function_name } '
569
529
formats = get_plot_formats (config )
570
530
571
531
# Try to determine if all images already exist
572
532
573
- is_doctest , code_pieces = _split_code_at_show (code )
533
+ is_doctest , code_pieces = _split_code_at_show (code , function_name )
574
534
575
535
# Look for single-figure output files first
576
- all_exists = True
577
536
img = ImageFile (output_base , output_dir )
578
537
for format , dpi in formats :
579
538
if context or out_of_date (code_path , img .filename (format ),
580
539
includes = code_includes ):
581
540
all_exists = False
582
541
break
583
542
img .formats .append (format )
543
+ else :
544
+ all_exists = True
584
545
585
546
if all_exists :
586
547
return [(code , [img ])]
587
548
588
549
# Then look for multi-figure output files
589
550
results = []
590
- all_exists = True
591
551
for i , code_piece in enumerate (code_pieces ):
592
552
images = []
593
553
for j in itertools .count ():
@@ -611,6 +571,8 @@ def render_figures(code, code_path, output_dir, output_base, context,
611
571
if not all_exists :
612
572
break
613
573
results .append ((code_piece , images ))
574
+ else :
575
+ all_exists = True
614
576
615
577
if all_exists :
616
578
return results
@@ -793,13 +755,13 @@ def run(arguments, content, options, state_machine, state, lineno):
793
755
794
756
# make figures
795
757
try :
796
- results = render_figures (code ,
797
- source_file_name ,
798
- build_dir ,
799
- output_base ,
800
- keep_context ,
801
- function_name ,
802
- config ,
758
+ results = render_figures (code = code ,
759
+ code_path = source_file_name ,
760
+ output_dir = build_dir ,
761
+ output_base = output_base ,
762
+ context = keep_context ,
763
+ function_name = function_name ,
764
+ config = config ,
803
765
context_reset = context_opt == 'reset' ,
804
766
close_figs = context_opt == 'close-figs' ,
805
767
code_includes = source_file_includes )
0 commit comments