
    cF                         d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ  G d de          Z e            Zd	 ZddZddZddZddZddZddZd ZdS )    )absolute_importN)Path)validate_coerce_fig_to_dictvalidate_coerce_output_type)
get_module)ImageUriValidatorc                   d    e Zd ZdZd Zed             Zej        d             Zed             Z	dS )
JsonConfig)jsonorjsonautoc                     d| _         d S )Nr   _default_engineselfs    /lib/python3.11/site-packages/plotly/io/_json.py__init__zJsonConfig.__init__   s    %    c                     | j         S Nr   r   s    r   default_enginezJsonConfig.default_engine   s    ##r   c                     |t           j        vr.t          d                    t           j        |                    |dk    r|                                  || _        d S )Nz9Supported JSON engines include {valid}
    Received {val})validvalr   )r
   _valid_engines
ValueErrorformatvalidate_orjsonr   )r   r   s     r   r   zJsonConfig.default_engine   sl    j// 	%%+V*2KQTV%U%U  
 (? 	#  """"r   c                 F    t          d          }|t          d          d S )Nr   z-The orjson engine requires the orjson package)r   r   )clsr   s     r   r   zJsonConfig.validate_orjson&   s3    H%% 	NLMMM	N 	Nr   N)
__name__
__module____qualname__r   r   propertyr   setterclassmethodr    r   r   r
   r
      s        /N& & & $ $ X$ 
# 
# 
# N N [N N Nr   r
   c                     | dv rdS | S )zM
    This is used to ultimately *encode* into strict JSON, see `encode`

    )Infinityz	-InfinityNaNNr(   )consts    r   coerce_to_strictr-   0   s     00 tr   Fc                     t          dd          }|t          j        }|dk    r|d}nd}n|dvrt          d|z            t          d	d
          t          dd
          t          dd
          t          dd
          d}|dk    r)i }|rd|d<   nd|d<   ddlm} t          j        | fd|i|S |dk    rt          	                                 |j
        |j        z  }|r
||j        z  }	 |                                 } n# t          $ r Y nw xY w	 |                    | |                              d          S # t           $ r Y nw xY wt#          | dd|          }|                    ||                              d          S dS )a  
    Convert a plotly/Dash object to a JSON string representation

    Parameters
    ----------
    plotly_object:
        A plotly/Dash object represented as a dict, graph_object, or Dash component

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

    engine: str (default None)
        The JSON encoding engine to use. One of:
          - "json" for an engine based on the built-in Python json module
          - "orjson" for a faster engine that requires the orjson package
          - "auto" for the "orjson" engine if available, otherwise "json"
        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    str
        Representation of input object as a JSON string

    See Also
    --------
    to_json : Convert a plotly Figure to JSON with validation
    r   Tshould_loadNr   r   r   r   Invalid json engine: %szsage.allFnumpypandasz	PIL.Image)sage_allnppdimage   indent),:
separatorsr   )PlotlyJSONEncoderr!   )optionutf8)numpy_alloweddatetime_allowedmodules)r   configr   r   _plotly_utils.utilsr>   r   dumpsr
   r   OPT_NON_STR_KEYSOPT_SERIALIZE_NUMPYOPT_INDENT_2to_plotly_jsonAttributeErrordecode	TypeErrorclean_to_json_compatible)plotly_objectprettyenginer   rC   optsr>   cleaneds           r   to_json_plotlyrT   <   s9   < d333F  '& = 	FFFF	)	) =2V;<<< zu===e444u555KU;;;	 G  $A 	,DNN ",D999999z-GG->G$GGG	8	 A""$$$&)CC 	(F''D	)88::MM 	 	 	D		<<d<;;BB6JJJ 	 	 	D	 +!	
 
 
 ||GD|1188@@@3A As$   1D 
