a
    ga                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 dd	l
mZ dd
lmZmZ ddlmZ ddlmZ ddlmZ e	G dd deZdS )z^Output class.

Represents a widget that can be used to display output within the widget area.
    Nwraps   )	DOMWidget)
TypedTuple)register   )"__jupyter_widgets_output_version__)UnicodeDict)InteractiveShellclear_output)get_ipythonc                   @   s   e Zd ZdZedjddZedjddZedjddZedjddZ	ee
jddZee
jddZeddd	jddZee d
djddZdZdd Zd!ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zd S )"Outputa  Widget used as a context manager to display output.

    This widget can capture and display stdout, stderr, and rich output.  To use
    it, create an instance of it and display it.

    You can then use the widget as a context manager: any output produced while in the
    context will be captured and displayed in the widget instead of the standard output
    area.

    You can also use the .capture() method to decorate a function or a method. Any output 
    produced by the function will then go to the output widget. This is useful for
    debugging widget callbacks, for example.

    Example::
        import ipywidgets as widgets
        from IPython.display import display
        out = widgets.Output()
        display(out)

        print('prints to output area')

        with out:
            print('prints to output widget')

        @out.capture()
        def func():
            print('prints to output widget')
    Z
OutputViewT)syncZOutputModelz@jupyter-widgets/output z(Parent message id of messages to capture)helpz-The output messages synced from the frontend.)Ztraitr   r   c                 O   s6   |  t |i | W d   n1 s(0    Y  dS )z
        Clear the content of the output widget.

        Parameters
        ----------

        wait: bool
            If True, wait to clear the output until new output is
            available to replace it. Default: False
        Nr   )selfZpargskwargs r   ?lib/python3.9/site-packages/ipywidgets/widgets/widget_output.pyr   A   s    zOutput.clear_outputFc                    s    fdd}|S )a  
        Decorator to capture the stdout and stderr of a function.

        Parameters
        ----------

        clear_output: bool
            If True, clear the content of the output widget at every
            new function call. Default: False

        wait: bool
            If True, wait to clear the output until new output is
            available to replace it. This is only used if clear_output
            is also True.
            Default: False
        c                    s    t   fdd}|S )Nc                     sJ   rj  i   | i |W  d    S 1 s<0    Y  d S )Nr   )argsr   )
clear_argsclear_kwargsr   funcr   r   r   innerb   s    z8Output.capture.<locals>.capture_decorator.<locals>.innerr   )r   r   r   r   r   r   )r   r   capture_decoratora   s    z)Output.capture.<locals>.capture_decoratorr   )r   r   r   r   r   r   r   r   captureP   s    zOutput.capturec                 C   sL   |    t }|rHt|drHt|jdrH|jjd d | _|  jd7  _dS )z3Called upon entering output widget context manager.kernel_parent_headerheadermsg_idr   N)_flushr   hasattrr    r!   r#   _Output__counter)r   ipr   r   r   	__enter__k   s
    zOutput.__enter__c                 C   sX   t  }|dur&|r&|j|||fdd |   |  jd8  _| jdkrLd| _|rTdS dS )z2Called upon exiting output widget context manager.Nr   )Z	tb_offsetr   r   T)r   Zshowtracebackr$   r&   r#   )r   etypeZevaluetbr'   r   r   r   __exit__s   s    
zOutput.__exit__c                 C   s   t j  t j  dS )z Flush stdout and stderr buffers.N)sysstdoutflushstderr)r   r   r   r   r$      s    
zOutput._flushc                 C   s   |  j d||df7  _ dS )zAppend a stream output.stream)output_typenametextN)outputs)r   r3   stream_namer   r   r   _append_stream_output   s    
zOutput._append_stream_outputc                 C   s   | j |dd dS )z!Append text to the stdout stream.r-   r5   Nr6   r   r3   r   r   r   append_stdout   s    zOutput.append_stdoutc                 C   s   | j |dd dS )z!Append text to the stderr stream.r/   r7   Nr8   r9   r   r   r   append_stderr   s    zOutput.append_stderrc                 C   s4   t  jj}||\}}|  jd||df7  _dS )a  Append a display object as an output.

        Parameters
        ----------
        display_object : IPython.core.display.DisplayObject
            The object to display (e.g., an instance of
            `IPython.display.Markdown` or `IPython.display.Image`).
        Zdisplay_data)r1   datametadataN)r   instanceZdisplay_formatterformatr4   )r   Zdisplay_objectZfmtr<   r=   r   r   r   append_display_data   s    	zOutput.append_display_dataN)F)__name__
__module____qualname____doc__r
   tagZ
_view_nameZ_model_nameZ_view_moduleZ_model_moduler	   Z_view_module_versionZ_model_module_versionr#   r   r   r4   r&   r   r   r(   r+   r$   r6   r:   r;   r@   r   r   r   r   r      s&   
r   )rD   r,   	functoolsr   Z	domwidgetr   Ztrait_typesr   Zwidgetr   Z_versionr	   Z	traitletsr
   r   ZIPython.core.interactiveshellr   ZIPython.displayr   ZIPythonr   r   r   r   r   r   <module>   s   