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