DD)E 
EETc                     t          | |          }|r/|                    dg           D ]}|                    dd           t          |||          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

    engine: str (default None)
        The JSON encoding engine to use. One of:
          - "json" for an engine based on the built-in Python json module
          - "orjson" for a faster engine that requires the orjson package
          - "auto" for the "orjson" engine if available, otherwise "json"
        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    str
        Representation of figure as a JSON string

    See Also
    --------
    to_json_plotly : Convert an arbitrary plotly graph_object or Dash component to JSON
    datauidN)rP   rQ   )r   getpoprT   )figvalidaterP   remove_uidsrQ   fig_dicttraces          r   to_jsonr_      sg    N +399H  #\\&"-- 	# 	#EIIeT""""(6&AAAAr   c                 p   t          | ||||          }t          |t                    rt          |          }nt          |t                    r|}nd}|K	 |                    |           dS # t
          $ r Y nw xY wt          d                    |                    |                    |           dS )a+  
    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. a pathlib.Path object or 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

    engine: str (default None)
        The JSON encoding engine to use. One of:
          - "json" for an engine based on the built-in Python json module
          - "orjson" for a faster engine that requires the orjson package
          - "auto" for the "orjson" engine if available, otherwise "json"
        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.
    Returns
    -------
    None
    )r[   rP   r\   rQ   NzX
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
)file)	r_   
isinstancestrr   writerK   r   r   
write_text)rZ   ra   r[   rP   r\   rQ   json_strpaths           r   
write_jsonrh      s    H hv;v  H $ 	Dzz	D$		    "	JJx   F 	 	 	D	
F   
 
 	
 	!!!!!s   A. .
A;:A;c                    t          dd          }t          | t          t          f          s1t	          d                    t          |           |                     |t          j        }|dk    r|d}nd}n|d	vrt	          d
|z            |dk    r/t          
                                 |                    |           }n>t          | t                    r|                     d          } t          j        |           }|S )a  
    Parse JSON string using the specified JSON engine

    Parameters
    ----------
    value: str or bytes
        A JSON string or bytes object

    engine: str (default None)
        The JSON decoding engine to use. One of:
          - if "json", parse JSON using built in json module
          - if "orjson", parse using the faster orjson module, requires the orjson
            package
          - if "auto" use orjson module if available, otherwise use the json module

        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    dict

    See Also
    --------
    from_json_plotly : Parse JSON with plotly conventions into a dict
    r   Tr/   zr
from_json_plotly requires a string or bytes argument but received value of type {typ}
    Received value: {value})typvalueNr   r   r1   r2   zutf-8)r   rb   rc   bytesr   r   typerD   r   r
   r   loadsrL   r   )rk   rQ   r   
