
    g2                    0   U d dl mZ d dlZd dlmZmZmZmZmZ ddl	m
Z
mZ ddlmZ er d dlZej                  dk\  rd dlmZ nd dlmZ ed	   Zd
ed<   e	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd ZddZy)     )annotationsN)TYPE_CHECKINGAnyLiteralcastoverload   )import_vl_convertvl_version_for_vl_convert)spec_to_html)   
   )	TypeAlias)htmljsonpngsvgpdfvega	vega-liter   MimeBundleFormatc                     y N 	specformatmodevega_versionvegaembed_versionvegalite_versionembed_optionsenginekwargss	            L/var/www/openai/venv/lib/python3.12/site-packages/altair/utils/mimebundle.pyspec_to_mimebundler&      s     !$    c                     y r   r   r   s	            r%   r&   r&   "        r'   c                     y r   r   r   s	            r%   r&   r&   .   r)   r'   c                     y r   r   r   s	            r%   r&   r&   :   s     -0r'   c           	        ddl m}	 ddlm}
m} |dk7  rd}t        |      |} |       r
 |
|       } d}||	j                  j                  di       }n|}t        |      }|d	v rCt        | t        t        d
   |      |f||j                  dd      |j                  dd      d|S |dk(  rt        | f|||||d|}d|iS |dk(  r|d}t        |      d|d    d| iS |dk(  rd| iS d}t        |      )aE  
    Convert a vega-lite specification to a mimebundle.

    The mimebundle type is controlled by the ``format`` argument, which can be
    one of the following ['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite']

    Parameters
    ----------
    spec : dict
        a dictionary representing a vega-lite plot spec
    format : string {'html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'}
        the file format to be saved.
    mode : string {'vega-lite'}
        The rendering mode.
    vega_version : string
        The version of vega.js to use
    vegaembed_version : string
        The version of vegaembed.js to use
    vegalite_version : string
        The version of vegalite.js to use. Only required if mode=='vega-lite'
    embed_options : dict (optional)
        The vegaEmbed options dictionary. Defaults to the embed options set with
        alt.renderers.set_embed_options().
        (See https://github.com/vega/vega-embed for details)
    engine: string {'vl-convert'}
        the conversion engine to use for 'png', 'svg', 'pdf', and 'vega' formats
    **kwargs :
        Additional arguments will be passed to the generating function

    Returns
    -------
    output : dict
        a mime-bundle representing the image

    Note
    ----
    The png, svg, pdf, and vega outputs require the vl-convert package
    r   )	renderers)compile_with_vegafusionusing_vegafusionr   zmode must be 'vega-lite'r   Nr"   >   r   r   r   r   )r   r   r   r   formatLocaletimeFormatLocale)r#   format_localetime_format_localer   )r   r   r    r!   r"   z	text/htmlzMust specify vegalite_versionzapplication/vnd.vegalite.vz+jsonr   zapplication/jsonzPformat must be one of ['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'])altairr-   altair.utils.displayr.   r/   
ValueErroroptionsgetpreprocess_embed_options_spec_to_mimebundle_with_enginer   r   r   )r   r   r   r   r    r!   r"   r#   r$   r-   r.   r/   msginternal_modefinal_embed_optionsr   s                   r%   r&   r&   F   ss   d !N{(o26M&t, '//33ORH+,-@AM...45v>
 '++NDA,001CTJ
 
 	
 
6	
%/-'
 
 T""	;	#1CS/!,-=a-@,AGNN	6	"D))I 	 or'   c                   |j                  dd      }t        ||      }|dk(  rWt               }t               }	|dk(  r"|dk(  r| }
d|
iS |j	                  | |	      }
d|
iS |dk(  r6|dk(  r|j                  | ||      }d
|iS |j                  | |	||	      }d
|iS |dk(  r}|j                  dd      }d}|j                  d|      }|dk(  r|j                  | ||||      }n|j                  | |	||||      }||z  }t        |      \  }}d|id||z  ||z  difS |dk(  rJ|j                  dd      }|dk(  r|j                  | |||      }d|iS |j                  | |	|||      }d|iS d|}t        |      d|}t        |      )a  
    Helper for Vega-Lite to mimebundle conversions that require an engine.

    Parameters
    ----------
    spec : dict
        a dictionary representing a vega-lite plot spec
    format : string {'png', 'svg', 'pdf', 'vega'}
        the format of the mimebundle to be returned
    mode : string {'vega-lite', 'vega'}
        The rendering mode.
    engine: string {'vl-convert'}
        the conversion engine to use
    format_locale : str or dict
        d3-format locale name or dictionary. Defaults to "en-US" for United States English.
        See https://github.com/d3/d3-format/tree/main/locale for available names and example
        definitions.
    time_format_locale : str or dict
        d3-time-format locale name or dictionary. Defaults to "en-US" for United States English.
        See https://github.com/d3/d3-time-format/tree/main/locale for available names and example
        definitions.
    **kwargs :
        Additional arguments will be passed to the conversion function
    r#   N	vlconvertr   )
