o
    h2                     @  sF  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r9d dlmZ nd dlmZ ed	 Zd
ed< e						d9d:ddZe						d9d;d!dZe						d9d<d#dZe						d9d=d&dZ						d>d?d(dZ		d@dAd0d1ZdBd3d4Zd5d6 ZdCd7d8ZdS )D    )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   MimeBundleFormat.specdict[str, Any]formatLiteral['json', 'vega-lite']modeLiteral['vega-lite'] | Nonevega_version
str | Nonevegaembed_versionvegalite_versionembed_optionsdict[str, Any] | NoneengineLiteral['vl-convert'] | Nonereturndict[str, dict[str, Any]]c           	      K     d S N 	r   r   r   r   r   r    r!   r#   kwargsr)   r)   K/var/www/vscode/kcb/lib/python3.10/site-packages/altair/utils/mimebundle.pyspec_to_mimebundle      r-   Literal['html']dict[str, str]c           	      K  r'   r(   r)   r*   r)   r)   r,   r-   "   r.   Literal['pdf', 'svg', 'vega']c           	      K  r'   r(   r)   r*   r)   r)   r,   r-   .   r.   Literal['png']%tuple[dict[str, Any], dict[str, Any]]c           	      K  r'   r(   r)   r*   r)   r)   r,   r-   :   r.   6dict[str, Any] | tuple[dict[str, Any], dict[str, Any]]c                 K  s   ddl m}	 ddlm}
m} |dkrd}t||}| r#|
| } d}|du r/|	jdi }n|}t|}|d	v rTt	| t
td
 ||f||dd|ddd|S |dkrjt| f|||||d|}d|iS |dkr|du rxd}t|d|d  d| iS |dkrd| 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'])altairr5   altair.utils.displayr6   r7   
ValueErroroptionsgetpreprocess_embed_options_spec_to_mimebundle_with_enginer   r   r   )r   r   r   r   r   r    r!   r#   r+   r5   r6   r7   msginternal_modefinal_embed_optionsr   r)   r)   r,   r-   F   s`   2

		dict$Literal['png', 'svg', 'pdf', 'vega']Literal['vega-lite', 'vega']r:   str | dict | Noner;   r   c                 K  s  | dd}t||}|dkrt }t }	|dkr.|dkr#| }
d|
iS |j| |	d}
d|
iS |dkrO|dkrB|j| ||d}d
|iS |j| |	||d	}d
|iS |dkr|dd}d}|d|}|dkrp|j| ||||d}n|j	| |	||||d}|| }t
|\}}d|id|| || difS |dkr|dd}|dkr|j| |||d}d|iS |j| |	|||d}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   )r:   r;   )rK   r:   r;   zimage/svg+xmlr   scale_factorr   H   ppi)scalerN   r:   r;   )rK   rO   rN   r:   r;   z	image/png)widthheightr   )rO   r:   r;   )rK   rO   r:   r;   zapplication/pdfzUnexpected format zUnexpected normalized_engine )pop_validate_normalize_enginer	   r
   vegalite_to_vegavega_to_svgvegalite_to_svgr@   vega_to_pngvegalite_to_png_pngxyvega_to_pdfvegalite_to_pdfr>   )r   r   r   r:   r;   r+   r#   normalized_enginevlcrK   vgr   rO   default_ppirN   r   factorwhr   rC   r)   r)   r,   rB      s   "


rB   strc                 C  s   zt  }W n ty   d}Y nw | du rdn|  dddd}|dkr3|du r1d}t||S |du rI|dur?d}|S d|d}t|d	| d
}t|)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- _rJ   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replacer>   )r#   r   r]   r\   rC   r)   r)   r,   rS   &  s*   
"

rS   c                 C  s&   |  d}td| |d |d  S )zU
    read the (width, height) from a PNG header.

    Taken from IPython.display
    s   IHDRz>ii      )indexstructunpack)dataihdrr)   r)   r,   rY   Q  s   
rY   c                 C  sd   | pi   } | dd}t|trt }||| d< | dd}t|tr0t }||| 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.
    r8   Nr9   )copyr@   
isinstancerc   r	   get_format_localeget_time_format_locale)r!   r:   r]   r;   r)   r)   r,   rA   \  s   

rA   )......)r   r   r   r   r   r   r   r   r   r   r    r   r!   r"   r#   r$   r%   r&   )r   r   r   r/   r   r   r   r   r   r   r    r   r!   r"   r#   r$   r%   r0   )r   r   r   r1   r   r   r   r   r   r   r    r   r!   r"   r#   r$   r%   r   )r   r   r   r2   r   r   r   r   r   r   r    r   r!   r"   r#   r$   r%   r3   )NNNNNN)r   r   r   r   r   r   r   r   r   r   r    r   r!   r"   r#   r$   r%   r4   )NN)r   rF   r   rG   r   rH   r:   rI   r;   rI   r%   r   )r#   r$   r   rG   r%   rc   )r!   rF   r%   rF   )
__future__r   rm   typingr   r   r   r   r   
_importersr	   r
   r   r   sysversion_infor   typing_extensionsr   __annotations__r-   rB   rS   rY   rA   r)   r)   r)   r,   <module>   st    
n
v+