
    gI                         d dl 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	Zed
z   Zd ZddZddZddZddZddZddZd Zy)    N)Path)validate_coerce_fig_to_dictvalidate_coerce_output_type)
get_module)ImageUriValidatorc                   Z    e Zd ZdZd Zed        Zej                  d        Zed        Z	y)
JsonConfig)jsonorjsonautoc                     d| _         y )Nr   _default_engineselfs    L/var/www/dash_apps/app1/venv/lib/python3.12/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j                  t         j                  |            |dk(  r| j	                          || _        y )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   sT    j///%%+V*2K2KQTV%U 
 (?  ""r   c                 4    t        d      }|t        d      y )Nr   z-The orjson engine requires the orjson package)r   r   )clsr   s     r   r   zJsonConfig.validate_orjson%   s"    H%>LMM r   N)
__name__
__module____qualname__r   r   propertyr   setterclassmethodr    r   r   r	   r	      sP    /N& $ $ 
# 
# N Nr   r	   c                     | dv ry| S )zM
    This is used to ultimately *encode* into strict JSON, see `encode`

    )Infinityz	-InfinityNaNNr'   )consts    r   coerce_to_strictr,   /   s     00r   ))<z\u003c)>z\u003e)/z\u002f))u    z\u2028)u    z\u2029c                 L    | }|D ]  \  }}||v s|j                  ||      } |S r   )replace)json_str_swapoutunsafe_char	safe_chars        r   _safer7   F   s:    
C"' 6Y#++k95C6 Jr   c                 *   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        t        j                  | fd|i|t              S |dk(  rt        j                          |j                  |j                  z  }|r||j                  z  }	 | j                         } 	 t        |j                  | |      j#                  d      t$              S y# t         $ r Y =w xY w# t&        $ r Y nw xY wt)        | dd|      }t        |j                  ||      j#                  d      t$              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.utilsrH   r7   r
   dumps
_swap_jsonr	   r   OPT_NON_STR_KEYSOPT_SERIALIZE_NUMPYOPT_INDENT_2to_plotly_jsonAttributeErrordecode_swap_orjson	TypeErrorclean_to_json_compatible)plotly_objectprettyenginer   rM   optsrH   cleaneds           r   to_json_plotlyr`   N   s   < d3F ~&&FF	)	)2V;<< zu=e4u5KU;	G DN ",D9JJ}D*;DtDj
 	
 
8	""$&&)C)CCF'''D	)88:M
	]48??G  
  		  		 +!	
 V\\'$\7>>vFUUs$   6D8 /E 8	EE	EEc                     t        | |      }|r)|j                  dg       D ]  }|j                  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)r\   r]   )r   getpopr`   )figvalidater\   remove_uidsr]   fig_dicttraces          r   to_jsonrk      sO    N +39H \\&"- 	#EIIeT"	# (6&AAr   c                 &   t        | ||||      }t        |t              rt        |      }nt        |t              r|}nd}|	 |j	                  |       y|j                  |       y# t
        $ r Y nw xY wt        dj                  |            )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
    )rg   r\   rh   r]   NzX
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
)file)	rk   
isinstancestrr   writerV   r   r   
write_text)rf   rm   rg   r\   rh   r]   r2   paths           r   
write_jsonrs      s    H hv;vH $Dz	D$	  |	JJx  	!  		
F  
 	
s   A) )	A54A5c                    t        dd      }t        | t        t        f      s%t	        dj                  t        |       |             |t        j                  }|dk(  r|d}nd}n|dvrt	        d	|z        |dk(  r't        j                          |j                  |       }|S t        | t              r| j                  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   Tr9   zr
from_json_plotly requires a string or bytes argument but received value of type {typ}
    Received value: {value})typvaluer   r
   r;   r<   zutf-8)r   rn   ro   bytesr   r   typerN   r   r	   r   loadsrW   r
   )rv   r]   r   
