o
    ^ig                     @   s   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
mZmZ ddlmZ eG d	d
 d
eeeZeG dd deZeG dd deZeG dd deZdS )    N   )
CoreWidget)	DOMWidget)ValueWidget)register)UnicodeCUnicodeBool)CByteMemoryViewc                       sj   e Zd ZdZeddjddZedd Zedd	 Z	d
d Z
edd Zedd Z fddZ  ZS )_Mediaa+  Base class for Image, Audio and Video widgets.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw data that you want the browser to display.

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    z)The media data as a memory view of bytes.helpTsyncc                 K   s@   |  |}d|vr| ||}|dur||d< | dd|i|S )aB  
        Create an :class:`Media` from a local file.

        Parameters
        ----------
        filename: str
            The location of a file to read into the value from disk.

        **kwargs:
            The keyword arguments for `Media`

        Returns an `Media` with the value set from the filename.
        formatNvalue )_load_file_value_guess_format)clstagfilenamekwargsr   r   r   r   Z/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/ipywidgets/widgets/widget_media.py
_from_file   s   
z_Media._from_filec                 K   s(   t |tr
|d}| d|dd|S )aq  
        Create an :class:`Media` from a URL.

        :code:`Media.from_url(url)` is equivalent to:

        .. code-block: python

            med = Media(value=url, format='url')

        But both unicode and bytes arguments are allowed for ``url``.

        Parameters
        ----------
        url: [str, bytes]
            The location of a URL to load.
        zutf-8url)r   r   Nr   )
isinstancestrencode)r   r   r   r   r   r   from_url4   s   

z_Media.from_urlc                 C   s   |  |}|| _dS )z
        Convenience method for reading a file into `value`.

        Parameters
        ----------
        filename: str
            The location of a file to read into value from disk.
        N)r   r   )selfr   r   r   r   r   set_value_from_fileL   s   
	
z_Media.set_value_from_filec                 C   sN   t |dd d ur| S t|d}| W  d    S 1 s w   Y  d S )Nreadrb)getattrr"   open)r   r   fr   r   r   r   Y   s
   $z_Media._load_file_valuec                 C   sf   t |dd }|p	|}zt|\}}|d|sW d S |td|d  W S  ty2   Y d S w )Nnamez{}/)r$   	mimetypes
guess_type
startswithr   len	Exception)r   r   r   r'   mtype_r   r   r   r   a   s   z_Media._guess_formatc                    s   | j j}g }d| jd d  }| jjdkr$|d d d |d  }|| t||  D ]}|dkr7q0t	t
| |}|d|| q0d|}d||S )	Nz
value={!r}(   z...r   z{}={!r}z, z{}({}))	__class____name__r   r   tobytesnbytesappendsuper
_repr_keysr   r$   join)r    r   
class_name	signature	sig_valuekeyr   r1   r   r   	_get_reprp   s   

z_Media._get_repr)r2   
__module____qualname____doc__r
   r   r   classmethodr   r   r!   r   r   r>   __classcell__r   r   r=   r   r      s    




r   c                       s   e Zd ZdZedjddZedjddZedddjddZe	d	djddZ
e	d
djddZ fddZedd Zdd Z  ZS )Imagea  Displays an image as a widget.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw image data that you want the browser to display.  You can explicitly
    define the format of the byte string using the `format` trait (which
    defaults to "png").

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    	ImageViewTr   
ImageModelpngzThe format of the image.r   zFWidth of the image in pixels. Use layout.width for styling the widget.zHHeight of the image in pixels. Use layout.height for styling the widget.c                    s   t  j|i | d S N)r6   __init__)r    argsr   r=   r   r   rI      s   zImage.__init__c                 K      | j d|fi |S )Nimager   r   r   r   r   r   r   	from_file      zImage.from_filec                 C   
   |  tS rH   )r>   rD   r    r   r   r   __repr__      
zImage.__repr__)r2   r?   r@   rA   r   r   
_view_name_model_namer   r   widthheightrI   rB   rO   rS   rC   r   r   r=   r   rD      s    

rD   c                   @   s   e Zd ZdZedjddZedjddZedddjddZe	d	djddZ
e	d
djddZedddjddZedddjddZedddjddZedd Zdd ZdS )Videoa  Displays a video as a widget.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw video data that you want the browser to display.  You can explicitly
    define the format of the byte string using the `format` trait (which
    defaults to "mp4").

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    	VideoViewTr   
VideoModelmp4zThe format of the video.r   zWidth of the video in pixels.zHeight of the video in pixels.z/When true, the video starts when it's displayedzBWhen true, the video will start from the beginning after finishingzSSpecifies that video controls should be displayed (such as a play/pause button etc)c                 K   rK   )NvideorM   rN   r   r   r   rO      rP   zVideo.from_filec                 C   rQ   rH   )r>   rY   rR   r   r   r   rS      rT   zVideo.__repr__N)r2   r?   r@   rA   r   r   rU   rV   r   r   rW   rX   r	   autoplayloopcontrolsrB   rO   rS   r   r   r   r   rY      s    

rY   c                   @   s   e Zd ZdZedjddZedjddZedddjddZe	dd	djddZ
e	dd
djddZe	dddjddZedd Zdd ZdS )Audioa  Displays a audio as a widget.

    The `value` of this widget accepts a byte string.  The byte string is the
    raw audio data that you want the browser to display.  You can explicitly
    define the format of the byte string using the `format` trait (which
    defaults to "mp3").

    If you pass `"url"` to the `"format"` trait, `value` will be interpreted
    as a URL as bytes encoded in UTF-8.
    	AudioViewTr   
AudioModelmp3zThe format of the audio.r   z/When true, the audio starts when it's displayedzBWhen true, the audio will start from the beginning after finishingzSSpecifies that audio controls should be displayed (such as a play/pause button etc)c                 K   rK   )NaudiorM   rN   r   r   r   rO      rP   zAudio.from_filec                 C   rQ   rH   )r>   ra   rR   r   r   r   rS      rT   zAudio.__repr__N)r2   r?   r@   rA   r   r   rU   rV   r   r	   r^   r_   r`   rB   rO   rS   r   r   r   r   ra      s    

ra   )r(   widget_corer   	domwidgetr   valuewidgetr   widgetr   	traitletsr   r   r	   trait_typesr
   r   rD   rY   ra   r   r   r   r   <module>   s   w 