| Server IP : 35.80.110.71 / Your IP : 216.73.216.52 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ip-172-31-21-44 6.17.0-1019-aws #19~24.04.1-Ubuntu SMP Tue Jun 23 18:53:06 UTC 2026 x86_64 User : ubuntu ( 1000) PHP Version : 8.3.31 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /usr/lib/python3/dist-packages/IPython/sphinxext/__pycache__/ |
Upload File : |
�
}�e]� � � � d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlmZ ddl
mZ ddl
mZ ddlmZ ddlmZ ddlmZ dd lmZ d
Z ddlZdZ ed� \ ZZZg d
�Zd� Z G d� de!� Z" G d� de� Z#d� Z$d� Z%e&dk( r=ejN jQ d� s ejR d� e%� e*d� yy# e$ r Y �yw xY w)aA
Sphinx directive to support embedded IPython code.
IPython provides an extension for `Sphinx <http://www.sphinx-doc.org/>`_ to
highlight and run code.
This directive allows pasting of entire interactive IPython sessions, prompts
and all, and their code will actually get re-executed at doc build time, with
all prompts renumbered sequentially. It also allows you to input code as a pure
python input by giving the argument python to the directive. The output looks
like an interactive ipython section.
Here is an example of how the IPython directive can
**run** python code, at build time.
.. ipython::
In [1]: 1+1
In [1]: import datetime
...: datetime.date.fromisoformat('2022-02-22')
It supports IPython construct that plain
Python does not understand (like magics):
.. ipython::
In [0]: import time
In [0]: %pdoc time.sleep
This will also support top-level async when using IPython 7.0+
.. ipython::
In [2]: import asyncio
...: print('before')
...: await asyncio.sleep(1)
...: print('after')
The namespace will persist across multiple code chucks, Let's define a variable:
.. ipython::
In [0]: who = "World"
And now say hello:
.. ipython::
In [0]: print('Hello,', who)
If the current section raises an exception, you can add the ``:okexcept:`` flag
to the current block, otherwise the build will fail.
.. ipython::
:okexcept:
In [1]: 1/0
IPython Sphinx directive module
===============================
To enable this directive, simply list it in your Sphinx ``conf.py`` file
(making sure the directory where you placed it is visible to sphinx, as is
needed for all Sphinx directives). For example, to enable syntax highlighting
and the IPython directive::
extensions = ['IPython.sphinxext.ipython_console_highlighting',
'IPython.sphinxext.ipython_directive']
The IPython directive outputs code-blocks with the language 'ipython'. So
if you do not have the syntax highlighting extension enabled as well, then
all rendered code-blocks will be uncolored. By default this directive assumes
that your prompts are unchanged IPython ones, but this can be customized.
The configurable options that can be placed in conf.py are:
ipython_savefig_dir:
The directory in which to save the figures. This is relative to the
Sphinx source directory. The default is `html_static_path`.
ipython_rgxin:
The compiled regular expression to denote the start of IPython input
lines. The default is ``re.compile('In \[(\d+)\]:\s?(.*)\s*')``. You
shouldn't need to change this.
ipython_warning_is_error: [default to True]
Fail the build if something unexpected happen, for example if a block raise
an exception but does not have the `:okexcept:` flag. The exact behavior of
what is considered strict, may change between the sphinx directive version.
ipython_rgxout:
The compiled regular expression to denote the start of IPython output
lines. The default is ``re.compile('Out\[(\d+)\]:\s?(.*)\s*')``. You
shouldn't need to change this.
ipython_promptin:
The string to represent the IPython input prompt in the generated ReST.
The default is ``'In [%d]:'``. This expects that the line numbers are used
in the prompt.
ipython_promptout:
The string to represent the IPython prompt in the generated ReST. The
default is ``'Out [%d]:'``. This expects that the line numbers are used
in the prompt.
ipython_mplbackend:
The string which specifies if the embedded Sphinx shell should import
Matplotlib and set the backend. The value specifies a backend that is
passed to `matplotlib.use()` before any lines in `ipython_execlines` are
executed. If not specified in conf.py, then the default value of 'agg' is
used. To use the IPython directive without matplotlib as a dependency, set
the value to `None`. It may end up that matplotlib is still imported
if the user specifies so in `ipython_execlines` or makes use of the
@savefig pseudo decorator.
ipython_execlines:
A list of strings to be exec'd in the embedded Sphinx shell. Typical
usage is to make certain packages always available. Set this to an empty
list if you wish to have no imports always available. If specified in
``conf.py`` as `None`, then it has the effect of making no imports available.
If omitted from conf.py altogether, then the default value of
['import numpy as np', 'import matplotlib.pyplot as plt'] is used.
ipython_holdcount
When the @suppress pseudo-decorator is used, the execution count can be
incremented or not. The default behavior is to hold the execution count,
corresponding to a value of `True`. Set this to `False` to increment
the execution count after each suppressed command.
As an example, to use the IPython directive when `matplotlib` is not available,
one sets the backend to `None`::
ipython_mplbackend = None
An example usage of the directive is:
.. code-block:: rst
.. ipython::
In [1]: x = 1
In [2]: y = x**2
In [3]: print(y)
See http://matplotlib.org/sampledoc/ipython_directive.html for additional
documentation.
Pseudo-Decorators
=================
Note: Only one decorator is supported per input. If more than one decorator
is specified, then only the last one is used.
In addition to the Pseudo-Decorators/options described at the above link,
several enhancements have been made. The directive will emit a message to the
console at build-time if code-execution resulted in an exception or warning.
You can suppress these on a per-block basis by specifying the :okexcept:
or :okwarning: options:
.. code-block:: rst
.. ipython::
:okexcept:
:okwarning:
In [1]: 1/0
In [2]: # raise warning.
To Do
=====
- Turn the ad-hoc test() function into a real test suite.
- Break up ipython-specific functionality from matplotlib stuff into better
separated code.
� N)�StringIO)�
directives)� Directive)�logging)�Config)�InteractiveShell)�
ProfileDirFT� )�suppress�verbatim�savefig�doctestc �^ �� g }| j d� }t |� }d}d} ||k( r |S || }
|dz
}|
j � ��j d� r|j t
|
f� �Lt
�fd�t D � � r| rt d� ��} �t|j |
� }|�r t |j d� � |j d� }
}d d
j dgt t |� � dz z � z }t |� }g }||k rt|| }|j |� }|s|j d� rnJ|j |� r||d }|r
|d dk( r|dd }|
d|z z
}
n|j |� |dz
}||k r�t|j t | |
dj |� ff� ���|j |
� }|ret |j d� � |j d� }}||dz
k rdj |g||d z � }|j t |f� |S ��
)
a�
part is a string of ipython text, comprised of at most one
input, one output, comments, and blank lines. The block parser
parses the text into a list of::
blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
data is, depending on the type of token::
COMMENT : the comment string
INPUT: the (DECORATOR, INPUT_LINE, REST) where
DECORATOR: the input decorator (or None)
INPUT_LINE: the input as string (possibly multi-line)
REST : any stdout generated by the input line (not OUTPUT)
OUTPUT: the output string, possibly multi-line
�
r N� �#c 3 �F �K � | ] }�j d |z � �� � y�w��@N��
startswith��.0�pseudo_decorator�
line_strippeds ��E/usr/lib/python3/dist-packages/IPython/sphinxext/ipython_directive.py� <genexpr>zblock_parser.<locals>.<genexpr> s* �� �� �
� �
�$�$�S�+;�%;�<�
�� �!z@Applying multiple pseudo-decorators on one line is not supported� � %s:� �.� )�split�len�stripr �append�COMMENT�any�PSEUDO_DECORATORS�RuntimeError�match�int�group�join�str�INPUT�OUTPUT)�part�rgxin�rgxout�fmtin�fmtout�block�lines�N�i� decorator�line�matchin�lineno� inputline�continuation�Nc�rest�nextline�matchout�outputr s @r �block_parserrG � sT �� �*
�E��J�J�t��E��E�
�A� �A��I�
��a�4��Z �L�W �Q�x�� �Q����
�
��
��#�#�C�(��L�L�'�4��)���
�$5�
�
� �"�V�� � *� �� �+�+�d�#��� #�G�M�M�!�$4� 5�w�}�}�Q�7G�I�F� $�B�G�G�S�E�3�s�6�{�3C�A�3E�,F�$G�G�L��\�"�B� �D��A�#�
!��8��!�<�<��1���x�2�2�3�7���(�(��6� (���}�H��H�Q�K�3�$6�#+�A�B�<�����!1�1�I��K�K��)��A���1 �A�#�4
�L�L�%�)�Y�� � �$��!H�I�J�� �<�<��%��� �����!2�3�X�^�^�A�5F�F�F���1��u����F�8�e�A�B�i�#7�8���L�L�&�&�)�*���L�c � c �h � e Zd ZdZdd�Zd� Zd� Zd� Zdd�Zd� Z d � Z
d
� Zd� Zd� Z
d
� Zd� Zd� Zd� Zy)�EmbeddedSphinxShellz1An embedded IPython instance to run inside SphinxNc �� � t � | _ |�g }t � }d|j _ d|j
_ d|j
_ d|j
_ t j d�� }d}t j j ||� }t j |� }t j ||�� }t# j$ | j&