Show More
@@ -9,11 +9,15 like an interactive ipython section. | |||
|
9 | 9 | |
|
10 | 10 | To enable this directive, simply list it in your Sphinx ``conf.py`` file |
|
11 | 11 | (making sure the directory where you placed it is visible to sphinx, as is |
|
12 | needed for all Sphinx directives). | |
|
12 | needed for all Sphinx directives). For example, to enable syntax highlighting | |
|
13 | and the IPython directive:: | |
|
14 | ||
|
15 | extensions = ['IPython.sphinxext.ipython_console_highlighting', | |
|
16 | 'IPython.sphinxext.ipython_directive'] | |
|
13 | 17 | |
|
14 | 18 | By default this directive assumes that your prompts are unchanged IPython ones, |
|
15 | 19 | but this can be customized. The configurable options that can be placed in |
|
16 | conf.py are | |
|
20 | conf.py are: | |
|
17 | 21 | |
|
18 | 22 | ipython_savefig_dir: |
|
19 | 23 | The directory in which to save the figures. This is relative to the |
@@ -31,10 +35,21 ipython_promptin: | |||
|
31 | 35 | The default is 'In [%d]:'. This expects that the line numbers are used |
|
32 | 36 | in the prompt. |
|
33 | 37 | ipython_promptout: |
|
34 | ||
|
35 | 38 | The string to represent the IPython prompt in the generated ReST. The |
|
36 | 39 | default is 'Out [%d]:'. This expects that the line numbers are used |
|
37 | 40 | in the prompt. |
|
41 | ipython_mplbackend: | |
|
42 | The string which specifies if the embedded Sphinx shell should import | |
|
43 | Matplotlib and set the backend for each code-block. If `None`, or equal | |
|
44 | to '' or 'None', then `matplotlib` will not be automatically imported. If | |
|
45 | not `None`, then the value should specify a backend that is passed to | |
|
46 | `matplotlib.use()`. The default value is 'agg'. | |
|
47 | ||
|
48 | As an example, to use the IPython directive when `matplotlib` is not available, | |
|
49 | one sets the backend to `None`:: | |
|
50 | ||
|
51 | ipython_mplbacked = None | |
|
52 | ||
|
38 | 53 | |
|
39 | 54 | ToDo |
|
40 | 55 | ---- |
@@ -71,14 +86,11 except ImportError: | |||
|
71 | 86 | from md5 import md5 |
|
72 | 87 | |
|
73 | 88 | # Third-party |
|
74 | import matplotlib | |
|
75 | 89 | import sphinx |
|
76 | 90 | from docutils.parsers.rst import directives |
|
77 | 91 | from docutils import nodes |
|
78 | 92 | from sphinx.util.compat import Directive |
|
79 | 93 | |
|
80 | matplotlib.use('Agg') | |
|
81 | ||
|
82 | 94 | # Our own |
|
83 | 95 | from IPython import Config, InteractiveShell |
|
84 | 96 | from IPython.core.profiledir import ProfileDir |
@@ -119,8 +131,8 def block_parser(part, rgxin, rgxout, fmtin, fmtout): | |||
|
119 | 131 | |
|
120 | 132 | |
|
121 | 133 | OUTPUT: the output string, possibly multi-line |
|
122 | """ | |
|
123 | 134 |
|
|
135 | """ | |
|
124 | 136 | block = [] |
|
125 | 137 | lines = part.split('\n') |
|
126 | 138 | N = len(lines) |
@@ -561,30 +573,34 class IPythonDirective(Directive): | |||
|
561 | 573 | savefig_dir = os.path.join(confdir, savefig_dir) |
|
562 | 574 | |
|
563 | 575 | # get regex and prompt stuff |
|
564 | rgxin = config.ipython_rgxin | |
|
565 | rgxout = config.ipython_rgxout | |
|
566 | promptin = config.ipython_promptin | |
|
567 | promptout = config.ipython_promptout | |
|
576 | rgxin = config.ipython_rgxin | |
|
577 | rgxout = config.ipython_rgxout | |
|
578 | promptin = config.ipython_promptin | |
|
579 | promptout = config.ipython_promptout | |
|
580 | mplbackend = config.ipython_mplbackend | |
|
568 | 581 | |
|
569 |
return savefig_dir, source_dir, rgxin, rgxout, |
|
|
582 | return (savefig_dir, source_dir, rgxin, rgxout, | |
|
583 | promptin, promptout, mplbackend) | |
|
570 | 584 | |
|
571 | 585 | def setup(self): |
|
586 | # Get configuration values. | |
|
587 | (savefig_dir, source_dir, rgxin, rgxout, | |
|
588 | promptin, promptout, mplbackend) = self.get_config_options() | |
|
589 | ||
|
572 | 590 | if self.shell is None: |
|
573 | 591 | self.shell = EmbeddedSphinxShell() |
|
592 | if mplbackend: | |
|
593 | # Each ipython code-block is run in a separate process. | |
|
594 | import matplotlib | |
|
595 | matplotlib.use(mplbackend) | |
|
596 | ||
|
574 | 597 | # reset the execution count if we haven't processed this doc |
|
575 | 598 | #NOTE: this may be borked if there are multiple seen_doc tmp files |
|
576 | 599 | #check time stamp? |
|
577 | ||
|
578 | 600 | if not self.state.document.current_source in self.seen_docs: |
|
579 |
|
|
|
580 |
|
|
|
581 |
|
|
|
582 | ||
|
583 | ||
|
584 | ||
|
585 | # get config values | |
|
586 | (savefig_dir, source_dir, rgxin, | |
|
587 | rgxout, promptin, promptout) = self.get_config_options() | |
|
601 | self.shell.IP.history_manager.reset() | |
|
602 | self.shell.IP.execution_count = 1 | |
|
603 | self.seen_docs.add(self.state.document.current_source) | |
|
588 | 604 | |
|
589 | 605 | # and attach to shell so we don't have to pass them around |
|
590 | 606 | self.shell.rgxin = rgxin |
@@ -595,7 +611,6 class IPythonDirective(Directive): | |||
|
595 | 611 | self.shell.source_dir = source_dir |
|
596 | 612 | |
|
597 | 613 | # setup bookmark for saving figures directory |
|
598 | ||
|
599 | 614 | self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir, |
|
600 | 615 | store_history=False) |
|
601 | 616 | self.shell.clear_cout() |
@@ -621,7 +636,6 class IPythonDirective(Directive): | |||
|
621 | 636 | self.shell.is_doctest = 'doctest' in options |
|
622 | 637 | self.shell.is_verbatim = 'verbatim' in options |
|
623 | 638 | |
|
624 | ||
|
625 | 639 | # handle pure python code |
|
626 | 640 | if 'python' in self.arguments: |
|
627 | 641 | content = self.content |
@@ -633,9 +647,7 class IPythonDirective(Directive): | |||
|
633 | 647 | figures = [] |
|
634 | 648 | |
|
635 | 649 | for part in parts: |
|
636 | ||
|
637 | 650 | block = block_parser(part, rgxin, rgxout, promptin, promptout) |
|
638 | ||
|
639 | 651 | if len(block): |
|
640 | 652 | rows, figure = self.shell.process_block(block) |
|
641 | 653 | for row in rows: |
@@ -644,28 +656,19 class IPythonDirective(Directive): | |||
|
644 | 656 | if figure is not None: |
|
645 | 657 | figures.append(figure) |
|
646 | 658 | |
|
647 | #text = '\n'.join(lines) | |
|
648 | #figs = '\n'.join(figures) | |
|
649 | ||
|
650 | 659 | for figure in figures: |
|
651 | 660 | lines.append('') |
|
652 | 661 | lines.extend(figure.split('\n')) |
|
653 | 662 | lines.append('') |
|
654 | 663 | |
|
655 | #print lines | |
|
656 | 664 | if len(lines)>2: |
|
657 | 665 | if debug: |
|
658 | 666 | print('\n'.join(lines)) |
|
659 | else: #NOTE: this raises some errors, what's it for? | |
|
660 | #print 'INSERTING %d lines'%len(lines) | |
|
667 | else: | |
|
668 | # This is what makes the lines appear in the final output. | |
|
661 | 669 | self.state_machine.insert_input( |
|
662 | 670 | lines, self.state_machine.input_lines.source(0)) |
|
663 | 671 | |
|
664 | text = '\n'.join(lines) | |
|
665 | txtnode = nodes.literal_block(text, text) | |
|
666 | txtnode['language'] = 'ipython' | |
|
667 | #imgnode = nodes.image(figs) | |
|
668 | ||
|
669 | 672 | # cleanup |
|
670 | 673 | self.teardown() |
|
671 | 674 | |
@@ -676,13 +679,14 def setup(app): | |||
|
676 | 679 | setup.app = app |
|
677 | 680 | |
|
678 | 681 | app.add_directive('ipython', IPythonDirective) |
|
679 |
app.add_config_value('ipython_savefig_dir', None, |
|
|
682 | app.add_config_value('ipython_savefig_dir', None, 'env') | |
|
680 | 683 | app.add_config_value('ipython_rgxin', |
|
681 |
re.compile('In \[(\d+)\]:\s?(.*)\s*'), |
|
|
684 | re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env') | |
|
682 | 685 | app.add_config_value('ipython_rgxout', |
|
683 |
re.compile('Out\[(\d+)\]:\s?(.*)\s*'), |
|
|
684 |
app.add_config_value('ipython_promptin', 'In [%d]:', |
|
|
685 |
app.add_config_value('ipython_promptout', 'Out[%d]:', |
|
|
686 | re.compile('Out\[(\d+)\]:\s?(.*)\s*'), 'env') | |
|
687 | app.add_config_value('ipython_promptin', 'In [%d]:', 'env') | |
|
688 | app.add_config_value('ipython_promptout', 'Out[%d]:', 'env') | |
|
689 | app.add_config_value('ipython_mplbackend', 'agg', 'env') | |
|
686 | 690 | |
|
687 | 691 | |
|
688 | 692 | # Simple smoke test, needs to be converted to a proper automatic test. |
General Comments 0
You need to be logged in to leave comments.
Login now