value_dicts       r   from_json_plotlyrp     s'   6 d333F ec5\** 
%vKKu  &    
 
 	
  '& = 	FFFF	)	) =2V;<<< '""$$$\\%((

 eU## 	*LL))EZ&&
r   Figurec                 `    t          | |          }t          |          } |||          }|S )a  
    Construct a figure from a JSON string

    Parameters
    ----------
    value: str or bytes
        String or bytes object 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.

    engine: str (default None)
        The JSON decoding engine to use. One of:
          - if "json", parse JSON using built in json module
          - if "orjson", parse using the faster orjson module, requires the orjson
            package
          - if "auto" use orjson module if available, otherwise use the json module

        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

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

    Returns
    -------
    Figure or FigureWidget
    )rQ   )skip_invalid)rp   r   )rk   output_typers   rQ   r]   r!   rZ   s          r   	from_jsonru   W  sA    P  f555H &k
2
2C #h\
2
2
2CJr   c                 &   t          | t                    }t          | t                    rt          |           }nt          | t                    r| }nd}||                                }n|                                 }t          ||||          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. a pathlib.Path object or 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.

    engine: str (default None)
        The JSON decoding engine to use. One of:
          - if "json", parse JSON using built in json module
          - if "orjson", parse using the faster orjson module, requires the orjson
            package
          - if "auto" use orjson module if available, otherwise use the json module

        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    Figure or FigureWidget
    N)rs   rt   rQ   )rb   rc   r   	read_textreadru   )ra   rt   rs   rQ   file_is_strrg   rf   s          r   	read_jsonrz     s    J T3''K$ 	Dzz	D$		    >>##99;; |V   r   c                 D	   t          | t          t          t          f          r| S t          | t                    r fd|                                 D             S t          | t          t          f          r| rfd| D             S                     dd          }                    dd          }                    di           }|d         }|d         }|d	         }|d
         }|0| |j	        v rt          |           S | |j
        v rt          |           S | | |j        j        j        u rt          d          S t          | |j                  r|r#| j        j        dv r|                    |           S | j        j        dk    r'|                    |                                           S | j        j        dk    r|                                 S | j        j        dk    r|                                 } n$t          | |j                  rt          |           S || |j        u rd S t          | |j        |j        f          r|r(| j        j        dv r|                    | j                  S | j        j        dk    rt          | |j                  r,| j                                                                        }	n&|                                                                 }	|s<t7          t9          |	                    D ]}
|	|
                                         |	|
<    |	S 	 |                                 } n# t<          t>          f$ r Y nw xY w|s,	 |                                 S # t<          t>          f$ r Y n w xY wt          | t@          j                   r| S 	 |                                 S # t>          $ r Y nw xY wt          | tB          j"                  rt          |           S |)t          | |j#                  rtI          j%        |           S 	 | &                                } n# t>          $ r Y nw xY wt          | t                    r fd|                                 D             S t          | t          t          f          r| rfd| D             S | S )Nc                 2    i | ]\  }}|t          |fi S r(   rN   .0kvkwargss      r   
<dictcomp>z,clean_to_json_compatible.<locals>.<dictcomp>  0    QQQTQ+A8888QQQr   c                 *    g | ]}t          |fi S r(   r}   r   r   r   s     r   
<listcomp>z,clean_to_json_compatible.<locals>.<listcomp>  *    GGGa,Q99&99GGGr   rA   FrB   rC   r5   r6   r7   r8   nan)biufMUOc                 2    i | ]\  }}|t          |fi S r(   r}   r~   s      r   r   z,clean_to_json_compatible.<locals>.<dictcomp>8  r   r   c                 *    g | ]}t          |fi S r(   r}   r   s     r   r   z,clean_to_json_compatible.<locals>.<listcomp><  r   r   )'rb   intfloatrc   dictitemslisttuplerX   RRZZmacoremaskedndarraydtypekindascontiguousarraydatetime_as_stringtolist
datetime64NaTSeriesDatetimeIndexvaluesdtto_pydatetimerangelen	isoformatrM   rK   datetimedecimalDecimalImager   pil_image_to_urirJ   )objr   rA   rB   rC   r5   r6   r7   r8   	dt_valuesr   s    `         r   rN   rN     s   
 #UC()) 
#t HQQQQSYY[[QQQQ	C$	'	' H 	HGGGG3GGGG JJ66Mzz"4e<<jjB''Gz"H	B	BGE  (+ 	::HK 	s88O 
 "%*## 	<<RZ(( 	 	#3G!G 	#++C0003& #,,S1188:::3& #zz||#3& #jjllR]++ 	s88O 
 !"&= 	!4bi)9:;; 	! !3G!G !++CJ7773& !c29-- = # 4 4 6 6 = = ? ?II # 1 1 3 3 : : < <I' @ #3y>>22 @ @'0|'='='?'?	!   !!~&     	==??">* 	 	 	D		C*	+	+ 
zz||    #w'' Szz  7ZU[99 7 1#666  ""    #t HQQQQSYY[[QQQQ	C$	'	' H 	HGGGG3GGGGJsH   *L? ?MMM- -N N!N5 5
OOP/ /
P<;P<)FN)TFTNr   )rq   FN)
__future__r   r   r   r   pathlibr   plotly.io._utilsr   r   _plotly_utils.optional_importsr   _plotly_utils.basevalidatorsr   objectr
   rD   r-   rT   r_   rh   rp   ru   rz   rN   r(   r   r   <module>r      sz   & & & & & &          U U U U U U U U 5 5 5 5 5 5 : : : : : :
N N N N N N N N< 
	 	 	YA YA YA YAx/B /B /B /BdI" I" I" I"X> > > >B1 1 1 1h< < < <~t t t t tr   