vl_versionzapplication/vnd.vega.v5+jsonr   )r2   r3   )r@   r2   r3   zimage/svg+xmlr   scale_factorr	   H   ppi)scalerC   r2   r3   )r@   rD   rC   r2   r3   z	image/png)widthheightr   )rD   r2   r3   )r@   rD   r2   r3   zapplication/pdfzUnexpected format zUnexpected normalized_engine )pop_validate_normalize_enginer
   r   vegalite_to_vegavega_to_svgvegalite_to_svgr8   vega_to_pngvegalite_to_png_pngxyvega_to_pdfvegalite_to_pdfr6   )r   r   r   r2   r3   r$   r#   normalized_enginevlcr@   vgr   rD   default_ppirC   r   factorwhr   r;   s                        r%   r:   r:      sU   D ZZ$'F266BK'!.0
Vv~ 3B77 ))$:)F2B77u_v~oo"/'9 &  $S)) )))"/'9	 *  $S))u_JJ~q1EK**UK0Cv~oo"/'9 &  )))"/'9 *  ;&F#;DAq%q6zQZH(   u_JJ~q1Ev~oo"/'9	 &  &s++ )))"/'9 *  &s++ 'vj1CS/! ..?-BCor'   c                "   	 t               }| dn/| j                         j                  dd      j                  dd      }|dk(  r|d}t	        |      |S ||d}|S d|d}t	        |      d	| d
}t	        |      # t        $ r d}Y w xY w)z
    Helper to validate and normalize the user-provided engine.

    engine : {None, 'vl-convert'}
        the user-provided engine string
    format : string {'png', 'svg', 'pdf', 'vega'}
        the format of the mimebundle to be returned
    N- _r?   zIThe 'vl-convert' conversion engine requires the vl-convert-python packagezSaving charts in z format requires the vl-convert-python package: see https://altair-viz.github.io/user_guide/saving_charts.html#png-svg-and-pdf-formatzInvalid conversion engine z. Expected vl-convert)r
   ImportErrorlowerreplacer6   )r#   r   rR   rQ   r;   s        r%   rH   rH   &  s    ! FLLN$:$:3$C$K$KCQS$T 
 K';]CS/!  
	"? +  $F: .h h  S/!*6*4IJo1  s   
B   BBc                b    | j                  d      }t        j                  d| |dz   |dz          S )zU
    read the (width, height) from a PNG header.

    Taken from IPython.display
    s   IHDRz>ii      )indexstructunpack)dataihdrs     r%   rN   rN   Q  s2     ::gD==TAXr	 :;;r'   c                .   | xs i j                         } | j                  dd      }t        |t              rt	               }|j                  |      | d<   | j                  dd      }t        |t              rt	               }|j                  |      | d<   | S )a  
    Preprocess embed options to a form compatible with Vega Embed.

    Parameters
    ----------
    embed_options : dict
        The embed options dictionary to preprocess.

    Returns
    -------
    embed_opts : dict
        The preprocessed embed options dictionary.
    r0   Nr1   )copyr8   
isinstancestrr
   get_format_localeget_time_format_locale)r"   r2   rR   r3   s       r%   r9   r9   \  s     #(b..0M "%%nd;M-%!(+(=(=m(Ln%&**+=tD$c*!,/,F,F-
() r'   )......)r   dict[str, Any]r   zLiteral['json', 'vega-lite']r   Literal['vega-lite'] | Noner   
str | Noner    ro   r!   ro   r"   dict[str, Any] | Noner#   Literal['vl-convert'] | Nonereturnzdict[str, dict[str, Any]])r   rm   r   zLiteral['html']r   rn   r   ro   r    ro   r!   ro   r"   rp   r#   rq   rr   zdict[str, str])r   rm   r   zLiteral['pdf', 'svg', 'vega']r   rn   r   ro   r    ro   r!   ro   r"   rp   r#   rq   rr   rm   )r   rm   r   zLiteral['png']r   rn   r   ro   r    ro   r!   ro   r"   rp   r#   rq   rr   z%tuple[dict[str, Any], dict[str, Any]])NNNNNN)r   rm   r   r   r   rn   r   ro   r    ro   r!   ro   r"   rp   r#   rq   rr   z6dict[str, Any] | tuple[dict[str, Any], dict[str, Any]])NN)r   dictr   $Literal['png', 'svg', 'pdf', 'vega']r   zLiteral['vega-lite', 'vega']r2   str | dict | Noner3   ru   rr   r   )r#   rq   r   rt   rr   rj   )r"   rs   rr   rs   )
__future__r   rc   typingr   r   r   r   r   
_importersr
   r   r   r   sysversion_infor   typing_extensionsr   __annotations__r&   r:   rH   rN   r9   r   r'   r%   <module>r}      s/   "  > > D 
7"$/%< ) 
 
 ),"$'#&+.+.
$

$(
$ &
$ 	
$
 "
$ !
$ )
$ )
$ 
$ 

$ 
 ),"$'#&+.+.



 &
 	

 "
 !
 )
 )
 
 

 
 ),"$'#&+.+.


)
 &
 	

 "
 !
 )
 )
 
 

 
 ),"$'#&+.+.
0

0
0 &
0 	
0
 "
0 !
0 )
0 )
0 +
0 

0 )-#$(#'+/+/g
gg &g 	g
 "g !g )g )g <g\ (,,0s
s0s 's %	s
 *s 	sl(((0( 	(V<r'   