B
    &[_h                 @   s\   d dl mZ d dlmZ d dlZd dlmZmZ dddZdd	d
Z	dddZ
dddZdS )    )absolute_import)string_typesN)validate_coerce_fig_to_dictvalidate_coerce_output_typeTFc             C   sp   ddl m} t| |}|r<x |dg D ]}|dd q(W ddi}|rRd|d	< nd
|d< tj|fd|i|S )a  
    Convert a figure to a JSON string representation

    Parameters
    ----------
    fig:
        Figure object or dict representing a figure

    validate: bool (default True)
        True if the figure should be validated before being converted to
        JSON, False otherwise.

    pretty: bool (default False)
        True if JSON representation should be pretty-printed, False if
        representation should be as compact as possible.

    remove_uids: bool (default True)
        True if trace UIDs should be omitted from the JSON representation

    Returns
    -------
    str
        Representation of figure as a JSON string
    r   )PlotlyJSONEncoderdatauidN	sort_keysT   indent),:
separatorscls)Z_plotly_utils.utilsr   r   getpopjsondumps)figvalidateprettyremove_uidsr   fig_dicttraceopts r   3/tmp/pip-install-l29rncou/plotly/plotly/io/_json.pyto_json
   s    

r   c          	   C   sN   t | |||d}t|t}|r@t|d}|| W dQ R X n
|| dS )aj  
    Convert a figure to JSON and write it to a file or writeable
    object

    Parameters
    ----------
    fig:
        Figure object or dict representing a figure

    file: str or writeable
        A string representing a local file path or a writeable object
        (e.g. an open file descriptor)

    pretty: bool (default False)
        True if JSON representation should be pretty-printed, False if
        representation should be as compact as possible.

    remove_uids: bool (default True)
        True if trace UIDs should be omitted from the JSON representation

    Returns
    -------
    None
    )r   r   r   wN)r   
isinstancer   openwrite)r   filer   r   r   json_strfile_is_strfr   r   r   
write_json;   s    
r&   Figurec             C   sB   t | ts tdjt| | dt| }t|}|||d}|S )a  
    Construct a figure from a JSON string

    Parameters
    ----------
    value: str
        String containing the JSON representation of a figure

    output_type: type or str (default 'Figure')
        The output figure type or type name.
        One of:  graph_objs.Figure, 'Figure', graph_objs.FigureWidget, 'FigureWidget'

    skip_invalid: bool (default False)
        False if invalid figure properties should result in an exception.
        True if invalid figure properties should be silently ignored.

    Raises
    ------
    ValueError
        if value is not a string, or if skip_invalid=False and value contains
        invalid figure properties

    Returns
    -------
    Figure or FigureWidget
    zb
from_json requires a string argument but received value of type {typ}
    Received value: {value})typvalue)skip_invalid)r   r   
ValueErrorformattyper   loadsr   )r)   output_typer*   r   r   r   r   r   r   	from_jsong   s    

r0   c          	   C   sD   t | t}|r.t| d}| }W dQ R X n|  }t|||dS )a  
    Construct a figure from the JSON contents of a local file or readable
    Python object

    Parameters
    ----------
    file: str or readable
       A string containing the path to a local file or a read-able Python
       object (e.g. an open file descriptor)

    output_type: type or str (default 'Figure')
        The output figure type or type name.
        One of:  graph_objs.Figure, 'Figure', graph_objs.FigureWidget, 'FigureWidget'

    skip_invalid: bool (default False)
        False if invalid figure properties should result in an exception.
        True if invalid figure properties should be silently ignored.

    Returns
    -------
    Figure or FigureWidget
    rN)r*   r/   )r   r   r    readr0   )r"   r/   r*   r$   r%   r#   r   r   r   	read_json   s    
r3   )TFT)TFT)r'   F)r'   F)
__future__r   sixr   r   Zplotly.io._utilsr   r   r   r&   r0   r3   r   r   r   r   <module>   s   
1
,
5