value_dicts       r   from_json_plotlyr{   ,  s    6 d3F ec5\*%vKu  &  
 	
 ~&&FF	)	)2V;<<""$\\%(
 	 eU#LL)EZZ&
r   c                 J    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
    )r]   )skip_invalid)r{   r   )rv   output_typer}   r]   ri   r    rf   s          r   	from_jsonr   m  s0    P  f5H &k
2C h\
2CJr   c                     t        | t              }t        | t              rt        |       }nt        | t              r| }nd}||j                         }n| j	                         }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)r}   r~   r]   )rn   ro   r   	read_textreadr   )rm   r~   r}   r]   file_is_strrr   r2   s          r   	read_jsonr     sq    J T3'K$Dz	D$	  >>#99; |V r   c           
      &
   t        | t        t        t        f      r| S t        | t              r.| j                         D ci c]  \  }}|t        |fi | c}}S t        | t        t        f      r| r| D cg c]  }t        |fi | c}S |j                  dd      }|j                  dd      }|j                  di       }|d   }|d   }|d   }	|d   }
|2| |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|j%                  |       S | j                   j"                  dk(  r|j'                  |       j)                         S | j                   j"                  dk(  r| j)                         S | j                   j"                  dk(  r2| j)                         } n!t        | |j*                        rt        |       S |	V| |	j,                  u ry t        | |	j.                  |	j0                  f      r$|r3| j                   j"                  d
v r|j%                  | j2                        S | j                   j"                  dk(  rt        | |	j.                        rot5        j6                         5  t5        j8                  dt:               |j=                  | j>                  jA                               j)                         }d d d        n| jA                         j)                         }|s/tC        tE                    D ]  }||   jG                         ||<    S 	 | jA                         } |s	 | jG                         S t        | tL        jL                        r| S 	 | j)                         S c c}}w c c}w # 1 sw Y   xY w# tH        tJ        f$ r Y hw xY w# tH        tJ        f$ r Y Nw xY w# tJ        $ r Y nw xY wt        | tN        jP                        rt        |       S |
+t        | |
jR                        rtU        jV                  |       S 	 | jY                         } n# tJ        $ r Y nw xY wt        | t              r4| j                         D ci c]  \  }}|t        |fi | c}}S c c}}w t        | t        t        f      r!| r| D cg c]  }t        |fi | c}S c c}w | S )NrK   FrL   rM   r?   r@   rA   rB   nan)biufMUOignore)-rn   intfloatro   dictitemsrZ   listtuplerd   RRZZmacoremaskedndarraydtypekindascontiguousarraydatetime_as_stringtolist
datetime64NaTSeriesDatetimeIndexvalueswarningscatch_warningssimplefilterFutureWarningarraydtto_pydatetimerangelen	isoformatrY   rV   datetimedecimalDecimalImager   pil_image_to_urirU   )objkwargskvrK   rL   rM   r?   r@   rA   rB   	dt_valuesr   s                r   rZ   rZ     s}   
 #UC()
#tEHYY[QTQ+A888QQ	C$	'CFGa,Q9&9GG JJ6Mzz"4e<jjB'Gz"H	B	BGE (++:HKKs8O 
~"%%**###<RZZ(3G!G++C003&,,S188::3&zz|#3&jjlR]]+s8O 
~"&&=bii)9)9:;3G!G++CJJ773&c299-!002 N --hF %'HHSVV-A-A-C$D$K$K$M		N N !$ 1 1 3 : : <I' #3y>2 @'0|'='='?	!@ !  ! 	==?" 
C**	+
zz|k R H\N N, ~&  >* 		   #w'Sz ZU[[9 11#66  "  #tEHYY[QTQ+A888QQQ	C$	'CFGa,Q9&9GGGJsm   O8OAOO* O? P O'*O<;O<?PP	P P 6R 	RR9S6T)FN)TFTNr   )FigureFN)r
   r   r   r   pathlibr   plotly.io._utilsr   r   _plotly_utils.optional_importsr   _plotly_utils.basevalidatorsr   objectr	   rN   r,   rQ   rX   r7   r`   rk   rs   r{   r   r   rZ   r'   r   r   <module>r      s         U 5 :
N N< 
	

   ]V@/BdI"X>B1h<~xr   