U
    Zfl                  
   @   sd   d dl Z d dlZd dlZdddZdddZdddZdd	 Zdd
dZeeeeeeeeed	ZdS )    Nc              
   C   sD   |dkrt | }t| d}t| ||W  5 Q R  S Q R X dS )a  
    Convert a file into the format expected by the Download component.
    :param path: path to the file to be sent
    :param filename: name of the file, if not provided the original filename is used
    :param type: type of the file (optional, passed to Blob in the javascript layer)
    :return: dict of file content (base64 encoded) and meta data used by the Download component
    Nrb)ntpathbasenameopen
send_bytesread)pathfilenametypef r   4/tmp/pip-unpacked-wheel-47crqvv_/dash/dcc/express.py	send_file   s    	
r   c                 K   s:   t | tr| ntt | f|}tt| ||ddS )a  
    Convert data written to BytesIO into the format expected by the Download component.
    :param src: array of bytes or a writer that can write to BytesIO
    :param filename: the name of the file
    :param type: type of the file (optional, passed to Blob in the javascript layer)
    :return: dict of data frame content (base64 encoded) and meta data used by the Download component
    Tcontentr	   r
   base64)	
isinstancebytes
_io_to_strioBytesIOdictr   	b64encodedecodesrcr	   r
   kwargsr   r   r   r   r      s     r   c                 K   s0   t | tr| ntt | f|}t|||ddS )a  
    Convert data written to StringIO into the format expected by the Download component.
    :param src: a string or a writer that can write to StringIO
    :param filename: the name of the file
    :param type: type of the file (optional, passed to Blob in the javascript layer)
    :return: dict of data frame content (NOT base64 encoded) and meta data used by the Download component
    Fr   )r   strr   r   StringIOr   r   r   r   r   send_string)   s     r   c                 K   s.   | j }dd | _ || f| |  }|  |S )Nc                   S   s   d S )Nr   r   r   r   r   <lambda>8       z_io_to_str.<locals>.<lambda>)closegetvalue)Zdata_iowriterr   Zdata_io_closeZ
data_valuer   r   r   r   5   s    
r   c                 K   s4   | j }|t kr td|t| | ||f|S )a  
    Convert data frame into the format expected by the Download component.
    :param writer: a data frame writer
    :param filename: the name of the file
    :param type: type of the file (optional, passed to Blob in the javascript layer)
    :return: dict of data frame content (base64 encoded) and meta data used by the Download component

    Examples
    --------

    >>> df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': [2, 1, 5, 6], 'c': ['x', 'x', 'y', 'y']})
    ...
    >>> send_data_frame(df.to_csv, "mydf.csv")  # download as csv
    >>> send_data_frame(df.to_json, "mydf.json")  # download as json
    >>> send_data_frame(df.to_excel, "mydf.xls", index=False) # download as excel
    >>> send_data_frame(df.to_pickle, "mydf.pkl") # download as pickle

    zZThe provided writer ({}) is not supported, try calling send_string or send_bytes directly.)__name___data_frame_senderskeys
ValueErrorformat)r$   r	   r
   r   namer   r   r   send_data_frame@   s    r+   )	Zto_csvZto_jsonZto_htmlZto_excelZ
to_featherZ
to_parquetZ
to_msgpackZto_stataZ	to_pickle)NN)N)N)N)	r   r   r   r   r   r   r   r+   r&   r   r   r   r   <module>   s"   



